0

I am going to create application and want to protect in case of jailbreak. Is there any process to detect whether device is rooted or not so that we can make application stop working when device is detected as rooted.

Any help will be appreciated.

Thanks in advance.

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67

2 Answers2

2

"jailBreaked means Rooted"

Here is a class that will check for Root one of three ways.

public class Root {

    private static String LOG_TAG = Root.class.getName();

    public boolean isDeviceRooted() {
        if (checkRootMethod1()){return true;}
        if (checkRootMethod2()){return true;}
        if (checkRootMethod3()){return true;}
        return false;
    }

    public boolean checkRootMethod1(){
        String buildTags = android.os.Build.TAGS;

        if (buildTags != null && buildTags.contains("test-keys")) {
            return true;
        }
        return false;
    }

    public boolean checkRootMethod2(){
        try {
            File file = new File("/system/app/Superuser.apk");
            if (file.exists()) {
                return true;
            }
        } catch (Exception e) { }

        return false;
    }

    public boolean checkRootMethod3() {
        if (new ExecShell().executeCommand(SHELL_CMD.check_su_binary) != null){
            return true;
        }else{
            return false;
        }
    }
}



public class ExecShell {

    private static String LOG_TAG = ExecShell.class.getName();

    public static enum SHELL_CMD {
        check_su_binary(new String[] {"/system/xbin/which","su"}),
        ;

        String[] command;

        SHELL_CMD(String[] command){
            this.command = command;
        }
    }

    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;
            //e.printStackTrace();
        }

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(localProcess.getOutputStream()));
        BufferedReader in = new BufferedReader(new InputStreamReader(localProcess.getInputStream()));

        try {
            while ((line = in.readLine()) != null) {
                Log.d(LOG_TAG, "--> Line received: " + line);
                fullResponse.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d(LOG_TAG, "--> Full response was: " + fullResponse);

        return fullResponse;
    }

}
Nimit
  • 1,714
  • 3
  • 22
  • 33
  • but I have read that "A rooted device may be modified in any way" so do you think that the result of this code will always correct. for reference please check answer given by "Longpoke" http://stackoverflow.com/questions/7625205/how-to-protect-media-content-video-audio-on-android-from-being-saved-redistri – Jaiprakash Soni Apr 05 '12 at 12:54
  • Could you explain what checkRootMethod1() is actually doing? What is the "test-keys" build tag, and what does it have to do with root? – thisiscrazy4 Sep 15 '13 at 03:45
  • WARNING! checkRootMethod3 is wrong. you must check for /su in the output. on S7 it outputs 1 and return becomes true – kakopappa Oct 26 '16 at 11:14
0

First of all, there are no "JailBreaks" in Android. If you want to check whether a device is rooted you can use the search of stackoverflow for a suitable solution.

Community
  • 1
  • 1
Ben Weiss
  • 17,182
  • 6
  • 67
  • 87