Skip to content
Android library providing APIs to a Unix (root) shell
Java Shell
Branch: master
Clone or download
Latest commit 8382580 Sep 12, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
busybox Add nullability annotations Sep 12, 2019
core Add nullability annotations Sep 12, 2019
example Add nullability annotations Sep 12, 2019
gradle/wrapper Update AS Sep 10, 2019
io fix(io): Fix SuFile#getParent() when no parent available Apr 23, 2019
.gitignore Initial code Jan 19, 2018 Release 2.5.1 Sep 12, 2019
LICENSE Create LICENSE Jan 25, 2018 Update README Sep 12, 2019
build.gradle Update AS Sep 10, 2019 Update Gradle plugin and migrate to AndroidX Nov 11, 2018
gradlew Initial code Jan 19, 2018
gradlew.bat Initial code Jan 19, 2018
settings.gradle Move I/O to separate module Jan 3, 2019


An Android library that provides APIs to a Unix (root) shell.

Some poorly coded applications requests a new shell (call su, or worse su -c <commands>) for every single command, which is very inefficient. This library makes sharing a single, globally shared shell session in Android applications super easy: developers won't have to bother about concurrency issues, and with a rich selection of both synchronous and asynchronous APIs, it is much easier to create a powerful root app.

Optionally, libsu comes with a whole suite of I/O classes, re-creating classes but enhanced with root access. Without even thinking about command-lines, you can use File, RandomAccessFile, FileInputStream, and FileOutputStream equivalents on all files that are only accessible with root permissions. The I/O stream classes are carefully optimized and have very promising performance.

Also optionally, this library bundles with prebuilt busybox binaries. App developers can easily setup and create an internal busybox environment without relying on potentially flawed (or even no) external busybox.

One complex Android application using libsu for all root related operations is Magisk Manager.


Link to Changelog


android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
repositories {
    maven { url '' }
dependencies {
    def libsuVersion = '2.5.1'
    implementation "com.github.topjohnwu.libsu:core:${libsuVersion}"

    /* Optional: For using classes */
    implementation "com.github.topjohnwu.libsu:io:${libsuVersion}"

    /* Optional: For including prebuilt busybox binaries */
    implementation "com.github.topjohnwu.libsu:busybox:${libsuVersion}"

Quick Tutorial


Set configurations in your MainActivity or Application class:

static {
    /* Shell.Config methods shall be called before any shell is created
     * This is the why in this example we call it in a static block
     * The followings are some examples, check Javadoc for more details */

Shell Operations

// Run commands and get output immediately
List<String> output ="find /dev/block -iname boot").exec().getOut();

// Aside from commands, you can also load scripts from InputStream
Shell.Result result =;

// You can get more stuffs from the results
int code = result.getCode();
boolean ok = result.isSuccess();
output = result.getOut();

// Run commands and output to a specific List
List<String> logs = new ArrayList<>();"cat /cache/magisk.log").to(logs).exec();

// Run commands in the background and don't care results"setenforce 0").submit();

// Run commands in the background and get results via a callback"sleep 5", "echo hello").submit(result -> {
    /* This callback will be called on the main (UI) thread
     * after the operation is done (5 seconds after submit) */
    result.getOut();  /* Should return a list with a single string "hello" */

// Create a reactive callback List, and update the UI on each line of output
List<String> callbackList = new CallbackList<String>() {
    public void onAddElement(String s) {
        /* This callback will be called on the main (UI) thread each time
         * the list adds a new element (in this case: shell outputs a new line)*/
        uiUpdate(s);  /* Some method to update the UI */
    "for i in 1 2 3 4 5;do",
    "  echo $i"
    "  sleep 1"
    "echo 'countdown done!'").to(callbackList).submit(result -> {
        /* Some stuffs cannot be acquired from callback lists
         * e.g. return codes */

// Also get STDERR
List<String> stdout = new ArrayList<>();
List<String> stderr = new ArrayList<>();"echo hello", "echo hello >&2").to(stdout, stderr).exec();


Add com.github.topjohnwu.libsu:io as dependency to access the I/O wrapper classes:

/* Treat files that require root access just like ordinary files */
File logs ="/cache/magisk.log");
if (logs.exists()) {
    try (InputStream in = new SuFileInputStream(logs);
         OutputStream out = new SuFileOutputStream("/data/magisk.log.bak")) {
        /* All file data can be accessed by Java Streams */

        // For example, use a helper method to copy the logs
        ShellUtils.pump(in, out);
    } catch (IOException e) {


The I/O classes relies on several commandline tools. Most of the tools are availible in modern Android via toybox (Android 6+), however for compatibility and reliable/reproducible behavior (some applets included in toybox is not fully featured), it will be a good idea to have BusyBox included to the environment:

/* If you want to bundle prebuilt busybox binaries with your app,
 * add com.github.topjohnwu.libsu:busybox as a dependency, and
 * register BusyBoxInstaller as an initializer. */

/* If your app only targets Magisk users, and you are not willing to
 * increase your APK size for the busybox binaries, you can tell libsu
 * to use Magisk's internal busybox */


Initialize shells with custom Shell.Initializer, similar to what .bashrc will do:

class ExampleInitializer extends Shell.Initializer {
    public boolean onInit(Context context, Shell shell) {
        try (InputStream bashrc = context.getResources().openRawResource(R.raw.bashrc)) {
            // Load a script from raw resources
                .add(bashrc)                            /* Load a script from resources */
                .add("export ENVIRON_VAR=SOME_VALUE")   /* Run some commands */
        return true;

// Register the class as an initializer


This repo also comes with an example app (:example), check the code and play/experiment with it.

You can’t perform that action at this time.