You can only do this if the device is rooted and your application has super user rights.
Clear another applications cache
-
12-12-2021 - |
Question
I´ve been reading around here about cleaning another applications cahce-memory, and I´ve also tried coding my own app. The result I´ve got, is that with Androids current securitylayer, it´s not possible.
But, there is currently many cache-cleaner applications out there on the Market (Google Play)?
When I started my application which I gave the android.permission.DELETE_CACHE_FILES
permission, the LogCat printed
Not granting permission android.permission.DELETE_CACHE_FILES to package <MY_PACKAGE_NAME> (protectionLevel=3 flags=0x8be46)
After some research I found out that 3:rd party apps would not be granted permissions with protectionLevel=3
so, I encounter a java.lang.SecurityException whenever I try to delete another application cache (logically)
My question is therefor: "How is these applications on Google Play permitted and able to delete other applications cache?"
Sorry for my bad English, not a native speaker
Solution
OTHER TIPS
Let me tell you how these apps achieve this.
Some android classes have methods
which are private(@hidden)
to user by default. We cannot access them directly. but you have a better approach to use Reflection
.
Add permission.
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
And follow these answers
https://stackoverflow.com/a/17334600/3496570
https://stackoverflow.com/a/14509140/3496570
And don't forget to create a package
name android.content.pm
and add IPackageDataObserver.aidl to package . Then you are good to go.
For Android 11 and above, you can use ACTION_CLEAR_APP_CACHE. This action doesn't automatically clear app cache, but shows a system dialog and lets the user decide.
It also requires MANAGE_EXTERNAL_STORAGE permission, check the documentation.
Usage:
Android manifest:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
android:minSdkVersion="30"
tools:ignore="ScopedStorage" />
Clean all cache:
//Get the cache dialog
int requestCode = 999;
Intent intent = new Intent(StorageManager.ACTION_CLEAR_APP_CACHE);
activity.startActivityForResult(intent, requestCode);