简体   繁体   English

Android Java 即使使用 Root 权限也不会删除文件

[英]Android Java not deleting files even with Root Access

I've been making an Android app that clears the Cache & Data of my application.我一直在制作一个清除应用程序缓存和数据的 Android 应用程序。 Don't forget, my device is rooted.别忘了,我的设备已经root了。 So why doesn't this piece of code work?那么为什么这段代码不起作用呢? It clearly has Root permissions and full paths.它显然具有 Root 权限和完整路径。 What's the problem behind this?这背后的问题是什么?

        try {


            Process p = Runtime.getRuntime().exec("su rm -r /data/user/0/com.rick.app");
            p.waitFor();
            p = Runtime.getRuntime().exec("su rm -r /data/user_de/0/com.rick.app");
            p.waitFor();
            p = Runtime.getRuntime().exec("su rm -r /storage/emulated/0/Android/data/com.rick.app");
            p.waitFor();
            p = Runtime.getRuntime().exec("su rm -r /data/data/com.rick.app");
            p.waitFor();
            p = Runtime.getRuntime().exec("su rm -r /data/app/com.rick.app");
            p.waitFor();
            p = Runtime.getRuntime().exec("su rm -r /storage/emulated/obb/com.rick.app");
            p.waitFor();
            p = Runtime.getRuntime().exec("su rm -r /storage/emulated/0/Android/data/com.rick.app/");
            p.waitFor();
            //p = Runtime.getRuntime().exec("reboot");
            //p.waitFor();

            //Runtime.getRuntime().exec("reboot");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
  1. The command is wrong you should use sudo instead of su命令错误你应该使用 sudo 而不是 su
  2. Android has a lot of limitations! Android有很多限制! by default you can't use root commands with sudo!默认情况下,您不能在 sudo 中使用 root 命令! to do it you shou *ROOT your phone要做到这一点,你应该 *ROOT 你的手机
  3. to delete and create files in android you should add specific permissions in manifest.xml file and get permission from user要在 android 中删除和创建文件,您应该在 manifest.xml 文件中添加特定权限并获得用户的权限
  4. to create, edit and remive files you should use filling methods you can find theme here要创建、编辑和删除文件,您应该使用填充方法,您可以在此处找到主题

Generally if the command will fail you wont notice it because you dont check exit code and dont redirect the process output.通常,如果命令失败,您不会注意到它,因为您不检查退出代码并且不重定向进程 output。 Consider this code using the ProcessBuilder使用ProcessBuilder考虑此代码

Process process = new ProcessBuilder().inheritIO().command("sudo rm ...").start();
process.waitFor();
int exitCode = process.exitValue();
if (exitCode != 0) {
    System.err.println("Delete failed with exit code " + exitCode);
}

This way we can figure out what exactly happens here.这样我们就可以弄清楚这里到底发生了什么。 As other pointed out java has also apis to delete files via File#delete or using nio Here is a snipped to delete it recursivly正如其他人指出的那样 java 也有通过File#delete或使用 nio 删除文件的 api 这是一个递归删除它的片段

Files.walk(Paths.get("/data/data/"))
    .map(Path::toFile)
    .sorted(Comparator.reverseOrder())
    .forEach(File::delete);

In Kotlin you can even use File#deleteRecursively在 Kotlin 你甚至可以使用 File#deleteRecursively

I just wanted to mention that you can actually get the cache directory via context.cacheDir()我只是想提一下,您实际上可以通过 context.cacheDir() 获取缓存目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM