Straightforward Android Native Executables
I spent some time trying to figure out how to build a native code "Hello, world!" program for Android and noticed that prior to the r4b release of the Android NDK, there is a lot of bad info out there on how to do this. Usually, it involves using some kind of unofficial cross-compiler, plus the entire source tree of the Android OS, and/or whatever other kludgy hacks like linking the executable statically to some kind of ARM libc. This seems bad. With the newest version of the NDK generating a proper gdbserver executable and gdb setup stubs, why anyone would want to do this by hand is beyond me.
There was a script that made sense at one point: agcc.pl (here), which essentially takes all of the Makefile fragments and command-line switches and wraps them for you, letting you treat the NDK's cross-compilers almost like a normal gcc, but this is probably still more work than it's worth.
The Easy Way
The easiest way to get the NDK to build a native executable for you is just to use
include $(BUILD_EXECUTABLE)
at the end of your Android.mk, and then build like normal.
Anything else will just cause premature baldness.
Read on to see a fully-worked example...
hello-gdbserver: A debuggable JNI example for Android
Prolog
Before I get into the rest of the post, here is a link to a .zip of the Eclipse project files if you just want to skip ahead.
The Rest Of The Post
Native software development on Android using its JNI capabilities is incredibly frustrating without proper visibility into the causes of native code errors.
Here is an example project and some background info that shows how to get gdb and gdbserver working to catch those nasty bugs. Just extract the project files into the NDK's sample directory and import the project into Eclipse.
Here were some of the search terms I tried unsuccessfully while trying find something like this:
using gdb on android
using ndk-gdb
debug jni android
hello world android native code
hello world android emulator
debugging using gdbserver android
Requirements
- A phone running Android 2.2 (Froyo) (might work on the emulator, but I didn't focus on that)
- The latest Android NDK (at least r4b)
- Lots of Patience
The NDK-GDB.TXT readme file in the NDK has a good overview of the process of getting a debuggable Android application + JNI build set up, but I would have much preferred it if Google had simply included something like a hello-gdbserver test app in the provided samples. One simple app where you could induce a crash to see what would happen, like a tutorial for smarter bug-hunting. So, I made this one.