Search code examples
androidandroid-studioadb

adb dpm set-device-owner not working when installed outside of android studio


I am having an issue with trying to set the device owner on my android device. If I install the app onto the physical device from within android studio by hitting the play button, I am able to add and remove device owner with adb shell dpm set-device-owner package/.receiver without any issues. I can do this from within android studio or via cmd.

When I install the app from using the apk and manually installed or using adb outside of android studio, I can no longer set the device owner. I always get the error Not allowed to set the device owner because there are already some accounts on the device

This is what makes no sense, as there are no accounts on the device. I can go back into android studio, install the app over the top of it and run the adb command again and it works just fine without doing anything else.

I am lost here because there error seems incorrect, but I have no idea why installing from the apk outside of android studio is not working.

Edit: Some notes

  • I am not testing with an emulator. When I say installing from android studio, I mean installing directly to the phone, not an emulator
  • I have the manifest pointing to the xml with properly defined
  • The app works fine when installing from android studio and setting device owner, I am getting all the expected permissions
  • When I install from the output apk, I am no longer able to set device owner (previous was cleared, even tried on a fresh recovery of the phone. Accounts settings is empty with no accounts listed)

Solution

  • We have finally arrived at the cause of this issue. When running an app from android studio, it gets the testOnly flag set to true, doing so allows for certain things to be allowed that normally would not pass. So up until testing from outside of android studio, we just had no idea there was a problem.

    This issue: The SIM card adds an account under com.android.sim package that does not show itself in the accounts settings. So to us the phone appeared to have no accounts because this was hidden.

    After removing the SIM card, we were finally able to set device owner.

    Thank you to everyone who gave suggestions.