Name: RootCommands
Owner: Guardian Project
Description: RootCommands - Library to access root commands with Java API
Created: 2016-02-22 16:34:25.0
Updated: 2017-09-15 15:24:19.0
Pushed: 2016-02-22 17:33:05.0
Size: 248
Language: Java
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
RootCommands is no longer in active development. If you like to take over the maintaining, simply fork it and implement fixes. I will only do basic maintenance like merging pull requests and releasing new versions.
This is a library to simplify the usage of root commands on the Android OS. It is a Java wrapper around native binaries shipped with every Android OS, but can also be used to package and execute your own native binaries.
libraries/RootCommands
to your project and include it in settings.gradle
(see https://github.com/dschuermann/root-commands/blob/master/settings.gradle)compile project(':libraries:RootCommands')
to your project build.gradle
. (see https://github.com/dschuermann/root-commands/blob/master/ExampleApp/build.gradle)To see RootCommands in action, compile the RootCommands Demo
project and observe the logcat output.
You can enable debug logging in RootCommands by the following line:
Commands.DEBUG = true;
This tries to find the su binary, opens a root shell and checks for root uid.
RootCommands.rootAccessGiven()) {
// your code
You can instantiate SimpleCommands with the shell commands you want to execute. This is a very basic approach of executing something on a shell.
tart root shell
l shell = Shell.startRootShell();
imple commands
leCommand command0 = new SimpleCommand("echo this is a command",
"echo this is another command");
leCommand command1 = new SimpleCommand("toolbox ls");
leCommand command2 = new SimpleCommand("ls -la /system/etc/hosts");
l.add(command0).waitForFinish();
l.add(command1).waitForFinish();
l.add(command2).waitForFinish();
d(TAG, "Output of command2: " + command2.getOutput());
d(TAG, "Exit code of command2: " + command2.getExitCode());
lose root shell
l.close();
For more complex commands you can extend the Command class to parse the output while the shell executes the command.
ate class MyCommand extends Command {
private static final String LINE = "hosts";
boolean found = false;
public MyCommand() {
super("ls -la /system/etc/");
}
public boolean isFound() {
return found;
}
@Override
public void output(int id, String line) {
if (line.contains(LINE)) {
Log.d(TAG, "Found it!");
found = true;
}
}
@Override
public void afterExecution(int id, int exitCode) {
}
ava
tart root shell
l shell = Shell.startRootShell();
ustom command classes:
mmand myCommand = new MyCommand();
l.add(myCommand).waitForFinish();
d(TAG, "myCommand.isFound(): " + myCommand.isFound());
lose root shell
l.close();
Toolbox is similar to busybox, but normally shipped on every Android OS. You can find toolbox commands on https://github.com/CyanogenMod/android_system_core/tree/ics/toolbox . This means that these commands are designed to work on every Android OS, with a working toolbox binary on it. They don't require busybox!
The Toolbox class is based on this toolbox executeable and provides some nice commands as java methods like:
l shell = Shell.startRootShell();
box tb = new Toolbox(shell);
tb.isRootAccessGiven()) {
Log.d(TAG, "Root access given!");
se {
Log.d(TAG, "No root access!");
d(TAG, tb.getFilePermissions("/system/etc/hosts"));
l.close();
Android APKs are normally not designed to include native executables. But they are designed to include native libraries for different architectures, which are deployed when the app is installed on the device. Androids mechanism will deploy the proper native library based on the architecture of the device.
This method only deploys files that are named like lib*.so
, which are included from the libs folder of your project.
We are missusing Androids library method to deploy our native executables, by renaming them after compilation, so that they are included in the apk and deployed based on the architecture.
Note: Permission and owner of deployed files: -rwxr-xr-x system system 38092 2012-09-24 19:51 libhello_world_exec.so
*
to lib*_bin.so
.ndk-build
to build executablesgradle renameExecutables
gradle build
Now that your executables are bundled, you can use our SimpleExecutableCommand
like in the following example:
leExecutableCommand execCommand = new SimpleExecutableCommand(this, "hello_world", "");
tarted as normal shell without root, but you can also start your executables on a root
hell if you need more privileges!
l shell = Shell.startShell();
l.add(execCommand).waitForFinish();
box tb = new Toolbox(shell);
tb.killAllBinary("hello_world")) {
Log.d(TAG, "Hello World daemon killed!");
se {
Log.d(TAG, "Killing failed!");
l.close();
Fork RootCommands and do a Pull Request. I will merge your changes back into the main project.
RootCommands is based on several other open source projects, thus several authors are involved: