如何以编程方式获取完整的 android 应用 UID?
How to get a full android app UID programmatically?
编辑 2020 年 4 月 10 日
我可能给我们要找的东西起错了名字。它实际上可能是安装的应用程序的 linux 用户名,而不是它的 UID。那么我们应该如何以编程方式获得它呢?
原题如下
当我们使用 adb shell ps
甚至在非 root android 设备上时,它 returns 处理信息,其中 UID 以 u0_xxxx
的形式出现,其中 x
表示十六进制的一些任意数字(system/root 进程除外)。
例如
u0_a464 31481 894 5015336 69200 0 0 S com.example.app_uid_checker
在此示例中,app_uid_checker 是我在用户 space 中的应用。但是,当尝试以编程方式获取 UID 时,我得到 10464(a464 的十进制表示),并且没有 u0
前缀。
我试过了
- 包管理器的 getApplicationInfo()
- activity 经理的 getAllRunningProcess()
- android.os.Process.myUid()
(遵循 this post on SO 中的建议。他们都是 return 10464。我如何获得 "full" UID?(为了更好的术语,我不确定是什么与 10464 版本相比,应调用 UID 的 u0_a464 版本)。
即使我们可以以编程方式使用adb shell ps
我认为这可能不是一个好方法,因为 adb 需要启用开发人员模式。
您需要使用 geteuid(2) 和 getpwuid(3) 来检索数据,因为 JVM 不会公开它。
extern "C" JNIEXPORT jstring JNICALL Java_com_example_GetUser_getUser(JNIEnv* env) {
uid_t uid = geteuid();
struct passwd *user;
if (uid == -1)
return NULL;
user = getpwuid(uid);
return env->NewStringUTF(user->pw_name);
}
编辑 2020 年 4 月 10 日
我可能给我们要找的东西起错了名字。它实际上可能是安装的应用程序的 linux 用户名,而不是它的 UID。那么我们应该如何以编程方式获得它呢?
原题如下
当我们使用 adb shell ps
甚至在非 root android 设备上时,它 returns 处理信息,其中 UID 以 u0_xxxx
的形式出现,其中 x
表示十六进制的一些任意数字(system/root 进程除外)。
例如
u0_a464 31481 894 5015336 69200 0 0 S com.example.app_uid_checker
在此示例中,app_uid_checker 是我在用户 space 中的应用。但是,当尝试以编程方式获取 UID 时,我得到 10464(a464 的十进制表示),并且没有 u0
前缀。
我试过了
- 包管理器的 getApplicationInfo()
- activity 经理的 getAllRunningProcess()
- android.os.Process.myUid()
(遵循 this post on SO 中的建议。他们都是 return 10464。我如何获得 "full" UID?(为了更好的术语,我不确定是什么与 10464 版本相比,应调用 UID 的 u0_a464 版本)。
即使我们可以以编程方式使用adb shell ps
我认为这可能不是一个好方法,因为 adb 需要启用开发人员模式。
您需要使用 geteuid(2) 和 getpwuid(3) 来检索数据,因为 JVM 不会公开它。
extern "C" JNIEXPORT jstring JNICALL Java_com_example_GetUser_getUser(JNIEnv* env) {
uid_t uid = geteuid();
struct passwd *user;
if (uid == -1)
return NULL;
user = getpwuid(uid);
return env->NewStringUTF(user->pw_name);
}