首页 > 解决方案 > 如何在 Android 中使用 root 创建/删除 /data/data 目录中的文件?

问题描述

我正在开发一个 POC,我需要在其中创建并稍后删除/data/data根设备目录中的文件。我尝试以标准方式创建文件,但它PERMISSION_DENNIED按预期抛出异常。我知道这是可能的,因为 Root Explorer 应用程序可以做到。

如何通过 root 以编程方式创建/删除文件?

预先感谢!

标签: androidmobilerooted

解决方案


基于@GabeSechan 评论,我能够使用此命令实现此目的。

创建新文件:

final String[] sCommand = {"su", "-c", "touch /data/..."};
        try {
            Runtime.getRuntime().exec(sCommand);

        } catch (Exception e) {
            Log.e(TAG, "Issue occurred while creating new file " + e.getMessage());
        }
    }

并删除文件:

final String[] sCommand = {"su", "-c", "rm /data/..."};
try {
    Runtime.getRuntime().exec(sCommand);

} catch (Exception e) {
    Log.e(TAG, "Issue occurred while deleting " + e.getMessage());
}

推荐阅读