From 670bf385acaa05f34a22ddac83598d79162bf919 Mon Sep 17 00:00:00 2001 From: lijia Date: Tue, 20 Sep 2022 15:17:52 +0800 Subject: [PATCH] =?UTF-8?q?ROOT=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dahe/mylibrary/utils/RootCheck.java | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/mylibrary/src/main/java/com/dahe/mylibrary/utils/RootCheck.java b/mylibrary/src/main/java/com/dahe/mylibrary/utils/RootCheck.java index d6f26c0..b8919db 100644 --- a/mylibrary/src/main/java/com/dahe/mylibrary/utils/RootCheck.java +++ b/mylibrary/src/main/java/com/dahe/mylibrary/utils/RootCheck.java @@ -1,5 +1,6 @@ package com.dahe.mylibrary.utils; +import android.os.Build; import android.util.Log; import java.io.BufferedReader; @@ -14,38 +15,28 @@ import java.io.InputStreamReader; * @Description 判断Android设备是否拥有Root权限 */ public class RootCheck { - private final static String TAG = "RootUtil"; + + private static final String[] rootRelatedDirs = new String[]{ + "/su", "/su/bin/su", "/sbin/su", + "/data/local/xbin/su", "/data/local/bin/su", "/data/local/su", + "/system/xbin/su", + "/system/bin/su", "/system/sd/xbin/su", "/system/bin/failsafe/su", + "/system/bin/cufsdosck", "/system/xbin/cufsdosck", "/system/bin/cufsmgr", + "/system/xbin/cufsmgr", "/system/bin/cufaevdd", "/system/xbin/cufaevdd", + "/system/bin/conbb", "/system/xbin/conbb"}; public static boolean isRoot() { - String binPath = "/system/bin/su"; - String xBinPath = "/system/xbin/su"; - if (new File(binPath).exists() && isExecutable(binPath)) - return true; - if (new File(xBinPath).exists() && isExecutable(xBinPath)) - return true; - return false; - } - - private static boolean isExecutable(String filePath) { - Process p = null; - try { - p = Runtime.getRuntime().exec("ls -l " + filePath); - // 获取返回内容 - BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); - String str = in.readLine(); - Log.i(TAG, str); - if (str != null && str.length() >= 4) { - char flag = str.charAt(3); - if (flag == 's' || flag == 'x') - return true; - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (p != null) { - p.destroy(); + boolean hasRootDir = false; + String[] rootDirs; + int dirCount = (rootDirs = rootRelatedDirs).length; + for (int i = 0; i < dirCount; ++i) { + String dir = rootDirs[i]; + if ((new File(dir)).exists()) { + hasRootDir = true; + break; } } - return false; + return Build.TAGS != null && Build.TAGS.contains("test-keys") || hasRootDir; } + }