The device policy manager API docs and the android 5.0 overview both mention something about a device owner app. How can I setup my app as a device owner?
Edit: Is there any other ways than rooting and NFC if available please share.
The device policy manager API docs and the android 5.0 overview both mention something about a device owner app. How can I setup my app as a device owner?
Edit: Is there any other ways than rooting and NFC if available please share.
There's actually a way other than NFC and rooting to set an application as a device owner app. You could use the dpm command line tool from an adb shell
.
Usage :
usage: dpm [subcommand] [options] usage: dpm set-device-owner usage: dpm set-profile-owner 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.
UPDATE : The dpm
utility is really simple actually. Its goal is to create a new file called device_owner.xml
under /data/system/device_owner.xml
that references the Device/Profile owner apps.
The Android platform is then reading this file to check which application is considered as a Device Owner or Profile Owner App.
On a rooted device, you could indeed create this file by yourself, but since the dpm
tool is doing it, you'd better use it (DRY principle) :
For example via a Runtime.exec()
command:
Runtime.getRuntime().exec("dpm set-device-owner com.foo.deviceowner/.DeviceAdminRcvr");
Also notice that this tool is working only if no account is set for the user (make sure no account is set in Settings > Accounts) before its use.
Source and more information at Android shell command tool : Device Policy Manager
If you're root on your device, you can follow this method to become device owner.
First, create a file device_owner.xml
with following content:
Now do the following steps
adb push device_owner.xml /sdcard/
adb shell
su
cp /sdcard/device_owner.xml /data/system/
cd /data/system/
chown system:system device_owner.xml
reboot
Note : Before rebooting device, make sure that you installed the application, which you are trying to make device owner. If you will not do, you will get boot animation for infinite time.
Update:
On my Android 7.1.2 set-top box (AOSF and rooted), I found a couple things that have evolved over time.
exec("dpm set-device-owner ...")
throws and exception unless
is declared in the AndroidManifest.xml. But that brings other issues, more about that here./data/system/device_policy.xml
doesn't appear anymore. Instead, it's now /data/system/device_policy_2.xml
and the schema is slightly different. Running dpm set-device-owner com.myDomain.myPackage/.myComponent
through an adb shell generates the file as:
You can also use reflexivity, by calling the DevicePolicyManager
method called setProfileOwner
which was hidden in the SDK Documentation.
Don't forget to cancel it otherwise you'll have some conflicts with the Google Play ;)
Just tried, and the dpm
command requires root privilege on real devices(Samsung T550 for example), otherwise it will fail with SecurityException
. adb shell
only grants root on android emulators. So you will have to root the device first.