Determine if running on a rooted device

后端 未结 24 2542
無奈伤痛
無奈伤痛 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:13

    In my application I was checking if device is rooted or not by executing "su" command. But today I've removed this part of my code. Why?

    Because my application became a memory killer. How? Let me tell you my story.

    There were some complaints that my application was slowing down devices(Of course I thought that can not be true). I tried to figure out why. So I used MAT to get heap dumps and analyze, and everything seemed perfect. But after relaunching my app many times I realized that device is really getting slower and stopping my application didn't make it faster (unless I restart device). I analyzed dump files again while device is very slow. But everything was still perfect for dump file. Then I did what must be done at first. I listed processes.

    $ adb shell ps
    

    Surprize; there were many processes for my application (with my application's process tag at manifest). Some of them was zombie some of them not.

    With a sample application which has a single Activity and executes just "su" command, I realized that a zombie process is being created on every launch of application. At first these zombies allocate 0KB but than something happens and zombie processes are holding nearly same KBs as my application's main process and they became standart processes.

    There is a bug report for same issue on bugs.sun.com: http://bugs.sun.com/view_bug.do?bug_id=6474073 this explains if command is not found zombies are going to be created with exec() method. But I still don't understand why and how can they become standart processes and hold significant KBs. (This is not happening all the time)

    You can try if you want with code sample below;

    String commandToExecute = "su";
    executeShellCommand(commandToExecute);
    

    Simple command execution method;

    private boolean executeShellCommand(String command){
        Process process = null;            
        try{
            process = Runtime.getRuntime().exec(command);
            return true;
        } catch (Exception e) {
            return false;
        } finally{
            if(process != null){
                try{
                    process.destroy();
                }catch (Exception e) {
                }
            }
        }
    }
    

    To sum up; I have no advice for you to determine if device is rooted or not. But if I were you I would not use Runtime.getRuntime().exec().

    By the way; RootTools.isRootAvailable() causes same problem.

提交回复
热议问题