Android判斷是否Root方法介紹
為了照顧那些著急的同學(xué),先直接給出結(jié)論:
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; }
好,接下來(lái)我們來(lái)看看到底是如何得到上述的解決方案的。首先,這是既有的判斷root權(quán)限的方案,即判定兩個(gè)root權(quán)限相關(guān)文件夾是否存在,以及當(dāng)前賬戶是否具備訪問(wèn)其內(nèi)容的權(quán)限,如果都成立,那么就認(rèn)為當(dāng)前賬號(hào)具備root權(quán)限。然而,這種root方案在一些情況下不能很好地發(fā)揮作用。
/** * 判斷Android設(shè)備是否擁有Root權(quán)限 */ 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); // 獲取返回內(nèi)容 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; } }
然后我就找到了如下方案,該方案號(hào)稱是騰訊bugly的root權(quán)限判斷方案:
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; }
當(dāng)然,本人生性多疑,偶像是曹操曹丞相,所以自然不能人云亦云,還是實(shí)際確認(rèn)一下bugly實(shí)際上是否是這樣實(shí)現(xiàn)的,以及確保bugly在新的版本中有沒(méi)有對(duì)該方案有進(jìn)一步的改進(jìn)。
然后我就到bugly官網(wǎng),下載了其最新發(fā)布的jar包,筆者下載時(shí)最新的版本為4.4.4,然后直接解壓,然后在解壓的目錄中搜索“test-keys”內(nèi)容。
grep -r test-keys "D:\迅雷下載\Bugly_v3.4.4
最后找到了對(duì)應(yīng)的文件位置和對(duì)應(yīng)方法: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; }
然后分析一下對(duì)應(yīng)變量的意思,我們就能還原出騰訊判斷Root的代碼,即我們開(kāi)頭所貼出的解決方案:
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; }
到此這篇關(guān)于Android判斷是否Root方法介紹的文章就介紹到這了,更多相關(guān)Android判斷Root內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)易登陸注冊(cè)邏輯的實(shí)例代碼
在android的應(yīng)用中越來(lái)越多的包含了網(wǎng)絡(luò)互動(dòng)功能,這就帶來(lái)了注冊(cè),登陸賬號(hào)功能,這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)簡(jiǎn)易登陸注冊(cè)邏輯的相關(guān)資料,需要的朋友可以參考下2021-06-06Android開(kāi)發(fā)之進(jìn)度條ProgressBar的示例代碼
本篇文章主要介紹了Android開(kāi)發(fā)之進(jìn)度條ProgressBar的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的全過(guò)程記錄
對(duì)于android軟件開(kāi)發(fā)初級(jí)學(xué)習(xí)者來(lái)說(shuō),簡(jiǎn)單的頁(yè)面跳轉(zhuǎn)是必學(xué)的,這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10Android使用友盟集成QQ、微信、微博等第三方分享與登錄方法詳解
之前的項(xiàng)目第三方分享和登錄一直都使用ShareSDK實(shí)現(xiàn)的。為了統(tǒng)一使用友盟的全家桶,所以三方分享和登錄也就選擇了友盟,這里為大家整理出詳細(xì)方法2018-03-03Android 登錄Web 時(shí)對(duì)cookie 處理
本文主要介紹 Android登錄web時(shí)對(duì)cookie的處理方法,這里cookie 的讀寫做了詳細(xì)介紹,并附有代碼進(jìn)行講解,希望能幫到有需要的同學(xué)2016-07-07使用Broadcast實(shí)現(xiàn)Android組件間的通信
這篇文章主要為大家詳細(xì)介紹了使用Broadcast實(shí)現(xiàn)Android組件間的通信,感興趣的小伙伴們可以參考一下2016-06-06Android Studio應(yīng)用開(kāi)發(fā)集成百度語(yǔ)音合成使用方法實(shí)例講解
這篇文章主要介紹了Android Studio應(yīng)用開(kāi)發(fā)集成百度語(yǔ)音合成使用方法實(shí)例講解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Android App中實(shí)現(xiàn)相冊(cè)瀑布流展示的實(shí)例分享
這篇文章主要介紹了Android App中實(shí)現(xiàn)相冊(cè)瀑布流展示的實(shí)例分享,例子中利用到了緩存LruCache類的相關(guān)算法來(lái)解決大量加載問(wèn)題,需要的朋友可以參考下2016-04-04