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?
Was facing the same issue, finally found a solution.
Install Script
Uninstall Script
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.