35

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?

Erez A. Korn
  • 2,697
  • 3
  • 24
  • 32

6 Answers6

53

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

Joe Bowbeer
  • 3,574
  • 3
  • 36
  • 47
26

Use PackageManager and getApplicationInfo().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
12
  • 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.

frogatto
  • 28,539
  • 11
  • 83
  • 129
pavan
  • 121
  • 1
  • 2
8

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

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

Jonas Zeiger
  • 140
  • 2
  • 3
4
PackageManager packageManager = getPackageManager();
try {
    applicationId = String.valueOf(packageManager.getApplicationInfo("com.example.app", PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
frogatto
  • 28,539
  • 11
  • 83
  • 129
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
2

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);
Matt Ke
  • 3,599
  • 12
  • 30
  • 49