Archive for April, 2009

Running native code in Android 2

So, the previous method only really works for statically compiled programs, meaning it’s kinda useless for anything more complex, or for cross-compiling much of anything really.

Luckily, I found another page that details a better, though more time consuming way of doing things.

Compiling for Android

Basically, you’re downloading the Android source and compiling it to get a cross-compiler that links to the appropriate Android libraries, and anything else you might need.

CAVEATS:

  1. Compiling takes a VERY long time. Don’t start the compilation over SSH, because something bad will happen, and you’ll be forced to close the SSH session, which’ll bork everything. If you’re doing stuff remotely, VNC into your Linux box, open a terminal window there, and do what you need that way.
  2. I haven’t tested this on anything other than Linux.

Comments off

Running native code in Android

As everyone probably knows by now, Doom has been ported to Android.

This is exciting for two reasons,

  1. It’s Doom!
  2. It’s actually native code running with a Dalvik frontend.

Now, Dalvik doesn’t have JNI, so how can you write something in C and run it?

This guy will lead you down a link-clicking rabbit hole that’ll tell you how. The important parts are the compiler (Choose ARM GNU/Linux and IA32 GNU/Linux), and the technique of running system commands from Dalvik, which is detailed on Gimite’s page.

One note, he links to a dynamic link method of getting everything to work, which doesn’t seem to be strictly necessary. I just compiled this:

#include <stdio.h>

int main (int argc, char** argv) {
        printf("Hello world!\n");
        return 0;
}

and it wrote to stdio just fine.

The other important part is getting the native code to actually run. You can put your binaries in your assets directory, but I’m thinking that the directory is within the .apk your app gets bundled into, and I don’t think can even run anything from there. I wound up copying the binaries from the assets directory to /data/data/com.joshsera (where com.joshsera is replaced by your package name), chmodding it, and running it.

File newHello = new File("/data/data/com.joshsera/hello");
try {
        newHello.createNewFile();
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newHello));
        BufferedInputStream in = new BufferedInputStream(this.getAssets().open("hello"));
        int b;
        while ((b = in.read()) != -1) {
                out.write(b);
        }
        //
        out.flush();
        out.close();
        in.close();
        // chmod?
        this.doCommand("/system/bin/chmod", "777", "/data/data/com.joshsera/hello");
} catch (IOException ex) {

}

doCommand is where I stuck the code to run system commands.

public void doCommand(String command, String arg0, String arg1) {
        try {
                // android.os.Exec is not included in android.jar so we need to use reflection.
                Class

Related Links

Resource Links