Determining if an Android device is rooted programmatically?
Rooting detection is a cat and mouse game and it is hard to make rooting detection that will work on all devices for all cases.
See Android Root Beer https://github.com/scottyab/rootbeer for advanced root detection which also uses JNI and native CPP code compiled into .so native library.
If you need some simple and basic rooting detection check the code below:
/**
* 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;
}
Probably not always correct. Tested on ~10 devices in 2014.
How can you detect if the device is rooted in the app?
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. In practice you could look at some of the standard root builds to find features they have or characteristics you can look at... but there is no way to guarantee that whatever you do will actually detect a "rooted" device.
Related Topics
Automatically Accept All Sdk Licences
How to Create a Custom-Shaped Bitmap Marker With Android Map API V2
Synchronise Scrollview Scroll Positions - Android
Android Sqlite Database: Slow Insertion
Android How to Use Mediascannerconnection Scanfile
Imageview in Circular Through Xml
How to Find Out If the Gps of an Android Device Is Enabled
Is the Way the Firebase Database Quickstart Handles Counts Secure
How to Create an Object of an Activity in Other Class
Java.Util.Zip.Zipexception: Duplicate Entry During Packagealldebugclassesformultidex
Android Getresources().Getdrawable() Deprecated API 22
Difference Between Match_Parent and Fill_Parent
Detect Application Heap Size in Android
How to Change Theme For Alertdialog