您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Android如何判斷是否Root”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“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”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。