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.
To resolve this issue, follow these steps:
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:
adb shell dpm remove-device-owner com.homeapp/com.homeapp.utils.MyDeviceAdminReceiver
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:
adb shell dpm set-device-owner com.homeapp/com.homeapp.utils.MyDeviceAdminReceiver
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.