App already set as profile owner, but can't remove it anymore

5k views Asked by At

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?

1

There are 1 answers

2
jasxir On

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.