Rooting an android device will let the users customize their devices through “root access”. It also carries a risk being unusable, while it provides a lot of capabilities to improve the performance of the device. This article is about how to detect whether an android device is rooted or not.
You can create your own android application to find out whether a device is rooted or not. This Android Root Detection Techniques article gives a detailed view on which Linux commands are needed to check in order to verify root.
Here I will provide you a guide on how to execute these commands through your java code.
Execute Commands:
java.lang.Runtime provides a set of overloaded exec() methods. We can use them to execute the Linux commands on an android device.
1.Runtime.getRuntime().exec(String command);
2.Runtime.getRuntime().exec(String [] commands);
The (1) can be used to execute single commands and use the (2)if needed to execute followed by commands.
Log the Output:
The exec() method will return a Process instance. You can read the output of commands using the InputStreamReader
Process p = Runtime.getRuntime().exec(String command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb = new StringBuffer(br.readLine());
Log.d(“Output”, sb.toString());
Examples:
1. Check for Su binaries:
try {
Runtime.getRuntime().exec("/system/bin/su");
} catch (Exception e) {
Log.d("Exception",e.getMessage());
}
This way you can check for all single commands:
/system/bin/su , /system/xbin/su, /sbin/su , /system/su, /system/bin/.ext/.su , /system/usr/we-need-root/su-backup, /system/xbin/mu, busybox df.
You can verify the command is successful when no exceptions occur.
2. When executing complex commands:
ex: ls -l | awk ‘$1 ~ /^.*w.*/’|awk -F : ‘/data/’
This command checks the permissions of directory “/data”. By default it is not writable or readable. If the device is rooted directory permission can be changed. Pass this command to the exec() method as a String [] array as follows. This way you can check the permissions of other directories : /data , /system, /sys, /sbin, /etc, /proc, /dev.
try {
Runtime.getRuntime()
.exec(new String[] { "/system/bin/sh", "-c",
" ls -l | awk '$1 ~ /^.*w.*/'|awk -F : '/data/'" });
} catch (Exception e) {
Log.d("Exception", e.getMessage());
}
For these directories use following: /system/bin, /system/sbin, /system/xbin, /vendor/bin
Runtime.getRuntime()
.exec(new String[] { "/system/bin/sh", "-c",
" ls -l /system/xbin | awk '$1 ~ /^.*w.*/'" });
3. Check the existence of packages:
com.noshufou.android.su, com.thirdparty.superuser, eu.chainfire.supersu, com.koushikdutta.superuser,com.zachspong.temprootremovejb, com.ramdroid.appquarantine
<pre> Runtime.getRuntime()
.exec(new String[] { "/system/bin/sh", "-c",
" pm list packages | awk -F: '/package:com.thirdparty.superuser/'" });
Execute commands through the “adb shell”:
1. Start device or the emulator
2. Open command prompt –> run adb shell

3. Command: id
You can check the uid, If it is either 0 or contain root the device is rooted.

Command: cat /system/build.prop | grep ro.build.tags
Check the value of build tag. A non-rooted device has ‘release-keys’, while a rooted device has ‘test-keys’ for build tag.

*Note: On a non-rooted device you won’t be able to execute these commands

Thank You! 🙂
Like this:
Like Loading...