新版cocos2dx配置有稍微的修改,见url:
http://www.cocos2d-x.org/wiki/How_to_build_and_run_HelloWorld_on_Android_NDK_r5_and_above
This document describes how to run tests under ndk.
The development environment is windows & cygwin, linux is of course.
1. Set Up Development Environment
This document doesn't describe how to set up the environment for ndk. There is already a good description
on the android social site. The address is:
http://developer.android.com/
There are some attentions below.
1.1 The ndk version must be r4 or r5
ndk-r4 offered by google doesn't support STL, but the engine depends on it, so we should
find a version that supports STL. Luckily, some one has 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
1.1.1 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
1.1.2 Change the tests/test.android/jni/tests/Android.mk
Replace
# it is used for ndk-r4 # if you build with nkd-r4, uncomment it # LOCAL_LDLIBS := -L$(LOCAL_PATH)/../../libs/armeabi \ # -lGLESv1_CM \ # -lcocos2d -lcocosdenshion -llog \ # -lbox2d -lchipmunk \ # -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) \ -lGLESv1_CM \ -lcocos2d -llog -lcocosdenshion \ -lbox2d -lchipmunk \ -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 \ -lGLESv1_CM \ -lcocos2d -lcocosdenshion -llog \ -lbox2d -lchipmunk \ -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) \ # -lGLESv1_CM \ # -lcocos2d -llog -lcocosdenshion \ # -lbox2d -lchipmunk \ # -L$(call host-path, $(LOCAL_PATH)/../../../../cocos2dx/platform/third_party/android/libraries) -lcurl
1.1.3 Change build_native.sh
Set "ANDROID_NDK_ROOT" to the path of ndk-r4
1.2 We use eclipse as the IDE
Use other IDE is ok, but we do not instruct it here.
1.3 The version of cygwin should be 1.7 or above
This is described in google documents.
1.4 The tests can run on 2.0 and above
2. Download source code
Here we suppose you have set up the ndk development environment, and you can run the samples
offered by ndk. If not, you should read the google documents carefully, or you can ask some
kind people. It is easy, don't worry about it.
Now, download source code of ndk-r4 and cocos2dx.
The address of ndk-r4 is:
http://www.crystax.net/android/ndk.php
The address of cocos2dx is:
https://github.com/cocos2d/cocos2d-x
3. Compiling
Change the value of variables ANDROID_NDK_ROOT and COCOS2DX_ROOT, which are defined in
$COCOS2DX_ROOT/tests/test.android/build_native.sh. COCOS2DX_ROOT means the path of cocos2d-x,
it contains directories such as Box2D, chipmunk and so on.
Run build_native.sh to compile native code. If you use windows + cygwin, and do not know how to run it, follow me.
Run cygwin Enter the directory of Tests/test.android ./build_native.sh
4. Running
Running tests is the same as other ndk samples. We use Eclipse as an example.
4.1 Build Project
This step will generate R.java automatically, it will also compile the java code and
generate the .apk.
4.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.
4.3 Run Project
Right click the project, select "run as", then select "Android Application". Don't forget to
start your emulator before running.
5. About the resource path
cocos2d-x unzips resource file from .apk at running, so we should set the path of .apk by using
CCFileUtils::setResourcePath(). Because the resource should be putted in specific directory to
packed into .apk, so we put the resource in assets. It is the reason to use CCFileUtils::setRelativePath("assets")
in $COCOS2DX_ROOT/tests/AppDelegate.cpp.
Why not use "assets/Images/xxx.png" in the source code? Becauce the tests code can run in
different platforms, and other platforms such as iOS don't have to put the resource in assets.
6. Warning
6.1 clean project
You should do "clean project" after recompiling native code. If not, the latest .so will not be
packed into .apk.