2

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

MAA
  • 93
  • 3
  • 13

3 Answers3

4

You can only do this if the device is rooted and your application has super user rights.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • 2
    But there are currently applications on GooglePlay not requiring root, that does the job – MAA Dec 27 '12 at 00:21
  • I don't know of any. Could you provide an example? – Ahmad Dec 27 '12 at 00:21
  • @Mylleranton: You are confusing apps that delete *all* cache files (which exist) and apps that delete *a single app's* cache files (which cannot exist outside of custom firmware or device rooting). Apps that delete all cache files are exploiting a poorly-secured area of Android, something which I am trying to get fixed. – CommonsWare Dec 27 '12 at 00:28
  • @ Mylleranton I think I found what you mean. I downloaded one of those apps. They don't clean the cache, they just start an intent and bring the user to the specific apps settings.(So he has to clear the cache manually). To start this intent you'll need `ACTION_APPLICATION_DETAILS_SETTINGS` @CommonsWare Yap you're right for that. – Ahmad Dec 27 '12 at 00:30
  • @Ahmad Hmm, the one I downloaded just had a button "clear all cache", and it did [App Cache Cleaner](https://play.google.com/store/apps/details?id=mobi.infolife.cache) – MAA Dec 27 '12 at 00:50
  • Like CommonsWare said, clearing all cache can be done by 'exploiting a poorly-secured area of Android'. But you can't delete the cache of one specific app – Ahmad Dec 27 '12 at 01:47
  • @CommonsWare how can i read user has clicked which button enable or disable in case of ACTION_APPLICATION_DETAILS_SETTINGS and return back to my previous activity – Erum Nov 27 '14 at 08:16
  • @Mylleranton u r right they are clearing cache on one button click have u got how are they doing ? – user3233280 Dec 03 '14 at 15:49
0

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.

PrashanD
  • 2,643
  • 4
  • 28
  • 58
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

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);
Atakan Yildirim
  • 684
  • 11
  • 22