转载地址:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_run_test_cases_on_android-ndk#How-to-run-test-cases-on-android-ndk
新版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.
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.
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
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
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
Set "ANDROID_NDK_ROOT" to the path of ndk-r4
Use other IDE is ok, but we do not instruct it here.
This is described in google documents.
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
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
Running tests is the same as other ndk samples. We use Eclipse as an example.
This step will generate R.java automatically, it will also compile the java code and
generate the .apk.
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.
Right click the project, select "run as", then select "Android Application". Don't forget to
start your emulator before running.
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.
You should do "clean project" after recompiling native code. If not, the latest .so will not be
packed into .apk.