Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if running on a rooted device

Tags:

android

root

My app has a certain piece of functionality that will only work on a device where root is available. Rather than having this feature fail when it is used (and then show an appropriate error message to the user), I'd prefer an ability to silently check if root is available first, and if not,hide the respective options in the first place.

Is there a way to do this?

like image 447
miracle2k Avatar asked Jul 09 '09 01:07

miracle2k


People also ask

How do Android apps detect root?

Most detection apps simply try to run su or perform basic checks. Root Inspector uses multiple methods of root detection. There are 15 root checks via SDK (Java) and 13 checks via NDK (Native Code). This can be an effective detection mechanism against RootCloak or RootCloak Plus as well.

Can we track a rooted phone?

Access Phone Settings From Anywhere With WebKey A while back I wrote about a security app called Mobile Defense that could track your Android if it ever got stolen. Mobile Defense only allowed for very general tracking. Now, with a rooted Android, full remote access is possible, and that's what WebKey accomplishes.


2 Answers

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

/** @author Kevin Kowalewski */ public class RootUtil {     public static boolean isDeviceRooted() {         return checkRootMethod1() || checkRootMethod2() || checkRootMethod3();     }      private static boolean checkRootMethod1() {         String buildTags = android.os.Build.TAGS;         return buildTags != null && buildTags.contains("test-keys");     }      private static boolean checkRootMethod2() {         String[] paths = { "/system/app/Superuser.apk", "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/system/sd/xbin/su",                 "/system/bin/failsafe/su", "/data/local/su", "/su/bin/su"};         for (String path : paths) {             if (new File(path).exists()) return true;         }         return false;     }      private static boolean checkRootMethod3() {         Process process = null;         try {             process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });             BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));             if (in.readLine() != null) return true;             return false;         } catch (Throwable t) {             return false;         } finally {             if (process != null) process.destroy();         }     } } 
like image 148
Kevin Parker Avatar answered Oct 19 '22 12:10

Kevin Parker


If you are already using Fabric/Firebase Crashlytics you can call

CommonUtils.isRooted(context) 

This is the current implementation of that method:

public static boolean isRooted(Context context) {     boolean isEmulator = isEmulator(context);     String buildTags = Build.TAGS;     if (!isEmulator && buildTags != null && buildTags.contains("test-keys")) {         return true;     } else {         File file = new File("/system/app/Superuser.apk");         if (file.exists()) {             return true;         } else {             file = new File("/system/xbin/su");             return !isEmulator && file.exists();         }     } }  public static boolean isEmulator(Context context) {     String androidId = Secure.getString(context.getContentResolver(), "android_id");     return "sdk".equals(Build.PRODUCT) || "google_sdk".equals(Build.PRODUCT) || androidId == null; } 
like image 42
kingston Avatar answered Oct 19 '22 12:10

kingston