Cross-compile source for Android

1. Use the ndk-build and an


1. Linux Host (not yet tried on windows)

2. Android NDK on host machine

Let’s create a simple hello world C program in file test.c

#include <stdio.h>
#include <stdlib.h>
int main()
     printf("Hello World\n");
     return 0;

So, above program will just print “Hello World” on standard output.

Now, to compile it with NDK, we need a the will look like this.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) 
# give module name
LOCAL_MODULE    := hello_world  
# list your C files to compile
# this option will build executables instead of building library for android application.

The NDK system is only able to compile in android project hierarchy.

So, we need to create a directory structure like this

     `-- jni
     `-- libs

Now, place test.c and file in jni directory.

Follow these steps on command line:

     $ mkdir  ~/hello_world/
     $ mkdir ~/hello_world/jni
     $ mkdir ~/hello_world/libs
     $ cd ~/hello_world/jni/
     $ gedit test.c
               # create your C code in file, save and exit.
     $ gedit
               # write contents, save and exit.
     $ export PATH=$PATH:<path/to/ndk>/
     $ ndk-build

By executing “ndk-build”, ndk will compile our test.c (not static) and puts the binary in hello_world/libs/armeabi/


So, take netcat for android as example:

First, create a root directory

$ mkdir workDir

$ cd workDir

Then download the source

$ git clone

Create two directory – jni  and libs

$ mkdir jni

$ mkdir libs

move all sources to the jni folder

$ mv platform_external_netcat/* jni

Compile, then you will get the result at the libs folder

$ ndk-build



2. Use the tool-chain

although the first method is simple and straight, this time let’s try to compile it with traditional method.

I tried to write a makefile for that, but I gave up and wrote shell since I was not that good at writing makefile… T..T

export NDK=/home/jiafei427/software/android-ndk-r11c
export SYSROOT=$NDK/platforms/android-23/arch-arm64
export CC=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc
echo”remove prebuilt files…”
rm -rf *.o
rm -rf netcat

echo “$CC –sysroot=$SYSROOT -DANDROID -c -o atomicio.o atomicio.c”
$CC –sysroot=$SYSROOT -DANDROID -fPIE -c -o atomicio.o atomicio.c
echo “$CC -DANDROID -c -o netcat.o netcat.c”
$CC –sysroot=$SYSROOT -DANDROID -fPIE -c -o netcat.o netcat.c
echo “$CC -o netcat atomicio.o netcat.o”
$CC –sysroot=$SYSROOT -fPIE -pie -o netcat atomicio.o netcat.o

Run this shell will compile the sources and create netcat program.








Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s