0

I am using below code snippet. It works fine for 10+ devices. But it is returning true for few non rooted devices e.g Samsung Core 2 and Intex Aqua 4g.

my code is --

package com.abc.example;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;


public class RootUtils {
    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" };
        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();
        }
    }
}
Irshad A Khan
  • 141
  • 1
  • 11
  • 1
    You only check for the presence of `su`. This does not necessarily mean that the device is rooted. – Phantômaxx May 24 '16 at 14:28
  • Check which condition return 'true' value on not rooted device (I'll guess it the third). If so, I believe it's related to a case you didn't consider in that function (for example, which exe don't exist or something like that) – Daniel May 24 '16 at 19:12
  • @Daniel I don't have that device to check or debug. and if that device doesnt have su .exe installed that method should return false. :( – Irshad A Khan May 25 '16 at 05:49
  • @Bob Maloonga, My problem is if device is not even rooted(i.e it wont have su installed) but still this method returning true value and not allowing user to use the app.... – Irshad A Khan May 25 '16 at 05:51
  • So how exactly do we supposed to help you? Do you have any non rooted device you can debug? – Daniel May 25 '16 at 05:54
  • i did debug with other non rooted device like 10+ devices. It is working fine on them. But i m facing issue in Intex Aqua and Samsung Core 2 device. – Irshad A Khan May 25 '16 at 06:01
  • As I said, having the `su` executable (which Samsung and install because they do some heavy customizations) doesn't grant that the device is rooted. So, your method to check if the device is rooted basing on the presence of the `su` executable is simply **totally wrong**. – Phantômaxx May 25 '16 at 06:35
  • Can u suggest some solution or way (other than this su check) to my problem? Any code snippet or link would be highly helpful. – Irshad A Khan May 25 '16 at 09:09

0 Answers0