Is there an option to delete the cache of all Apps or certain Apps in Android M ?
Seems like most ways don't work anymore in Android M.
As a reference I used this Code out of this discussion : Android: Clear Cache of All Apps?
PackageManager pm = getPackageManager();
// Get all methods on the PackageManager
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("freeStorage")) {
// Found the method I want to use
try {
long desiredFreeStorage = 8 * 1024 * 1024 * 1024; // Request for 8GB of free space
m.invoke(pm, desiredFreeStorage , null);
} catch (Exception e) {
// Method invocation failed. Could be a permission problem
}
break;
}
}
Combined with this permission
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
This Code works fine on devices lower than Android 6.0. But on devices with Android 6.0 it results in this exception
java.lang.IllegalArgumentException: Wrong number of arguments; expected 3, got 2
There is an open source cleaner on GitHub, which stopped development with this reason:
Partial incompatibility with Android 6.0 and newer
Starting with Android 6.0, CLEAR_APP_CACHE permission seems to be no longer available to regular applications and since this permission is required for cleaning of internal cache, Cache Cleaner is not able to clean internal cache on Android 6.0 and newer. However, cleaning of external cache is still supported.
Is there really no way, to delete App Cache on devices with Android M ?
How does the Google Setting App handle it ? It can't be the new method:
public abstract void freeStorage(String volumeUuid, long freeStorageSize,
IntentSender pi)
because I think it does not delete the Cache of certain apps, but of enough apps to clear the expected ram size... But the Settings app can clear the Cache of certain apps
UPDATE Like the excepted answer explains, there seems to be no way to delete tha App Cache without root on Devices with Android M and above. But I will post it here if I find a way....
adb shell pm trim-caches 10m
fails withNeither user 2000 nor current process has android.permission.CLEAR_APP_CACHE.
Looks like a bug – Morale