7

I'm developing system application which requires special permissions. For some reason I can't get permission CLEAR_APP_USER_DATA, but I can use INSTALL_PACKAGES, DELETE_PACKAGES and others. What might cause this?

Manifest:

uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"/>
uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
uses-permission android:name="android.permission.DELETE_PACKAGES"/>
uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

I removed '<', because it wouldn't show the rest of the lines.

My code that uses permission:

String apkPackage = intent.getExtras().getString(context.getString(R.string.key_package));
            if (apkPackage != null) {
                PackageManager pm = context.getPackageManager();
                Class<?>[] types = new Class[] {String.class, IPackageDataObserver.class};
                try {
                    Method methodClearUserData = pm.getClass().getMethod("clearApplicationUserData", types);
                    methodClearUserData.invoke(pm, new Object[] {apkPackage, null});
                    Method methodDeleteCache = pm.getClass().getMethod("deleteApplicationCacheFiles", types);
                    methodDeleteCache.invoke(pm, new Object[] {apkPackage, null});
                } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                    Log.w(TAG, "Unable to invoke clear method.", e);
                }
            } else {
                Log.w(TAG, "Provided APK package is null.");
            }

[Edit]

07-15 14:29:32.136: W/RequestReceiver(4367): Unable to invoke clear method.
07-15 14:29:32.136: W/RequestReceiver(4367): java.lang.reflect.InvocationTargetException
07-15 14:29:32.136: W/RequestReceiver(4367):    at java.lang.reflect.Method.invokeNative(Native Method)
07-15 14:29:32.136: W/RequestReceiver(4367):    at java.lang.reflect.Method.invoke(Method.java:525)
07-15 14:29:32.136: W/RequestReceiver(4367):    at com.test.appmanager.RequestReceiver.onReceive(RequestReceiver.java:114)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2558)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.app.ActivityThread.access$1500(ActivityThread.java:165)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.os.Handler.dispatchMessage(Handler.java:107)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.os.Looper.loop(Looper.java:194)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.app.ActivityThread.main(ActivityThread.java:5370)
07-15 14:29:32.136: W/RequestReceiver(4367):    at java.lang.reflect.Method.invokeNative(Native Method)
07-15 14:29:32.136: W/RequestReceiver(4367):    at java.lang.reflect.Method.invoke(Method.java:525)
07-15 14:29:32.136: W/RequestReceiver(4367):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-15 14:29:32.136: W/RequestReceiver(4367):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-15 14:29:32.136: W/RequestReceiver(4367):    at dalvik.system.NativeStart.main(Native Method)
07-15 14:29:32.136: W/RequestReceiver(4367): Caused by: java.lang.SecurityException: Neither user 10070 nor current process has android.permission.CLEAR_APP_USER_DATA.
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.os.Parcel.readException(Parcel.java:1425)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.os.Parcel.readException(Parcel.java:1379)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.content.pm.IPackageManager$Stub$Proxy.clearApplicationUserData(IPackageManager.java:2918)
07-15 14:29:32.136: W/RequestReceiver(4367):    at android.app.ApplicationPackageManager.clearApplicationUserData(ApplicationPackageManager.java:1177)
07-15 14:29:32.136: W/RequestReceiver(4367):    ... 14 more
SMGhost
  • 3,867
  • 6
  • 38
  • 68

3 Answers3

5

Error: java.lang.SecurityException: PID 9237 does not have permission android.permission.CLEAR_APP_USER_DATA to clear data of package

Step 1. Enable Developer Option

Step 2. Enable USB Debugging

Very important Step :

Step 3. Search for "Disable Permission Monitoring" and Enable it. This will help resolving the Issue with Android 8 and above .

ShyCoder
  • 51
  • 1
  • 1
4

Change the settings in the device, then it works cool.

The way how we enable developer options in the same way we will have an option(USB debugging(Security Settings)), so turn on that.

Option might be different in different devices.

Ex: Redmi Note 5 Pro - You can find in Settings --> Additional Settings --> Developer Options --> USB debugging

Karthikeya
  • 324
  • 3
  • 10
1

That permission is marked as "Not for use by third-party applications." You mentioned that you are building a "system application", does this mean you are creating an app to be bundled with a custom platform image (for your own device, custom ROM, etc.)? In order for this to be used, it would have to be a "system" application which is pre-installed on an image, is owned by the 'system' user and signed with the platform key.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • INSTALL_PACKAGES is also "Not for use by third-party applications.", but it works as well as others. This app will be bundled with custom ROM, but for now I use rooted device and move the app to system folder myself. – SMGhost Jul 15 '14 at 10:50
  • That's true, INSTALL_PACKAGES is also marked as not for use. This *generally* means that the permission shouldn't be used, but in some cases the system will actually enforce it as well. However, I just skimmed through the Android 4.4.2 sources and nothing in the framework enforces these permissions only being used by system apps. Looking closer at your code, I suspect the problem is really that you're using reflection to get access to a hidden method and the signature is not correct. `clearApplicationUserData` has 2 forms in 4.4.2: (String, IPackageDataObserver, int) and (String, int). – Larry Schiefer Jul 15 '14 at 11:15
  • Also, just copying your app to the system folder (or /system/app) doesn't give it any special permissions or capabilities. In this case it's not critical, but in other cases such as accessing hardware interfaces via a HAL, your app would have to be signed by the platform key of the ROM and marked as the 'system' user. – Larry Schiefer Jul 15 '14 at 11:16
  • I forgot to mention, I use 4.2.2 and it has only one method with signature: `public abstract void clearApplicationUserData(String packageName, IPackageDataObserver observer);` – SMGhost Jul 15 '14 at 11:24
  • I don't have that version of AOSP handy, so I can't tell you if it's doing something different. However, it's very fragile to rely on reflection to get access to APIs, I wouldn't recommend it. If you're creating a system app for a ROM, you should build it into the image and make sure the app is properly signed with the platform key, marked as the system user and also linked with the internal framework libraries so reflection is not needed. Best of luck! – Larry Schiefer Jul 15 '14 at 11:44