Android M reflection method freeStorageAndNotify exception
Asked Answered
C

2

17

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

Crouton answered 13/8, 2015 at 13:28 Comment(4)
"I already have android.permission.CLEAR_APP_CACHE - This happens only in android "M" Version (Flashed the preview sdk from developer site)" -- it is possible that they changed the protectionLevel of this permission to be signature or system. That would explain the symptoms.Strychnine
That's a shame :) we'll just need to bypass that too. Any suggestions where to start? thanksCrouton
I checked the permissions in framework-res.apk for the 3rd M Preview, and the protection level is indeed signature|system for both CLEAR_APP_CACHE and DELETE_CACHE_FILESVarsity
#3635601 i found solution there(above link).Unlisted
M
14

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).

Miseno answered 18/8, 2015 at 11:12 Comment(2)
How other third party apps like 'Clean master' are still able to delete system cache of all installed applications by taking accessibility permission from user for 6.0 devices?Montymonument
I suppose you should ask them how they do it. That is if they are interested in revealing it. Or you could actually try and post this question.Miseno
W
3

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.

Woodenware answered 23/8, 2015 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.