Android – Working in a Shell through an Application

Original goal: Find the Google Android Java standard for accessing a shell through an application.
Result: Google’s version of Java for Android doesn’t have a standard official supported method for accessing the shell from the application.

Here are a few helpful links to illustrate Google’s views and also helpful examples of code I stumbled across. This is definitely not all inclusive, just a few random examples which helped me along the way. Feel free to contribute other helpful code examples in the comments!

Google Developer Comments regarding Shell Access:
Android Developer stating su is no longer supported
Android Developer stating shell commands are no longer supported

Very helpful open source toolset designed by Stericson to provide basic root commands for any Android Application:
RootTools – gives Rooted developers easy access to common rooted tools

Shell Access from Android Application Code Examples
Example how to request root access in your application
Example using su shell to execute reboot
Complex example with lots of code for accessing the shell through an application
Short and simple code snippet for sending a command to an su shell:

    /** Low-level code for pushing a query command through SU */
        public int superUserCommandWithByteResult(String theCommand) throws IOException {
                Process p = Runtime.getRuntime().exec("su");
            DataOutputStream w =new DataOutputStream(p.getOutputStream());
            DataInputStream r = new DataInputStream(p.getInputStream());
            int resultByte = r.readByte();
            // w.writeBytes("exit\n");
            // w.flush();
            return resultByte;

My application WiMAX Keys Check – MarketGithub – uses root shell access two times:
Simple Method to Verify Root Access:

    private boolean canSU() {
        Process process = null;
        int exitValue = -1;
        try {
            process = Runtime.getRuntime().exec("su");
            DataOutputStream toProcess = new DataOutputStream(process.getOutputStream());
            toProcess.writeBytes("exec id\n");
            exitValue = process.waitFor();
        } catch (Exception e) {
            exitValue = -1;
        return exitValue == 0;

Simple Method to Read in Raw Partition Data:

    private void setWimaxPhone() {
        //grep supersonic or speedy /system/build.prop
        try {
            File file = new File("/system/build.prop");
            BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line = data.readLine();
            while(line != null) {
                if(line.contains("supersonic")) {
                    tracker.trackEvent(GAE_WIMAX_CHECK, PHONE_EVO, null, 0);
                    wimaxPhone = PHONE_EVO;
                } else if(line.contains("speedy")) {
                    tracker.trackEvent(GAE_WIMAX_CHECK, PHONE_SHIFT, null, 0);
                    wimaxPhone = PHONE_SHIFT;
                line = data.readLine();
        } catch (Exception e) {
            wimaxPhone = null;

Leave a Reply

Your email address will not be published. Required fields are marked *