Is it possible to run a native arm binary on a non-rooted android phone?

15,089

After using the toolchain included in the Android NDK to compile your binaries, it is possible to package them with a typical Android app and have them spawn as subprocesses.

You'll have to include all the necessary files within the assets folder of your application. In order to run them, you have to have the program copy them from the assets folder to a runnable location like: /data/data/com.yourdomain.yourapp/nativeFolder

You can do this like so:

private static void copyFile(String assetPath, String localPath, Context context) {
    try {
        InputStream in = context.getAssets().open(assetPath);
        FileOutputStream out = new FileOutputStream(localPath);
        int read;
        byte[] buffer = new byte[4096];
        while ((read = in.read(buffer)) > 0) {
            out.write(buffer, 0, read);
        }
        out.close();
        in.close();

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Keep in mind that the assetPath is not absolute but in respect to assets/.

IE: "assets/nativeFolder" is just "nativeFolder"

To then run your application and read its output you could do something like this:

  Process nativeApp = Runtime.getRuntime().exec("/data/data/com.yourdomain.yourapp/nativeFolder/application");


            BufferedReader reader = new BufferedReader(new InputStreamReader(nativeApp.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();

            // Waits for the command to finish.
            nativeApp.waitFor();

            String nativeOutput =  output.toString();
Share:
15,089
AnimatedRNG
Author by

AnimatedRNG

I'm an Arch Linux user. Currently working on a lot of random projects, see my Github for details. Proficient in C, C++, Python, and Java. My New Years resolution is to learn Rust and Scala.

Updated on June 11, 2022

Comments

  • AnimatedRNG
    AnimatedRNG almost 2 years

    Well, I've been diving in the murky waters of low-level Android programming (native C/C++ using the CodeSourcery toolchain). I tried out the executable on an emulator and it worked. I'd like to try it out on a real device. So I plugged in my nexus and pushed the files on to the filesystem. Then I tried to execute the binary, and I got a permission error. It really doesn't matter how I mount it, or where I send it, I'm not root and it's not letting me execute it. Is there any way to run a program like this on a non-rooted phone?

  • sarwar
    sarwar almost 13 years
    Happy to help. Would love to see google make the non-JNI side of the NDK a little easier to use.
  • speeder
    speeder about 11 years
    Hey, you mention "expanding on the above answer" and there is not "above answer" can you edit your post to add the "above answer" (maybe a deleted one) ?
  • sarwar
    sarwar about 11 years
    I guess the author deleted it, I'll remove the reference. I believe it just suggested "you should check out the NDK".
  • jpalm
    jpalm over 10 years
    Make sure you mark the new (copied) binary as executable: mBin.setExecutable(true); if you're using Java's File objects.
  • Chris Stratton
    Chris Stratton over 10 years
    Yes, Android has a chmod, but it at least used to be a simple one which only accepted octal modes, and not the textual flags available on most desktop unixes. And Android's shell error messages are notoriously unspecific - permission denied can actually be any sort of problem.