问题
I would like to be able to get the Linux UID (user ID) of an installed Android application.
Excerpt from Security and Permissions: "At install time, Android gives each package a distinct Linux user ID. The identity remains constant for the duration of the package's life on that device."
Is there a way to retrieve this UID?
回答1:
Use PackageManager
and getApplicationInfo()
.
回答2:
adb shell dumpsys package com.example.myapp | grep userId=
回答3:
- The
packages.xml
file present in/data/system
- The
packages.list
file present in/data/system
Contain the list of applications installed and their corresponding UID's.
回答4:
PackageManager packageManager = getPackageManager();
try {
applicationId = String.valueOf(packageManager.getApplicationInfo("com.example.app", PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
回答5:
As CommonsWare already wrote, you can use PackageManager
to get the UID.
Here's an example:
int uid;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(
context.getPackageName(), 0);
uid = info.uid;
} catch (PackageManager.NameNotFoundException e) {
uid = -1;
}
Log.i(LOG_TAG, "UID = " + uid);
来源:https://stackoverflow.com/questions/6123434/obtain-the-linux-uid-of-an-android-app