vendredi 30 janvier 2015

Where is stored my system APK ?

Given a package name of a system app, how can I get its APK path ?
First, connect your device and from the platform-tools, and do a :

adb shell dumpsys > dumpsys.txt

Now, you can edit the file dumpsys.txt with your favorite editor and search for the following string :
Package [<YOUR_PACKAGE_NAME>]

For example : with the package name com.google.android.setupwizard
you’ll have :

 Package [com.google.android.setupwizard] (bf142e1):
    userId=10018 gids=[3003]
    pkg=Package{33203806 com.google.android.setupwizard}
    codePath=/system/priv-app/SetupWizard
    ...

You see that codePath is interesting here. You can now easily navigate to thiscodePath to get the name of the APK , by listing the files available :

$ adb shell
$ cd /system/priv-app/SetupWizard
$ ls 
SetupWizard.apk
arm64

To backup this APK, you can now simply run the following :

adb pull /system/priv-app/SetupWizard/SetupWizard.apk SetupWizard-backup.apk

lundi 12 janvier 2015

Android Shell command DPM : Device Policy Manager

Device Policy Manager is available through the command line tool dpm and cand be use in an ADB shell. This tool allows you to set an application as Device Owner or Profile Owner without the need to provision it through NFC. Useful when developing !
The first thing to do is to install the application as a normal one, and then set this application as device/profile owner.
Usage :
usage: dpm [subcommand] [options]
usage: dpm set-device-owner <COMPONENT>
usage: dpm set-profile-owner <COMPONENT> <USER_ID>

dpm set-device-owner: Sets the given component as active admin, and its package as device owner.
dpm set-profile-owner: Sets the given component as active admin and profile owner for an existing user.
The parameter <COMPONENT> is composed of package-name/class-name of the DeviceAdminReceiver class you implemented in your Device/Profile Owner application. It splits the String at the first / taking the part before as the package name and the part after as the
class name. If the / is immediately followed by a . then the final class name will be the concatenation of the package name with the string following the /.
com.foo.mypackage/com.foo.mypackage.MyDeviceAdminReceiver will become package=com.foo.mypackage and class=com.foo.mypackage.MyDeviceAdminReceiver.
You could shorten the component to com.foo.mypackage/.MyDeviceAdminReceiver as well.
Example :
adb shell 
dpm set-device-owner com.foo.deviceowner/.DeviceAdminRcvr
The parameter <USER_ID>is the serial number of the user. 0 is a constant for the owner of the device. For any other user, you could programmatically get the current user id with the following code :
UserManager userManager = (UserManager)getSystemService(Context.USER_SERVICE);
UserHandle me = android.os.Process.myUserHandle();
long serialNumber = userManager.getSerialNumberForUser(me);
Notice that once the Device Owner application is set, it cannot be unset with the dpm command. You’ll need to programmatically use the DevicePolicyManager.clearDeviceOwnerApp() method or factory reset your device.
UPDATE:
“Device owner can only be set on an unprovisioned device, unless it was initiated by “adb”, in which case we allow it if no account is associated with the device” says the source code. So, make sure you don’t have any account (like Gmail) associated to your current user set before using the dpm command.
sources : Dpm.java