1

im struggling when using the method to force stop another running application in my app. I already see another thread in StackOverflow that my app need signed apk to run the force stop method. However, i already build and run in my android but it still says like below: Caused by: java.lang.SecurityException: Permission Denial: forceStopPackage() from pid=xxx, uid=xxx requires android.permission.FORCE_STOP_PACKAGES This is my code:

val am = applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val forceStopPackage: Method = am.javaClass.getDeclaredMethod("forceStopPackage", String::class.java)
forceStopPackage.isAccessible = true
forceStopPackage.invoke(am, radPackage) //radPackage is active running app

Here is the option to build signed app: enter image description here

And here is my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxx.xxx">



    <permission android:name="android.permission.FORCE_STOP_PACKAGES"
        android:protectionLevel="signature"
        android:permissionGroup="android.permission-group.SYSTEM_TOOLS" />


    <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"
        tools:ignore="ProtectedPermissions" />

</manifest>

Ill try running the app in real device with version Android P (API 28).

Hendy
  • 88
  • 5
  • 1
    That's a privileged permission - check [this out](https://stackoverflow.com/questions/20717708/android-permission-denial-forcestoppackage) – Vucko Mar 30 '21 at 08:59

1 Answers1

0

Your app needs to be a priv-app to acquire the android.permission.FORCE_STOP_PACKAGES permission.

You need to have your app in /system/priv-app/com.yourapp.package/ directory to acquire the required permission.

You can use these commands for testing purpose

$ adb root
$ adb remount
$ adb shell mkdir /system/priv-app/com.yourapp.package/
$ adb push </path/to/apk> /system/priv-app/com.yourapp.package/
$ adb reboot

But you may have to work with the ODM to make your app a priv-app in actual customer scenarios.