zzz
zzz

Reputation: 517

Unable to clear App Data with ADB Espresso

Unable to clear App Data with the following method, i am using rooted device, API 19.

public static void clearAppData() {

        try {
            Process su = Runtime.getRuntime().exec("su");
            DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

            outputStream.writeBytes(pm clear com.xxx.xxx.xxx);
            outputStream.flush();

            outputStream.writeBytes("exit\n");
            outputStream.flush();
            su.wait(2000);
        } catch (Exception e) {
            Log.e("Set Time", e.getMessage());
        }
    }

I am calling the above method in my test, but it fails to clear Cache. I have done same thing for changing the device time, and it works

Calling the above method in the following test:

@Test
    public void a_test() {


        CommonUtil.clearAppData();

        CommonUtil.changetime();

        CommonUtil.pausetime(5000);

}

Upvotes: 0

Views: 813

Answers (1)

zzz
zzz

Reputation: 517

This worked with the following solution, skipped adb command and created the following method

public static void clearSharedPrefs(Context context) {
        SharedPreferences sharedPreferences = context.
                getSharedPreferences(KEY_SP_PACKAGE, Context.MODE_PRIVATE);
//  sharedPreferences.edit().remove("KEY").commit(); //Remove Key
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }

Upvotes: 1

Related Questions