目前來說Android平台並沒有提供能夠root檢查的工具。但是我們可以通過兩種方式來判斷
- 手機里面是否有su文件
- 這個su文件是不是能夠執行
但是這兩種檢查方式都存在缺點。
第一種存在誤測和漏測的情況,比如su沒有放到常規路徑下,就容易漏掉,但是這種情況是有辦法盡量規避(或者說減小誤差)的,比喻運行which檢查,或者遍歷shell中所有的環境變量的PATH;還有一種情況是手機沒有root但是存在su文件,這種情況一般出現在手機曾經被root過,但是又進行了系統還原操作。
而第二種,有可能檢查不准確,或者會有彈窗提示用戶是否要授予root權限。
一、 檢查手機是否存在su文件
private static boolean checkSuFile() { Process process = null; try { // /system/xbin/which 或者 /system/bin/which process = Runtime.getRuntime().exec(new String[]{"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(); } } private static File checkRootFile() { File file = null; String[] paths = {"/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) { file = new File(path); if (file.exists()) return file; } return file; }
二、檢查su文件是否可運行
先看一種基於Linux的文件權限檢查,看文件是否有可執行權限,這種只能檢查這個文件是不是個可執行文件,比如沒有root的華為Meta9手機 adb shell
下執行效果如下:
shell@hwmt7:/system/bin $ ls -l su
-rwxr-xr-x root shell 71428 2016-08-26 18:40 su
上面的x表示有可執行權限。雖然如此,但是在華為Meta9上,adb shell中運行su還是會報錯:error: only position independent executables (PIE) are supported.
並不知道為啥。java檢查代碼如下:
/** * Author: liuqiang * Time: 2017-08-18 14:54 * Description:基於Linux的權限檢查 * 檢查su文件是否有x或者s權限 * @param filePath su 文件的路徑,比如/system/bin/su 或者/system/xbin/su */ private static boolean isCanExecute(String filePath) { java.lang.Process process = null; try { process = Runtime.getRuntime().exec("ls -l " + filePath); BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String str = in.readLine(); if (str != null && str.length() >= 4) { char flag = str.charAt(3); if (flag == 's' || flag == 'x') { Runtime.getRuntime().exec("su "); return true; } } } catch (IOException e) { e.printStackTrace(); } finally { if (process != null) { process.destroy(); } } return false; }
還有一種方式就是嘗試執行一下這個su文件,如果能執行成功,說明確實root了,如果執行不成功,那么理論上來說沒有root。但是這個沒有root只是理論上的,比如上這個手機安裝了root授權管理軟件,一旦請求root權限,就會彈窗提示用戶是否給其授權。如果用戶點擊拒絕授權,那么運行到的效果,判斷出來還是為非root。一般這樣的root授權管理apk有:/system/app/SuperSU/SuperSU.apk
等。
效果圖:

Android中java測試代碼如下:
/** * Author: liuqiang * Time: 2017-08-17 18:57 * Description: 這種方式會彈窗,如果用戶點擊拒絕授權那么判斷依然是沒有root */ private static boolean checkRootExecutable() { Process process = null; DataOutputStream os = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes("exit\n"); os.flush(); int exitValue = process.waitFor(); if (exitValue == 0) { return true; } else { return false; } } catch (Exception e) { Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: " + e.getMessage()); return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { e.printStackTrace(); } } }
這里需要注意的是int exitValue = process.waitFor();
這行代碼。判斷exitValue結果,當是0時,代表該智能設備具備最高權限。當exitValue結果不是0時,有兩種情況,要么是這台設備沒有被root,要么是當前應用沒有root權限。所以它的返回值無法精確判定。kingRoot在設備root但是沒有授權給應用時,返回值是固定的為0;但是SuperSU.apk,如果在拒絕后發揮值為1。
三、檢查手機是否存在已知的root管理軟件
像什么kingroot、SuperSU 、360Root、Root精靈、等apk是否存在。
綜上來說沒好的辦法來百分百確定這個手機是否root
from : https://www.jianshu.com/p/f9f39704e30c