2

I am try to kill another application using forceStopPackage(). But my application shows runtime error.

Error :

java.lang.SecurityException: Permission Denial: forceStopPackage() from pid=10377, uid=10200 requires android.permission.FORCE_STOP_PACKAGES

In my manifest file i added the following permissions.

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

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

Code :

ActivityManager am = (ActivityManager) 
         context.getSystemService(Context.ACTIVITY_SERVICE);
Method forceStopPackage = am.getClass().
          getDeclaredMethod("forceStopPackage", String.class);  
    forceStopPackage.setAccessible(true);  
    forceStopPackage.invoke(am, packageName);

How to solve the problem.

Prasanth S
  • 3,725
  • 9
  • 43
  • 75

2 Answers2

7

I know this is ab old thread but i wanted to make sure engineers visiting this thread has latest information.

android.permission.FORCE_STOP_PACKAGES is signature | priviliged permission hence it needs to go in to system partition under priv-app. So whoever wants to have this permission in their app have to work with respective OEM to pre-load them on to priv-app.

Starting from Android O: This permission along with App name needs to be declared in one of the permission xml

privapp-permissions-platform.xml – AOSP Apps privapp-permissions-google.xml – Google Apps privapp-permissions-.xml – OEM/ODM, OEM Associated and others

<privapp-permissions package="com.abc.android.xyz">
<permission name="android.permission.FORCE_STOP_PACKAGES"/>
</privapp-permissions> 
siva
  • 328
  • 5
  • 7
  • Thanks so much! Do you know if since Android O, the entry inside privapp-permissions-xx.xml is enough to get the permission granted, or is placing the app into /system/priv-app/ still required? – Martin L. Aug 21 '17 at 14:24
  • 1
    privapp-permission-xxx.xml is applicable for those apps that gets into /system/priv-app/. So it is not enough to get permission placed in \etc\permission\ folder For more information : https://source.android.com/devices/tech/config/perms-whitelist – siva Sep 06 '17 at 00:49
  • @siva `So whoever wants to have this permission in their app have to work with respective OEM to pre-load them on to priv-app` is not true if you are working on creating your own ROM, you can bundle the application inside `/system/apps` and your app will get the system permissions. Its a different ballgame if you want to get your application to the whole world, then your statement is true. – Nikhil Kumar Sep 20 '17 at 11:37
  • @Nikhil Kumar Private ROM is irrelevant for this discussion IMO and not helpful for the App developers targeting main stream. You can do N number of things with own ROM literally slay all Android O restrictions. Also Apps in /system/app will not have Priviliged permission but only System permissions. – siva Sep 28 '17 at 19:06
5

You have to add the permission in the AndroidManifest.xml like below.

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

Make sure only if your app is signed with system key os will allow to stop other apps.

Mahesh
  • 51
  • 1
  • 2