Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android M reflection method freeStorageAndNotify exception

I'm using reflection method freeStorageAndNotify:

Method freeStorageAndNotify = null;
freeStorageAndNotify = service.packageManager.getClass().getMethod(
                "freeStorageAndNotify", long.class, IPackageDataObserver.class);
freeStorageAndNotify.invoke(PackageManager.class, maxCache + freeSpace, packageDataObserver);

This causes InvocationTargetException:

java.lang.SecurityException: Neither user 10199 nor current process has android.permission.CLEAR_APP_CACHE.

Some points: - I already have android.permission.CLEAR_APP_CACHE - This happens only in android "M" Version (Flashed the preview sdk from developer site)

I know this is a hack, and google doesn't bring some official API for that, But there are so many cleaning apps which cleans all the device cache in one click, so if someone know how to bypass this issue with another workaround i'll be happy to see that.

Thanks very much for the help

like image 760
Udi Oshi Avatar asked Aug 13 '15 13:08

Udi Oshi


2 Answers

There was a bug raised on Android 5 regarding how any app can wipe out all cache files with a regular permission, but cannot wipe out one package's cache files except with a signature-level permission. It's details where

PackageManager has a deleteApplicationCacheFiles() to delete the cache from one package. This method is hidden from the SDK, and it requires DELETE_CACHE_FILES, a signature-level permission.

PackageManager also has a freeStorageAndNotify() method, to delete cache files from all packages. This method is hidden from the SDK, and it requires the CLEAR_APP_CACHE permission, which is merely flagged as "dangerous".

It was proposed to either that DELETE_CACHE_FILES should have its level relaxed, CLEAR_APP_CACHE should have its level raised.

A framework engineer responded

Note that freeStorageAndNotify's purpose is not to wipe out all cache files, but to free up X amount of space, for example by play store before it tries to download and install an app. So there are reasons to use it that work well with the system, but no reason for an app to use the method that just blindly erases all cache files for a single app (that is just there for the Settings app UI).

If indeed it is not an app error i.e. you haven't messed up the permissions and it works on Marshmallow / 6 / api 23 and not others that could only mean it became a signature level permission as well, like DELETE_CACHE_FILES.

A signature|system permission, meaning that it can only be held by apps that are signed with the firmware's signing key or are installed on the system partition (e.g., by a rooted device user). As described in this answer.

This would make sense, considering their intended use / their vision (no reason for an app to use the method that just blindly erases all cache files for a single app). It may have even been restricted as a result of that bug. When Android 6's code will come out we will know better (current available is 5.1.1 - link to PackageManager's freeStorageAndNotify).

like image 103
Laurentiu L. Avatar answered Oct 19 '22 19:10

Laurentiu L.


Refer to these pages: permissions by protection level and protection level definitions.

android.permission.CLEAR_APP_CACHE

This falls under the protection level "signature|privileged" which means that only same-signature or privileged apps (system signed basically) can have this permission.

Also you should check out the Behavior Changes in general.

like image 43
Gergely Kőrössy Avatar answered Oct 19 '22 18:10

Gergely Kőrössy