开发者

Obtain the Linux UID of an Android App

开发者 https://www.devze.com 2023-03-08 02:09 出处:网络
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

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 ret开发者_如何学Gorieve this UID?


adb shell dumpsys package com.example.myapp | grep userId=


Use PackageManager and getApplicationInfo().


  • 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.


Use android.os.Process.myUid() to get the calling apps UID directly.

Using the PackageManager is not necessary to find the own UID.


PackageManager packageManager = getPackageManager();
try {
    applicationId = String.valueOf(packageManager.getApplicationInfo("com.example.app", PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号