Bug 4670 - Deployment fails when attempting to overwrite an app which is a device administrator
Summary: Deployment fails when attempting to overwrite an app which is a device admini...
Status: RESOLVED NORESPONSE
Alias: None
Product: Visual Studio Extensions
Classification: Xamarin
Component: Android ()
Version: unspecified
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Dominique Louis
URL:
Depends on:
Blocks:
 
Reported: 2012-04-26 12:31 UTC by Jack Bond
Modified: 2016-09-04 21:10 UTC (History)
10 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Xamarin log showing the failure to uninstall a device owner app during VS debug (4.12 KB, text/plain)
2016-09-04 21:08 UTC, Mark Adamson
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NORESPONSE

Description Jack Bond 2012-04-26 12:31:25 UTC
Deployment fails when attempting to overwrite an app which is a device administrator. You can work around this by going into the control panel, remove the app's privileges, and then deploy again.
Comment 1 Jonathan Pobst 2012-04-26 14:15:57 UTC
Can you elaborate on what a device administrator is?  I haven't heard of this concept.
Comment 2 Jack Bond 2012-04-26 16:26:05 UTC
If you go into the standard android "Location and Security" tab in Settings, there is a "Device Administrators" section. Administrators aren't users, but rather applications (what a horrific operating system.) with slightly elevated privileges. The following code triggers a dialog to become an administrator...

        void VerifyAdmin()
        {
            DevicePolicyManager manager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);

            ComponentName cn = new ComponentName(this, Java.Lang.Class.FromType(typeof(AdministrativeReceiver)));

            try
            {
                if (!manager.IsAdminActive(cn))
                {
                    Intent activateDeviceAdminIntent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);

                    activateDeviceAdminIntent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, cn);

                    // It is good practice to include the optional explanation text to
                    // explain to user why the application is requesting to be a device
                    // administrator. The system will display this message on the activation
                    // screen.
                    activateDeviceAdminIntent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "Do it");

                    StartActivityForResult(activateDeviceAdminIntent, REQ_ACTIVATE_DEVICE_ADMIN);
                }
            }

            catch (Exception ex)
            {
                DebugError("Click_SetPassword", ex);
            }
        }

    [BroadcastReceiver(Permission="android.permission.BIND_DEVICE_ADMIN")]
    [MetaData("android.app.device_admin", Resource = "@xml/device_admin")]
    [IntentFilter(new string[] { "android.app.action.DEVICE_ADMIN_ENABLED" })]
    public class AdministrativeReceiver : DeviceAdminReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            RollLogger.MethodStart("AdministrativeReceiver", "OnReceive");
        }
    }
Comment 3 Jonathan Pobst 2012-04-26 16:32:30 UTC
Thanks for the explanation!

What message do you get when trying to deploy?

My guess is this is by design, and there's no way android is going to let us overwrite a privileged app.  Probably the best we can do is detect the deploy error and give a helpful message explaining what has to be done manually.
Comment 4 Jack Bond 2012-04-26 17:47:02 UTC
Should have included this in the first entry, but I didn't have a device handy.

Mono.Android.PackageAlreadyExistsException: Package /data/local/tmp/my-signed.apk already exists on device.
at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSucess

The failure to deploy is definitely an issue introduced with the 4.1 release. I've been deploying/debugging over admin apps no problem with the 4.03 release.
Comment 5 Jonathan Pobst 2012-05-31 15:12:34 UTC
We always uninstall before installing a new package, and this makes me think maybe the uninstall is failing.  Unfortunately, we aren't correctly checking the return code for uninstall.

Turn on "Extension debug logging" in Tools -> Options -> Mono for Android, restart VS, and try again.

My guess is there will be some sort of uninstall failure in the monodroid.log file written to the desktop.
Comment 6 dean.ellis 2013-06-05 11:16:18 UTC
Are you able to provide the information from the monodroid.log so we can check if the uninstall is failing?
Comment 7 Dominique Louis 2013-06-05 13:53:29 UTC
Please retest with latest release and let us know if this is still and issue.
Comment 8 Jack Bond 2013-06-05 14:01:59 UTC
If you guys would provide me a single developer license, I'd be happy to retest. I don't have a license for the latest release.
Comment 9 PJ 2013-11-19 17:05:42 UTC
This bug has been in the NEEDINFO state with no changes for the last 90 days. Can we put this back into the NEW or CONFIRMED state, or are we still awaiting response?

If there is no change in the status of this bug over the next two weeks, this bug will be marked as NORESPONSE.
Comment 10 PJ 2013-12-05 18:36:09 UTC
This bug has not been changed from the NEEDINFO state since my previous comment, marking as RESOLVED NORESPONSE.

Please feel free to REOPEN this bug at any time if you are still experiencing the issue. Please add the requested information and set the bug back to the NEW (or CONFIRMED) state.
Comment 11 pat 2014-07-15 13:30:58 UTC
I am seeing this issue now. 

Visual Studio 12
Latest Xamarin
Nexus 7 Hardware
APi Level 19

"Deployment failed because of an internal error: Package /data/local/tmp/CheckSecurity.Droid-Signed.apk already exists on device."
Comment 12 Colin Phillips 2015-06-03 12:06:40 UTC
This may be by design, but I need to know how to configure the build script to remove/disable my custom device administrator before attempting to remove the application.

As it stands, I must manually disable the app as a device administrator before debugging the application every time I make a code change.
Comment 13 Mark Adamson 2016-09-04 21:08:49 UTC
Created attachment 17329 [details]
Xamarin log showing the failure to uninstall a device owner app during VS debug

As requested previously, here is a log showing that the uninstall does indeed fail. 

Also note that it is possible to update the package by dragging the created apk directly onto the emulator and that works without any problem. So I guess that is going through a more direct app-upgrade process within android rather than trying an uninstall/install process. Could Xamarin debug do the same thing? Presumably it would be faster than the current approach.
Comment 14 Mark Adamson 2016-09-04 21:10:57 UTC
As requested previously, I've attached a log to the bug showing that the uninstall does indeed fail. 

Also note that it is possible to update the package by dragging the created apk directly onto the emulator and that works without any problem. So I guess that is going through a more direct app-upgrade process within android rather than trying an uninstall/install process. Could Xamarin debug do the same thing? Presumably it would be faster than the current approach.