8

I want to programatically find in my application if the android phone is rooted. I found many links like: Determine if running on a rooted device Effective way to programatically check if I'm rooted on Android? discussing on the same topic. However, as mentioned in those links there is no one and definite way to find out. These posts are pretty old and I was wondering if there is anything better to achieve it now in recent releases?

Thanks in advance!

Community
  • 1
  • 1
Keya
  • 859
  • 3
  • 9
  • 17
  • You won't get answer on this forum with such question. You should try out some of your/gathered ideas and ask about a problem you met while implementing it. On the other hand some idea would be to have a couple of methods that try each scenario (call `su`, request superuser permission, try to write on a privileged folder...) and if any is successful then you're on a rooted device. – JScoobyCed Oct 10 '13 at 06:26
  • 1
    My main intention by posting the question was to find out if there is any thing recent added in android framework to check for root. Apparently not! I am getting the same answers as in older posts. Thanks anyways! – Keya Oct 11 '13 at 08:00

2 Answers2

20

I've wrapped this code (working ok without any other external jar/lib) from RootTools

private static boolean isRooted() {
    return findBinary("su");
}



public static boolean findBinary(String binaryName) {
    boolean found = false;
    if (!found) {
        String[] places = {"/sbin/", "/system/bin/", "/system/xbin/", "/data/local/xbin/",
                "/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/"};
        for (String where : places) {
            if ( new File( where + binaryName ).exists() ) {
                found = true;
                break;
            }
        }
    }
    return found;
}
StErMi
  • 5,389
  • 5
  • 48
  • 71
  • This provides a good long list of possible places where su can exists. Thanks! – Keya Oct 11 '13 at 08:18
  • Well, this is basically the same method like roottools is using: https://code.google.com/p/roottools/source/browse/trunk/Stable/RootTools-sdk3-generic/src/com/stericson/RootTools/internal/RootToolsInternalMethods.java#597 Even the array content is in the same order ;) – MatF Oct 11 '13 at 08:54
  • 9
    @MatF as you can see I already said in my answer. I wrapped that code FROM RootTools... – StErMi Oct 11 '13 at 09:30
5

You can use the roottools library. They offer a method to check for root access. https://code.google.com/p/roottools/

MatF
  • 1,728
  • 2
  • 14
  • 31
  • Thanks. I am aware of roottools library. It solves the purpose. Though was looking for some third-party independent solution. – Keya Oct 11 '13 at 08:02
  • @Keya did you got any third party indepent solutions or some API to detect rooting in Android phones – Learner Nov 12 '14 at 12:09
  • Hey Learner, sorry I didn't find any solution. I was looking it for a POC but then we shifted our technique. Hence didn't pursue it further. Do update if you find anything. Thanks! – Keya Nov 13 '14 at 06:21