Run native binary executable from Android app

If you wan to run a native binary Linux executable from your Android app you can use this ready-made function. The only param is the command line to execute.


Basically this function get as param the full command line composed by the binary executable name and, if any, the command line params (for example '/system/bin/ls /sdcard'). Return the exit value number generated by executed binary and redirect all the console output of this same executable to the logcat tool.


int executeCommandLine(String commandLine)
{
     try {
            Process process = Runtime.getRuntime().exec(commandLine);            

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuffer output = new StringBuffer();
            char[] buffer = new char[4096];
            int read;

            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }

            reader.close();

            process.waitFor();

            Log.d("executeCommandLine", output.toString());

            return process.exitValue();
        } catch (IOException e) {
            throw new RuntimeException("Unable to execute '"+commandLine+"'", e);
        } catch (InterruptedException e) {
            throw new RuntimeException("Unable to execute '"+commandLine+"'", e);
        }
}

Hope this help.

Comments

Popular posts from this blog

Access GPIO from Linux user space

Launch an app from Android shell terminal

Qt 5 and " Failed to load platform plugin 'windows' " error