首页 > 解决方案 > 无法使用 ADB Espresso 清除应用数据

问题描述

无法使用以下方法清除应用程序数据,我使用的是有根设备 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());
        }
    }

我在测试中调用了上述方法,但它无法清除缓存。我已经为更改设备时间做了同样的事情,并且它有效

在以下测试中调用上述方法:

@Test
    public void a_test() {


        CommonUtil.clearAppData();

        CommonUtil.changetime();

        CommonUtil.pausetime(5000);

}

标签: androidautomationandroid-espresso

解决方案


这与以下解决方案一起使用,跳过了 adb 命令并创建了以下方法

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();
    }

推荐阅读