1

I have a class AdminReceiver extending DeviceAdminReceiver in my app. Previously I made it profile owner via ADB shell

adb shell dpm set-active-admin com.example.myApp/com.example.myApp.AdminReceiver 
adb shell dpm set-profile-owner com.example.myApp/com.example.myApp.AdminReceiver

And everything went fine. Subsequently, I wanted to remove it executing the following instruction from within my app programmatically:

DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
    try {
        dpm.clearProfileOwnerApp(packageName);
    } catch (Exception e) {
        Log.e(TAG, "removingProfile: ", e);
    }

And I get the following exception:

java.lang.SecurityException: Admin ComponentInfo{com.example.myApp/com.example.myApp.AdminReceiver} does not own the profile

.

When I try to set again the profile owner via ADB I get the following exception

java.lang.IllegalStateException: Trying to set the profile owner, but profile owner is already set

.

It's crazy because when i try to remove the profile owner it's like it doesn't have it and if i try to make it profile owner it's like it already has!

Is there any solution without any factory reset of the device?

Twisha Kotecha
  • 1,082
  • 1
  • 4
  • 18
Rino
  • 11
  • 1
  • 2
  • adb shell dpm remove-active-admin package.name/MyDeviceAdminReceiver try this cmd and this method dpm.clearDeviceOwnerApp(context.getPackageName()); – Amit pandey Feb 18 '20 at 12:10
  • I receive "java.lang.SecurityException: Attempt to remove non-test admin ComponentInfo{com.example.myApp/com.example.myApp.AdminReceiver}" if I try remove-active-admin from shell, with the "testOnly" flag to true set in the AndroidManifest.xml. If i run "clearDeviceOwner" from within the app it says that it's not device owner. – Rino Feb 20 '20 at 13:31
  • try dpm.clearDeviceOwnerApp(this.getPackageName()); – Amit pandey Feb 21 '20 at 05:17
  • From the docs the profile owner should be the app if you don't specify the user in the shell. But something must have went wrong... For anyone else: just factory reset the device. (Ref: https://developer.android.com/studio/command-line/adb#dpm ) – JensV Apr 21 '20 at 15:09

1 Answers1

4

Was facing the same issue, finally found a solution.

Install Script

adb install app.apk
adb shell dpm set-device-owner [your.package]/.DeviceAdminReceiver

Uninstall Script

adb install -r -t app-testOnly.apk
adb shell dpm remove-active-admin [your.package]/.DeviceAdminReceiver
adb shell pm uninstall [your.package]

Note: You need to create a testOnly version of the app first. Also if the actual app was signed, the testOnly has to be signed with the same certificate.

jasxir
  • 808
  • 9
  • 18
  • I can't create testOnly version because I can't reisntall app< I have message "app is already running" – kirkadev Sep 24 '21 at 19:45