This document describes how to run a simple 'HelloWorld' sample under NDK r4 and r5 with Windows (using Cygwin), or Linux.
Setting Up Development Environment
NOTE:
This document doesn't describe how to set up NDK. Please refer to the following website if you wish to learn more about the Android NDK, and how to download and install it:
http://developer.android.com/sdk/ndk/overview.html
Your NDK version must be r4 or r5
NDK-r4 does NOT support STL. Unfortunately, the engine depends on it, so we need to find a version that supports it. Luckily, some one has already done it. You can download it here:
http://www.crystax.net/android/ndk.php
- If you would like to build on crystax.net ndk-r4, please follow the steps 1.1.1 ~ 1.1.3
- If you would like to build cocos2d-x tests on ndk-r5, you can use the default settings in android.mk file, and just jump to 1.2
Change the cocos2dx/Android.mk
- Replace:
# it is used for ndk-r4 # if you build with nkd-r4, uncomment it # LOCAL_LDLIBS := -L$(LOCAL_PATH)/platform/third_party/android/libraries \ # -lGLESv1_CM -llog -lz \ # -lpng \ # -lxml2 \ # -ljpeg \ # -lskia # it is used for ndk-r5 # if you build with ndk-r4, comment it # because the new Windows toolchain doesn't support Cygwin's drive # mapping (i.e /cygdrive/c/ instead of C:/) LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/platform/third_party/android/libraries) \ -lGLESv1_CM -llog -lz \ -lpng \ -lxml2 \ -ljpeg \ -lskia
- with:
# it is used for ndk-r4 # if you build with nkd-r4, uncomment it LOCAL_LDLIBS := -L$(LOCAL_PATH)/platform/third_party/android/libraries \ -lGLESv1_CM -llog -lz \ -lpng \ -lxml2 \ -ljpeg \ -lskia # it is used for ndk-r5 # if you build with ndk-r4, comment it # because the new Windows toolchain doesn't support Cygwin's drive # mapping (i.e /cygdrive/c/ instead of C:/) # LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/platform/third_party/android/libraries) \ # -lGLESv1_CM -llog -lz \ # -lpng \ # -lxml2 \ # -ljpeg \ # -lskia
Change the HelloWorld/android/jni/helloworld/Android.mk
- Replace:
# it is used for ndk-r4 # if you build with nkd-r4, uncomment it # LOCAL_LDLIBS := -L$(LOCAL_PATH)/../../libs/armeabi -lcocos2d -llog -lcocosdenshion \ # -L$(LOCAL_PATH)/../../../../cocos2dx/platform/third_party/android/libraries -lcurl # it is used for ndk-r5 # if you build with ndk-r4, comment it # because the new Windows toolchain doesn't support Cygwin's drive # mapping (i.e /cygdrive/c/ instead of C:/) LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/armeabi) \ -lcocos2d -llog -lcocosdenshion \ -L$(call host-path, $(LOCAL_PATH)/../../../../cocos2dx/platform/third_party/android/libraries) -lcurl
- with:
# it is used for ndk-r4 # if you build with nkd-r4, uncomment it LOCAL_LDLIBS := -L$(LOCAL_PATH)/../../libs/armeabi -lcocos2d -llog -lcocosdenshion \ -L$(LOCAL_PATH)/../../../../cocos2dx/platform/third_party/android/libraries -lcurl # it is used for ndk-r5 # if you build with ndk-r4, comment it # because the new Windows toolchain doesn't support Cygwin's drive # mapping (i.e /cygdrive/c/ instead of C:/) # LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/armeabi) \ # -lcocos2d -llog -lcocosdenshion \ # -L$(call host-path, $(LOCAL_PATH)/../../../../cocos2dx/platform/third_party/android/libraries) -lcurl
Change build_native.sh
Set "ANDROID_NDK_ROOT" to the path of ndk-r4
Your Cygwin's version should be 1.7 or above
This is described in the NDK documentation.
NOTE
We recommend you to use Eclipse as your IDE when programming for Android, not only for cocos2d-x projects, but all other apps as well, because it is the IDE that Google supports. The Android tutorials on this site will also be using Eclipse for its examples, making it easier to follow. Other IDEs are fine, but we do not discuss them here.
2. Compiling
Change the value of variables ANDROID_NDK_ROOT and COCOS2DX_ROOT, which are defined in $COCOS2DX_ROOT/HelloWorld/android/build_native.sh. COCOS2DX_ROOT means the directory path of cocos2d-x, it contains directories such as Box2D, chipmunk and so on. This section will discuss how to compile a project without an IDE.
Linux
To compile the project, simply runbuild_native.sh
in your shell. In Linux, this simply means opening up a shell prompt such asbash.
Here is an example:
cd /usr/workspace/cocos2d-x/HelloWorld/android/ ./build_native.sh
This assumes that you put cocos2d-x in/usr/workspace
, and left HelloWorld in its default location.
Windows using Cygwin
The same applies for Windows, but you will need to use Cygwin'sbash, rather than the command prompt. This is becausebuild_native.sh
is a shell script meant for Linux, which calls a Linux-based tool included in Android-NDK. If you were to create an NDK app without using cocos2d-x, you would still have to call the tool,ndk_build, from Cygwin.build_native
simply automates the process of building the project.
Here is an example:
bash cd /cygdrive/c/cocos2d-x/HelloWorld/android ./build_native.sh
This assumes that you put cocos2d-x inC:/
, and left HelloWorld in its default location.
3. Running
Running Helloworld is the same as other ndk samples.
3.1 Build Project
This step will generate R.java automatically, it will also compile the java code and generate the.apk
.
3.2 Clean Project
If you recompile the native code and want to pack the .so to .apk, you should do this step. Step 1 will not pack the .so to .apk except the first time.
3.3 Run Project
Right click the project, select "run as", then select "Android Application". Don't forget to start your emulator before running.
4. Warning
You should do "clean project" after recompiling native code. If not, the latest .so will not be
packed into .apk.