www.pocketmagic.net/ Android C native development using the NDK under Windows
By Radu Motisan Posted on November 18th, 2010
A lot has changed since my previous article,
on Android C native development, back in 2009.
The Android NDK has been released and the JNI development is not only possible, but also easy. Have a look on
this article on the Android NDK.
The new challenge is to use the NDK to compile Native ARM-EABI executables that would run on an Android phone. I don't want to setup the complete Google Android source code as I did in my first article.
This is possible, and I will provide the steps to do it:
Step 1. Install the NDK
Step 2. Install Cygwin
(as presented in the
Android NDK Article)
While this guide is for Windows, you can easily adapt the following for a Linux box or a Mac.
Create a simple C code file, test.c
#include
int main()
{
printf("Hello Google Android world!\nwww.pocketmagic.net\n");
return 1;
exit(0);
}
Find the following locations:
NDK_ROOT (eg. D:\work_code\android\android-ndk-r4b\ )
NDK_ROOT\build\platforms\
NDK_ROOT\build\prebuilt\windows\arm-eabi-4.2.1\
You will need them.
When starting Cygwin using the Cygwin.bat BATCH File, make sure you define the PATH to the Android NDK:
set PATH=D:/work_code/android/android-sdk-windows/tools;D:/work_code/android/android-ndk-r4b;D:\work_code\android\android-ndk-r4b\build\prebuilt\windows\arm-eabi-4.2.1\bin\;$PATH;D:\work_code\android\android-ndk-r4b\build\platforms\android-5\arch-arm\usr\lib\;
Step 3. If you read my Android NDK article, you already know the NDK folder contains the ndk-build script, that is used to compile the JNI code.
We will build a similar script, to compile our test.c code file.
Let's call this file ndk-comp and place it where ndk-build is.
#!/bin/sh
OS='windows'
ANDROIDSDK='android-3'
PROGDIR=`dirname $0`
PROGDIR=`cd $PROGDIR && pwd`
ARMEABIGCC=$PROGDIR/build/prebuilt/$OS/arm-eabi-4.2.1/bin/arm-eabi-gcc
ARMEABILIB=$PROGDIR/build/platforms/$ANDROIDSDK/arch-arm/usr/lib
ARMEABIINC=$PROGDIR/build/platforms/$ANDROIDSDK/arch-arm/usr/include
ARMEABICRT=$PROGDIR/build/platforms/$ANDROIDSDK/arch-arm/usr/lib/crtbegin_dynamic.o
LINKER=/system/bin/linker
echo "GCC:"$ARMEABIGCC "LIB:"$ARMEABILIB "LINKER":$LINKER "PARAMS:"$@
$ARMEABIGCC $@ -Wl,-rpath-link=$ARMEABILIB,-dynamic-linker=$LINKER -L$ARMEABILIB $ARMEABICRT -I$ARMEABIINC -nostdlib -lc