简体   繁体   中英

Running root commands on Android device

I want to run some commands that require root privileges on an Android device. The commands should be executed using java code, so the application itself should be able to run these commands, without the need of connecting the device to a pc and running the commands using the shell.

I have used su and then the command I want to run (chmod 666). chmod 666 needs a rooted device to run properly, so I attempt to gain super user privileges first using su , then I tried to run it. I placed the java code inside the onCreate method. Here is my code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      String[] cmds = {"cd /dev/input" ,"chmod 666"};
        try {
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            for (String tmpCmd : cmds) {
                os.writeBytes(tmpCmd + "\n");
            }
            os.writeBytes("exit\n");
            os.flush();
        }
        catch (IOException e){
            e.printStackTrace();
        }

         setContentView(R.layout.activity_main__interface);
    }

However, when running this application I get this error:

11-20 12:32:14.120    3265-3265/com.project.android.test E/cutils﹕ to chown(/mnt/shell/emulated/0, 0, 0)
11-20 12:32:14.120    3265-3265/com.project.android.test E/cutils﹕ to chown(/mnt/shell/emulated/obb, 0, 0)
11-20 12:32:14.120    3265-3265/com.project.android.test E/cutils﹕ to chown(/storage/emulated/0/Android, 0, 0)
11-20 12:32:14.120    3265-3265/com.project.android.test E/cutils﹕ to chown(/storage/emulated/0/Android/obb, 0, 0)
11-20 12:32:14.346    3265-3265/com.project.android.test E/linker﹕ load_library(linker.cpp:761): library "libmaliinstr.so" not found
11-20 12:32:14.348    3265-3265/com.project.android.test E/﹕ appName=com.project.android.test, acAppName=com.android.cts.openglperf
11-20 12:32:14.348    3265-3265/com.project.android.test E/﹕ 0
11-20 12:32:14.348    3265-3265/com.project.android.test E/﹕ appName=com.project.android.test, acAppName=com.android.browser
11-20 12:32:14.348    3265-3265/com.project.android.test E/﹕ 0

I tried to search about chown , and I found that it is related to changing the ownership of a file as stated here,

http://www.cyberciti.biz/faq/how-to-use-chmod-and-chown-command/

Which I think is caused by trying to run su . I have removed the command array, only kept su, and ran the application again, but I got an error related to chown again. So, I think there is a problem running su , but I can't figure it out.

When I searched on SO on how to run su, I found the same method I used which relies on getRunTime.exec .

I've once read that su needs a rooted device to run properly, but I'm not sure about this, as I've only seen that in few resources, is it right?

Can anyone please tell me what is the problem in the code and how to solve it?

Any help is appreciated.

EDIT : One line of the error log I provided says the following, "load_library(linker.cpp:761): library "libmaliinstr.so" not found"

I searched about it and I found that some phones miss this library like Huawei. My device is Huawei, so it causes the library related error as mentioned here:

Library "libmaliinstr.so" not found

But I've read that this library doesn't affect the running of apps, but does it affect su ?

You code to execute su command is fine. The problem is due to hardware acceleration library missing when create the runtime. You may try the same "su" command in the ADB shell and it should works.

Add the following line in your manifest.xml to turn off hardware acceleration should fix this problem:

<activity android:hardwareAccelerated="false">

Please try this code, i run okay on android 5.1:

private void prepareKvmKernelModule(){
    try{
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
        outputStream.writeBytes("insmod /lib/modules/kvm.ko\n");
        outputStream.flush();
        outputStream.writeBytes("insmod /lib/modules/kvm-intel.ko\n");
        outputStream.flush();
        outputStream.writeBytes("chmod 777 /dev/kvm\n");
        outputStream.flush();
        outputStream.writeBytes("exit\n");
        outputStream.flush();
        p.waitFor();
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

Try this

String[] deviceCommands = {"su", "cd /dev/input", "chmod 666"};
try {
        Process process = Runtime.getRuntime().exec(deviceCommands);
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "error!", Toast.LENGTH_SHORT).show();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM