android中uid用于标识一个应用程序,uid在应用安装时被分配,并且在应用存在于手机上期间,都不会改变,范围是从10000开始,到19999结束,而且,UID由用户ID(UserId)和应用ID(AppId)共同决定
frameworks/base/core/java/android/os/UserHandle.java
/**
* @hide Range of uids allocated for a user.
*/
public static final int PER_USER_RANGE = 100000;
/**
* Returns the uid that is composed from the userId and the appId.
* @hide
*/
public static int getUid(@UserIdInt int userId, @AppIdInt int appId) {
if (MU_ENABLED) {
//默认不创建多个用户时,android 只有一个用户,uid==aapId
eturn userId * PER_USER_RANGE + (appId % PER_USER_RANGE);
} else {
return appId;
}
方式1:abd shell cat data/system/packages.xml,里面包含所有应用的uid和一些有关其的信息(需要root)
方式2:adb shell dumpsys package 应用包名 | adb shell grep userId=
方式3:adb shell cat /proc/
1、adb shell ps -A | adb shell grep -r “com.example.testpor”(-A:高android版本的adb shell需要带-A参数),其中,第二列30209就是pid
octopus-n106:/ # ps | grep -r "com.example.testpor"
(standard input):u0_a65 30209 1662 1006360 48836 ep_poll aae7b2d8 S com.example.testpor
2、adb shell cat /proc/30209 /status | grep Uid
Uid: 10065 10065 10065 10065
系统中会有多个用户 (User,即手机里的主机、访客等多用户), 每个用户也有一个唯一的 ID 值, 称为"UserId"
一个进程的appId是固定的,在初始化或安装的时候就决定了
frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
10170 private void commitScanResultsLocked(@NonNull ScanRequest request, @NonNull ScanResult result)
10171 throws PackageManagerException {
....
10184
10185 if (newPkgSettingCreated) {
10186 if (originalPkgSetting != null) {
10187 mSettings.addRenamedPackageLPw(pkg.packageName, originalPkgSetting.name);
10188 }
10189 // THROWS: when we can't allocate a user id. add call to check if there's
10190 // enough space to ensure we won't throw; otherwise, don't modify state
10191 mSettings.addUserToSettingLPw(pkgSetting);
10192
10193 if (originalPkgSetting != null && (scanFlags & SCAN_CHECK_ONLY) == 0) {
10194 mTransferedPackages.add(originalPkgSetting.name);
10195 }
10196 }
frameworks/base/services/core/java/com/android/server/pm/Settings.java
882 void addUserToSettingLPw(PackageSetting p) throws PackageManagerException {
883 if (p.appId == 0) {
884 // Assign new user ID
885 p.appId = newUserIdLPw(p);
886 } else {
887 // Add new setting to list of user IDs
888 addUserIdLPw(p.appId, p, p.name);
889 }
890 if (p.appId < 0) {
891 PackageManagerService.reportSettingsProblem(Log.WARN,
892 "Package " + p.name + " could not be assigned a valid UID");
893 throw new PackageManagerException(INSTALL_FAILED_INSUFFICIENT_STORAGE,
894 "Package " + p.name + " could not be assigned a valid UID");
895 }
896 }
4213 private int newUserIdLPw(Object obj) {
4214 // Let's be stupidly inefficient for now...
4215 final int N = mUserIds.size();
4216 for (int i = mFirstAvailableUid; i < N; i++) {
4217 if (mUserIds.get(i) == null) {
4218 mUserIds.set(i, obj);
4219 return Process.FIRST_APPLICATION_UID + i;
4220 }
4221 }
4222
4223 // None left?
4224 if (N > (Process.LAST_APPLICATION_UID-Process.FIRST_APPLICATION_UID)) {
4225 return -1;
4226 }
4227
4228 mUserIds.add(obj);
4229 return Process.FIRST_APPLICATION_UID + N;
4230 }
Pid就是各进程的身份标识,程序一运行系统就会自动分配给进程一个独一无二的PID。进程中止后PID被系统回收,可能会被继续分配给新运行的程序,但是在android系统中一般不会把已经kill掉的进程ID重新分配给新的进程,新产生进程的进程号,一般比产生之前所有的进程号都要大。