Android判斷是否Root方法介紹

為瞭照顧那些著急的同學,先直接給出結論:

    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 hasRootPrivilege() {
        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 Build.TAGS != null && Build.TAGS.contains("test-keys") || hasRootDir;
    }

好,接下來我們來看看到底是如何得到上述的解決方案的。首先,這是既有的判斷root權限的方案,即判定兩個root權限相關文件夾是否存在,以及當前賬戶是否具備訪問其內容的權限,如果都成立,那麼就認為當前賬號具備root權限。然而,這種root方案在一些情況下不能很好地發揮作用。

/**
 * 判斷Android設備是否擁有Root權限
 */
public class RootCheck {
 
    private final static String TAG = "RootUtil";
 
    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();
            }
        }
        return false;
    }
}

然後我就找到瞭如下方案,該方案號稱是騰訊bugly的root權限判斷方案:

private static final String[] a = 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 p() {
  boolean var0 = false;
  String[] var1 = a;
  int var2 = a.length;
 
  for(int var3 = 0; var3 < var2; ++var3) {
    String var4 = var1[var3];
    if ((new File(var4)).exists()) {
      var0 = true;
      break;
    }
  }
 
  return Build.TAGS != null && Build.TAGS.contains("test-keys") || var0;
}

當然,本人生性多疑,偶像是曹操曹丞相,所以自然不能人雲亦雲,還是實際確認一下bugly實際上是否是這樣實現的,以及確保bugly在新的版本中有沒有對該方案有進一步的改進。

然後我就到bugly官網,下載瞭其最新發佈的jar包,筆者下載時最新的版本為4.4.4,然後直接解壓,然後在解壓的目錄中搜索“test-keys”內容。

grep -r test-keys "D:\迅雷下載\Bugly_v3.4.4

最後找到瞭對應的文件位置和對應方法:com\tencent\bugly\crashreport\common\info\b.class

private static final String[] a = 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 l() {
    boolean var0 = false;
    String[] var1;
    int var2 = (var1 = a).length;
 
    for(int var3 = 0; var3 < var2; ++var3) {
        String var4 = var1[var3];
        if ((new File(var4)).exists()) {
            var0 = true;
            break;
        }
    }
 
    return Build.TAGS != null && Build.TAGS.contains("test-keys") || var0;
}

然後分析一下對應變量的意思,我們就能還原出騰訊判斷Root的代碼,即我們開頭所貼出的解決方案:

    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 hasRootPrivilege() {
        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 Build.TAGS != null && Build.TAGS.contains("test-keys") || hasRootDir;
    }

到此這篇關於Android判斷是否Root方法介紹的文章就介紹到這瞭,更多相關Android判斷Root內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: