Setting up an Android Cross Compiling Environment with the NDK

Setting up an Android cross compiling environment on Debian 6.0 using the Android NDK

The NDK provides a prebuilt toolchain that can be used to cross compile applications for Android. This article details building relatively simple command line applications, so there is no need for the Android SDK or Android source.

Run the build/tools/make-standalone-toolchain.sh. Additional documentation can be found in /docs/STANDALONE-TOOLCHAIN.HTML

$ ./make-standalone-toolchain.sh --platform=android-9 --install-dir=/home/morrispj/android/lib --ndk-dir=/home/morrispj/android/android-ndk-r8d

I also needed to install ia32-libs on my Debian 6.0 x86_64 machine. Without it, running the android gcc compiler threw an error:

$ ./arm-linux-androideabi-gcc -v 
bash: ./arm-linux-androideabi-gcc: No such file or directory

Test the compiler

I used the following hello world program in C.

#include <stdio.h>

int main(void) {
    printf("Hello World!\n");

    return 0;
}

And cross-compiled it with the following command:

$ ~/android/lib/bin/arm-linux-androideabi-gcc -Wall hello.c -o hello

I copied the resulting hello binary to my device was able to execute it. Note that Android prevents you from setting the executable permission on files in /sdcard, so you will have to place it elsewhere.

Conclusion

This article details the setup of a simple cross-compiling environment for Android. Future articles will describe the steps for compiling actual command line tools, which is often more complicated than just ./configure && make && make install