Handy Android code snippets

less than 1 minute read

This blog post is a memo of handy Android code snippets that I found particularly useful in programming and hacking.

Execute a shell command as root

Reference: https://blog.csdn.net/black_bird_cn/article/details/79717245

public static String execRootCmd(String cmd) {
    String result = "";
    DataOutputStream dos = null;
    DataInputStream dis = null;
    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        dis = new DataInputStream(p.getInputStream());
        dos.writeBytes(cmd + "\n");
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();
        String line = null;
        while ((line = dis.readLine()) != null) {
            result += line;
        }
        p.waitFor();
    }
    catch (Exception e) {
    }
    finally {
        if (dos != null) {
            try { dos.close(); }
            catch (IOException e) { }
        }
        if (dis != null) {
            try { dis.close(); }
            catch (IOException e) { 
        }
    }
    return result;
}

Preserve a root shell and execute commands as root

build.gradle

dependencies {
    implementation 'com.jrummyapps:android-shell:1.0.0'
}

Java

import com.jrummyapps.android.shell.CommandResult;
import com.jrummyapps.android.shell.Shell;

CommandResult result = Shell.SU.run("id");
Log.i("TAG", result.getStdout());

Leave a Comment