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

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-exe.c

Android.mk

Example build + install output (it really is that easy)

16 thoughts on “Straightforward Android Native Executables”

  1. I see that NDK5 now has the ability to create a standalone tool chain which can be easily used with a makefile. I’m a bit confused though why they did this vs your solution in this article for NDK4?

    Tom

    1. Beats me. I also don’t know why they haven’t included a sample project showing how to debug things, considering the amount of dev time that gets wasted on figuring this out.

  2. oh, very helpful, thank you…

    if you use NDKr5, you should edit build/core/build-binary.mk:322, not build/core/install-binary.mk:29

  3. wonderfull ! thanks. tested ok w/ ndk r6b on ubuntu/x86 (just a minor issue for me with the prototype of main()).

  4. Thank you. Worked with NDKr7 on Mac OSX Snow Leopard. I had wasted a lot of time screwing with Makefile. This ‘just works’ the first time.

  5. Great stuff !! Thanks for this post. I didn’t know compiling a native binary was that easy on Android.

  6. Dear Max,

    I have one question,

    I followed your instruction and created the executable as ‘hello-exe’.

    I can see the output over adb-shell as shown by you, but could not able to see the same output over adb device(emulator) which is running.

    1. Do we have to do so some configuration in emulator to see the output of executable? If yes please explain..

     

    Thanks,

    Akhilesh

     

     

    1. I think the difference is the executable output is on stdout which is tied to the adb-shell console. So you’re not going to see this in the graphical emulator window. adb-shell is connected to the emulator.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.