2

I want to check if an android devices is rooted or not. If device is rooted I dont want my application to show the appropriate message to the user and the application should not work on a rooted device.

I have gone through various links and blogs which have code snipplets to check if device is rooted or not. But I also found multiple developers saying that it is not possible to programmatically check for sure if a device is rooted or no. The code snippets might not give 100% accurate results on all the devices and results might also depend on the tool used for rooting the android device.

Please let me know if there is any way to confirm for sure that the device is rooted or not programmatically.

Thanks, Sagar

Pseudonym
  • 2,052
  • 2
  • 17
  • 38
Sagar Ekbote
  • 295
  • 1
  • 3
  • 11

2 Answers2

3

I don't have enough reputation points to comment, so I have to add another answer.

The code in CodeMonkey's post works on most devices, but at least on Nexus 5 with Marshmallow it doesn't, because the which command actually works even on non-rooted devices. But because su doesn't work, it returns a non-zero exit value. This code expects an exception though, so it has to be modified like this:

private static boolean canExecuteCommand(String command) {
    try {
        int exitValue = Runtime.getRuntime().exec(command).waitFor();
        return exitValue == 0;
    } catch (Exception e) {
        return false;
    }
}
Vlad
  • 7,997
  • 3
  • 56
  • 43
Ish
  • 111
  • 1
  • 3
0

Possible duplicate of Stackoverflow.

This one has an answer

Answer on the second link. The guy tested it on about 10 devices and it worked for him.

  /**
   * Checks if the device is rooted.
   *
   * @return <code>true</code> if the device is rooted, <code>false</code> otherwise.
   */
  public static boolean isRooted() {

    // get from build info
    String buildTags = android.os.Build.TAGS;
    if (buildTags != null && buildTags.contains("test-keys")) {
      return true;
    }

    // check if /system/app/Superuser.apk is present
    try {
      File file = new File("/system/app/Superuser.apk");
      if (file.exists()) {
        return true;
      }
    } catch (Exception e1) {
      // ignore
    }

    // try executing commands
    return canExecuteCommand("/system/xbin/which su")
        || canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su");
  }

  // executes a command on the system
  private static boolean canExecuteCommand(String command) {
    boolean executedSuccesfully;
    try {
      Runtime.getRuntime().exec(command);
      executedSuccesfully = true;
    } catch (Exception e) {
      executedSuccesfully = false;
    }

    return executedSuccesfully;
  }
Community
  • 1
  • 1
CodeMonkey
  • 1,136
  • 16
  • 31
  • Hi CodeMonkey, Thanks for the link but I have gone through it also I went through this link http://stackoverflow.com/questions/3576989/how-can-you-detect-if-the-device-is-rooted-in-the-app which says "At the end of the day, you can't. A rooted device may be modified in any way, and thus can completely hide whatever it wants from you" so I am confused whether one can know programatically for sure if device is rooted or no. – Sagar Ekbote Jul 21 '14 at 15:26
  • Hi Frank, I am storing some sensitive information in encrypted form in database. But if device is rooted one can acess these db files can decrypt the data. – Sagar Ekbote Jul 21 '14 at 15:30
  • Can anyone please help me to now whether its possible to say for sure if an android device is rooted? – Sagar Ekbote Aug 01 '14 at 08:53
  • 3
    `which` will not exist on most android devices (even rooted devices). The superuser management rarely is in `/system/app` now (most are installed from the Play Store). If `Build.TAGS` contains "test-keys" it does not mean the device is rooted. TL;DR: This answer is no good. :P – Jared Rummler Dec 22 '15 at 00:49