Erez A. Korn
Erez A. Korn

Reputation: 2758

Obtain the Linux UID of an Android App

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?

Upvotes: 37

Views: 41686

Answers (7)

JanKanis
JanKanis

Reputation: 6672

An easy way on the commandline / in shell scripts is

stat -c '%U' /data/user/0/id.of.app

Upvotes: 0

Jonas Zeiger
Jonas Zeiger

Reputation: 178

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

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

Upvotes: 11

Matt Ke
Matt Ke

Reputation: 3739

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

Upvotes: 3

Ege Kuzubasioglu
Ege Kuzubasioglu

Reputation: 6282

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

Upvotes: 4

pavan
pavan

Reputation: 131

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

Upvotes: 13

Joe Bowbeer
Joe Bowbeer

Reputation: 3841

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

Upvotes: 57

CommonsWare
CommonsWare

Reputation: 1006529

Use PackageManager and getApplicationInfo().

Upvotes: 27

Related Questions