检查 su 是否存在,即设备是否已植根

Check su exists i.e. Device is rooted or not

这个问题可能是重复的,但我已经查看了所有答案并注意到它们将不再有效。

 private static boolean checkRootMethod1() {
    String buildTags = android.os.Build.TAGS;
    return buildTags != null && buildTags.contains("test-keys");
}

方法一失败

 private static boolean checkRootMethod2() {
    String[] paths = { "/system/app/Superuser.apk", "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/system/sd/xbin/su",
            "/system/bin/failsafe/su", "/data/local/su" };
    for (String path : paths) {
        if (new File(path).exists()) return true;
    }
    return false;
}

方法 2 也失败了,因为 "SuperSu" 不再是系统应用程序,它可以被卸载并且 su 文件现在存在于单独的文件夹中 SU/bin/su

private static boolean checkRootMethod3() {
    Process process = null;
    try {
        process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        if (in.readLine() != null) return true;
        return false;
    } catch (Throwable t) {
        return false;
    } finally {
        if (process != null) process.destroy();
    }
}

方法 3 也失败了,因为如今 android 设备上没有 "which" 文件

RootTools.isavailable();

也失败了

我的问题是,我可以通过检查 su 文件是否在 Su/bin/su 文件夹中来检测设备是否已获得 root 权限吗?这是检测 root 设备的正确方法吗?

  1. 将“/su/bin/su”添加到 checkRootMethod2() 的路径变量
  2. 关于方法三,你也应该用“/system/bin/which”
  3. 调用"which"