0

i need to clear cache of an app without force closing the app. i need app to continue running. i am using espresso to test application but i need to clear cache before the app starts. is there any possible way to do it ?

public static void clearPreferences(Activity activity) {
    try {
        // clearing app data
        String packageName = activity.getPackageName();
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("pm clear "+packageName);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

this is what i have. but it closes the app and terminates test case

1 Answers1

0

check this link below it's helpful to you.

Clear Cache in Android Application programmatically

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        deleteDir(dir);
    } catch (Exception e) {}
}

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;
            }
        }
        return dir.delete();
    } else if(dir!= null && dir.isFile()) {
        return dir.delete();
    } else {
        return false;
    }
}

add permission in manifest <uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lovekush Vishwakarma
  • 3,035
  • 23
  • 25