简体   繁体   English

如何以编程方式获取完整的 android 应用 UID?

[英]How to get a full android app UID programmatically?

Edit 10 Apr 2020编辑 2020 年 4 月 10 日

I may have misnamed what we are looking for.我可能把我们要找的名字弄错了。 It may actually be the linux user name of the installed app, rather than its UID.它实际上可能是已安装应用程序的linux 用户名,而不是其 UID。 So how should we get that programmatically?那么我们应该如何以编程方式获得它呢?

Original question below下面的原始问题

When we use adb shell ps even on a non rooted android device, it returns process info where the UID comes in the form u0_xxxx where x represents some arbitrary digits in hex (except for system/root processes).当我们在非 root 的 android 设备上使用adb shell ps ,它返回进程信息,其中 UID 以u0_xxxx的形式u0_xxxx ,其中x表示一些任意的十六进制数字(系统/root 进程除外)。

For example例如

u0_a464 31481 894 5015336 69200 0 0 S com.example.app_uid_checker u0_a464 31481 894 5015336 69200 0 0 S com.example.app_uid_checker

In this example app_uid_checker is my app in user space.在这个例子中 app_uid_checker 是我在用户空间中的应用程序。 When trying to obtain the UID programmatically, though, I get 10464 (the decimal representation of a464), and without the u0 prefix.但是,当尝试以编程方式获取 UID 时,我得到 10464(a464 的十进制表示),并且没有u0前缀。

I tried我试过

  1. package manager's getApplicationInfo()包管理器的 getApplicationInfo()
  2. activity manager's getAllRunningProcess()活动管理器的 getAllRunningProcess()
  3. android.os.Process.myUid() android.os.Process.myUid()

(following suggestions in this post on SO . They all return 10464. How can I get the "full" UID? (for want of a better term, I'm not sure what the u0_a464 version of the UID should be called, as compared to the 10464 version)). (遵循这篇关于 SO 的帖子中的建议。它们都返回 10464。我怎样才能获得“完整的”UID?(为了更好的术语,我不确定应该调用 UID 的 u0_a464 版本,相比之下到 10464 版本))。

Even if we can programmatically use adb shell ps I think it may not be a good way, as adb needs developer mode to be enabled.即使我们可以以编程方式使用adb shell ps我认为这可能不是一个好方法,因为 adb 需要启用开发人员模式。

You need to use the geteuid(2) and getpwuid(3) to retrieve the data, as the JVM does not expose it.您需要使用 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);
}

Full working project: https://gitlab.com/hackintosh5/getuser完整的工作项目: https : //gitlab.com/hackintosh5/getuser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM