Created
August 20, 2014 01:19
-
-
Save booknara/ca5c7c6e5ff102a069b0 to your computer and use it in GitHub Desktop.
How to detect that a device is rooted in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.booknara.android; | |
| import java.io.BufferedReader; | |
| import java.io.BufferedWriter; | |
| import java.io.File; | |
| import java.io.InputStreamReader; | |
| import java.io.OutputStreamWriter; | |
| import java.util.ArrayList; | |
| import android.util.Log; | |
| // Reference : http://stackoverflow.com/a/8097801/2015075 | |
| public class RootUtil { | |
| private static String TAG = RootUtil.class.getName(); | |
| public boolean isDeviceRooted() { | |
| return checkRootMethod1() || checkRootMethod2() || checkRootMethod3(); | |
| } | |
| public boolean checkRootMethod1() { | |
| String buildTags = android.os.Build.TAGS; | |
| return buildTags != null && buildTags.contains("test-keys"); | |
| } | |
| public boolean checkRootMethod2() { | |
| try { | |
| File file = new File("/system/app/Superuser.apk"); | |
| return file.exists(); | |
| } catch (Exception e) {} return false; | |
| } | |
| public boolean checkRootMethod3() { | |
| return new ExecShell().executeCommand(SHELL_CMD.check_su_binary)!=null; | |
| } | |
| public enum SHELL_CMD { | |
| check_su_binary(new String[] { "/system/xbin/which", "su" }); | |
| String[] command; | |
| SHELL_CMD(String[] command) { | |
| this.command = command; | |
| } | |
| } | |
| public class ExecShell { | |
| public ArrayList<String> executeCommand(SHELL_CMD shellCmd) { | |
| String line = null; | |
| ArrayList<String> fullResponse = new ArrayList<String>(); | |
| Process localProcess = null; | |
| try { | |
| localProcess = Runtime.getRuntime().exec(shellCmd.command); | |
| } catch (Exception e) { | |
| return null; | |
| } | |
| BufferedWriter out = new BufferedWriter(new OutputStreamWriter( | |
| localProcess.getOutputStream())); | |
| BufferedReader in = new BufferedReader(new InputStreamReader( | |
| localProcess.getInputStream())); | |
| try { | |
| while ((line = in.readLine()) != null) { | |
| Log.d(TAG, "--> Line received: " + line); | |
| fullResponse.add(line); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| Log.d(TAG, "--> Full response was: " + fullResponse); | |
| return fullResponse; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
checkRootMethod3 is wrong. You have to check for /su in the output... on Samsung S7 it outputs 1 and return becomes true. Learn the hardway