Saturday, November 15, 2014

How to compile C++ Game for Andorid NDK using Mac Terminal and Ant

This is a walkthrough of building C++ application with Android ndk and ant without using Eclipse IDE

1. Download the latest Android SDK from http://dl.google.com/android/android-sdk_r23.0.2-macosx.zip

and install Java SDK SE from Oracle say jdk-7u71-macosx-x64.dmg
2. Download Android NDK from say https://dl.google.com/android/ndk/android-ndk-r9d-darwin-x86_64.tar.bz2

3. Install apache ant

Please take note that for Mac OSX Mavericks, ant is no longer in Mac, just download a binary distribution of Ant from http://ant.apache.org/bindownload.cgi . and unzip/untar it, and add its bin directory to PATH.

4. Use Mac Terminal and add path to the above three installation folders
e.g. export PATH=${PATH}:~/android-sdk-macosx/tools:~/android-sdk-macosx/platform-tools:~/android-ndk-r9d:~/apache-ant-1.9.4/bin


5. Use Terminal command android sdk and android avd to update the sdk (install sdk of minimum of API-15 (Android 4.0.x) and create a new avd)

If you use Intel HAX for fast virt mode in emulator, please set Use Host GPU to on.

6. Download some C++ game source code e.g. here http://www.apress.com/9781430258308 . Just download the Source Code from the book website and extract say chapter2.zip
If you want to support multiple platforms add this APP_ABI in HelloDroid/jni/Application.mk
APP_PLATFORM := android-16
APP_ABI := armeabi armeabi-v7a x86


7. ndk-build
cd ~/Download/HelloDroid
ndk-build clean && ndk-build


list target (assume sdk installed already in step 5 above)
android list target

update project with target 1 (assume target id 1 is already created with API-16 in step 5 above)
android update project -n com.beginndkgamecode.hellodroid -t 1 -p .

list avd (assume "NokiaX" is created in step 5 above)
android list avd

8. start emulator
emulator -avd NokiaX -scale 0.5 &

debug build
ant clean && ant debug

install apk to emulator
ant debug install

Note:
(1) use ant uninstall if need to remove
(2) use Control-fn-F12 to rotate the emulator in Mac
(3) the generated apk file is bin/com.beginndkgamecode.hellodroid-debug.apk
(4) add android:icon to AndroidManifest.xml like this to add a default icon.
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" ...

No comments: