1

I am releasing a new update, that will change my app build completely from the last version. I want the the app to be fresh and delete all old unceccery cache and data for current users. How can I achieve this? what the right code to use, and where to add it exactly, I am not expert in android development, so please guide me more specifically. I checked this question, but there's no mention of what code actually works to clear data and cache.

My setup (incase needed):

// Android Studio 3.0        
compileSdkVersion 26
buildToolsVersion '27.0.3' (Gradle)
minSdkVersion 16
targetSdkVersion 26
Kash
  • 1,663
  • 3
  • 24
  • 33

2 Answers2

1

You can listen to event in a bordcast receiver class, when your application is updated & do the cleanup in onReceive of your broadcast receiver.

The following comment in the link you shared :) has all the details.

https://stackoverflow.com/a/48520332/8312634

N0000B
  • 409
  • 1
  • 7
  • 16
  • Will it work even if I didn't declare a reciver in my previous version? And whats the proper method to clear data and cache? That's my biggest concern. – Kash Sep 10 '18 at 20:47
  • I believe you'll get this event after the application is updated, not before. So you can have the cleaup code executed on that event. Also, what do you mean by data & cache? If its a file, you'll need to delete them, or if its something like sharedprefs, you can clear them like so https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear() – N0000B Sep 10 '18 at 20:49
  • The old version uses Webview the cache and data are related to the Webview. – Kash Sep 10 '18 at 20:56
  • Try this to clear cache of WebView: https://developer.android.com/reference/android/webkit/WebView#clearCache%28boolean%29 – N0000B Sep 10 '18 at 21:01
0

Adding this to my MainActivity is what worked for me to clear cache and data.

   @Override
   protected void onDestroy() {
      super.onDestroy();
      try {
         trimCache(this);
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   public static void trimCache(Context context) {
      try {
         File dir = context.getCacheDir();
         if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
         }
      } catch (Exception e) {
         // TODO: handle exception
      }
   }

   public static boolean deleteDir(File dir) {
      if (dir != null && dir.isDirectory()) {
         String[] children = dir.list();
         for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
               return false;
            }
         }
      }

      // The directory is now empty so delete it
      return dir.delete();
   }

}

Answer found here: Clear Application cache on exit in android.

Now cache is cleared every time, a user closes the app, it works for me since I don't want to keep cache from webview, for others who want to clear this on updates only, implementing this with onReceive should work.

Kash
  • 1,663
  • 3
  • 24
  • 33