Determine if running on a rooted device

后端 未结 24 2665
無奈伤痛
無奈伤痛 2020-11-22 06:43

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 a

24条回答
  •  暖寄归人
    2020-11-22 07:09

    Using C++ with the ndk is the best approach to detect root even if the user is using applications that hide his root such as RootCloak. I tested this code with RootCloak and I was able to detect the root even if the user is trying to hide it. So your cpp file would like:

    #include 
    #include 
    
    
    /**
     *
     * function that checks for the su binary files and operates even if 
     * root cloak is installed
     * @return integer 1: device is rooted, 0: device is not 
     *rooted
    */
    extern "C"
    JNIEXPORT int JNICALL
    
    
    Java_com_example_user_root_1native_rootFunction(JNIEnv *env,jobject thiz){
    const char *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"};
    
    int counter =0;
    while (counter<9){
        if(FILE *file = fopen(paths[counter],"r")){
            fclose(file);
            return 1;
        }
        counter++;
    }
    return 0;
    }
    

    And you will call the function from your java code as follows

    public class Root_detect {
    
    
    
       /**
        *
        * function that calls a native function to check if the device is 
        *rooted or not
        * @return boolean: true if the device is rooted, false if the 
        *device is not rooted
       */
       public boolean check_rooted(){
    
            int checker = rootFunction();
    
            if(checker==1){
               return true;
            }else {
               return false;
            }
       }
       static {
        System.loadLibrary("cpp-root-lib");//name of your cpp file
       }
    
       public native int rootFunction();
    }
    

提交回复
热议问题