How to delete the cache of the apps that are installed from my own app

88 Views Asked by At

I'm developing an Android TV app with Jetpack Compose and Kotlin, which access to other installed apps like Netflix, I'd like to delete the cache from the installed apps which I logged in when I log out from my app. Is there any way to handle this?

Any solution to solve this

1

There are 1 best solutions below

0
BenjyTec On

Please refer to the official documentation:

These directories include both a dedicated location for storing persistent files, and another location for storing cache data. The system prevents other apps from accessing these locations [...]

It is impossible to directly access the cache directory of another App programmatically in Android API >= 23.

What you could however do is let your App request Accessibility Provider permissions. Once the user grants your App that permission, the App can automatically open the Settings Page of the other App and simulate clicks on the buttons to clean the cache. This is what all modern Cache Cleaner Apps are doing.

See the explanation for how the SDMaid Cleaner App achieves it (GitHub):

Cache cleaning via accessibility service is the only way to clean private app caches on Android 6.0+ without root. When this feature is activated, SD Maid will open the system's settings window for each app and click the Clear cache button as fast as the system allows it.

The accessibility service tries to click "Clear cache" for you. This automation is based on recognizing the right UI elements to click. This interaction is a bit brittle as it largely depends on matching texts, which may differ between languages, devices and Android versions.

So you will have to implement some logic that uses the Accessibility Service to perform clicks on the right buttons. You can follow this codelab to get started with Accessibility Services, especially have a look at the section "Acting on behalf of the user".