I’m building an app which will be set as a device-owner of the tablet. The tablet is a custom kiosk type device. To set the app as a device owner, I’ve done the following:
- Added permissions :
<uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
- Added Config :
<receiver
android:name=".utils.MyDeviceAdminReceiver"
android:exported="true"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/policies" />
</receiver>
- Created policy file :
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
<reset-password />
</uses-policies>
</device-admin>
- And finally, added an empty device admin receiver:
package com.homeapp.utils
import android.app.admin.DeviceAdminReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
class MyDeviceAdminReceiver : DeviceAdminReceiver() {}
And after building the app via android studio, I set the app to device owner using this command:
adb shell dpm set-device-owner com.homeapp/com.homeapp.utils.MyDeviceAdminReceiver
And also recieved a success:
Success: Device owner set to package com.homeapp/com.homeapp.utils.MyDeviceAdminReceiver
Active admin set to component com.homeapp/com.homeapp.utils.MyDeviceAdminReceiver
After home app has been set, now when I try to build the app from android studio, it fails with this error :
Couldn't terminate previous instance of app
I found this thread but the solutions mentioned on it didnt work:
https://issuetracker.google.com/issues/181004316
How to resolve this?
2
Answers
Since what i have understood from your question, you can try-
Remove Device owner status by using
adb shell dpm remove-active-admin com.homeapp/.utils.MyDeviceAdminReceiver
update the app after that reassign the device owner status by
adb shell dpm set-device-owner com.homeapp/.utils.MyDeviceAdminReceiver
Reason of error:- when app get device owner status it gets elevated previlage and prevents android studio to uninstall or update application.
The error "Couldn’t terminate the previous instance of the app" typically happens when the Android Studio cannot properly stop the app that’s already set as a device owner. Since you’re setting the app as a device owner, it has certain system-level restrictions that prevent regular uninstall or termination from the device.
Remove the Device Owner: Before you can re-build or re-install the app, you must remove the device owner status. You can do this using the following command:
Rebuild the App: After removing the device owner, rebuild the app in Android Studio.
Re-set Device Owner: Once the app is built and the installation succeeds, re-set it as the device owner using the command:
Ensure that this step is done after the app is installed and not while it’s being updated during a build, as the device owner status cannot be modified in the middle of an installation process.
Check ADB Debugging Settings: Ensure that ADB debugging and USB debugging are correctly enabled on your device, as this can sometimes interfere with commands.
Check for Emulator-Specific Issues: If you’re working with an emulator, the emulator may not correctly handle device owner status in some cases. Try using a physical device to see if the issue persists.