1.从http://www.cocos2d-x.org/download下载稳定版 比如cocos2d-x-2.2
2.解压cocos2d-x-2.2.zip,比如本文将其解压到 /opt 目录下
3.运行 android-buildsetup.sh,运行之前需要先设置3个环境变量,如将以下变量写到文件 /etc/profile中
export ANDROID_SDK_ROOT=/opt/android-sdk-linux export ANDROID_NDK_ROOT=/opt/android-ndk-r9b export COCOS2DX_ROOT=/opt/cocos2d-x-2.2 export PATH=$COCOS2DX_ROOT:$ANDROID_NDK_ROOT:$ANDROID_SDK_ROOT:$PATH
然后:
$ cd /opt/cocos2d-x-2.2/tools $ ./android-buildsetup.sh
2.2.3版本需要采用如下的方式运行:(如果按照上面的运行话,脚本还给出了提示,赞,这个比较人性化)
$ cd ./tools/android-buildsetup.sh lbq:/opt/cocos2d-x-2.2.3.$ ./tools/android-buildsetup.sh
即改成需要在根目录下运行
android-buildsetup.sh的主要目的是将android sdk与ndk的目录路径写到各个sample工程的local.properties文件中.
成功运行,如下图所示:
4.接着编译JNI,与普通的Andorid工程不同的是,不是进入android工程中的JNI目录,然后运行ndk-build,如果这样运行,会出现如下的错误:
$ cd /opt/cocos2d-x-2.2/samples/Cpp/HelloCpp/proj.android/jni $ ndk-build Android NDK: /opt/cocos2d-x-2.2/samples/Cpp/HelloCpp/proj.android/jni/Android.mk: Cannot find module with tag 'cocos2dx' in import path Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ? Android NDK: The following directories were searched: Android NDK: /opt/cocos2d-x-2.2/samples/Cpp/HelloCpp/proj.android/jni/Android.mk:19: *** Android NDK: Aborting. . Stop.
而应该是:
$ cd /opt/cocos2d-x-2.2/samples/Cpp/HelloCpp/proj.android $ ./build_native.sh
如果正确的话,就会产生.so文件
5.但是如果您的NDK版本不是最新或者不购新的话(至少是r6以上,不包括r6版本),则会出现如下的错误:
/opt/cocos2d-x-2.2/cocos2dx/platform/android/CCGL.h:47: error: 'PFNGLGENVERTEXARRAYSOESPROC' does not name a type /opt/cocos2d-x-2.2/cocos2dx/platform/android/CCGL.h:48: error: 'PFNGLBINDVERTEXARRAYOESPROC' does not name a type /opt/cocos2d-x-2.2/cocos2dx/platform/android/CCGL.h:49: error: 'PFNGLDELETEVERTEXARRAYSOESPROC' does not name a type
网上也能查到很多这样的错误,也都是说NDK版本低的原因。
刚开始我更新了NDK版本并且重新执行了步骤3,但是还会出现以上的错误,
后来发现,是因为build_native.sh这个Shell脚本写的有问题:
# write local.properties if it doesn't already existif [ -f "$_LOCAL_PROPERTIES_FILE" ]thenecho "$_LOCAL_PROPERTIES_FILE exists. skipping overwrite"elseecho "writing $_LOCAL_PROPERTIES_FILE"echo -e "$_CONTENTS" > "$_LOCAL_PROPERTIES_FILE"fi
看到了没,写local.properties当且仅当local.properties文件不存在的时候,所以只要一开始运行过一次android-buildsetup.sh,那么那些android工程下local.properties就不会被更新,所以需要修改下android-buildsetup.sh这个脚本,每次运行都强制写local.properties文件:
# write local.properties wether if is exist # if [ -f "$_LOCAL_PROPERTIES_FILE" ] # then # echo "$_LOCAL_PROPERTIES_FILE exists. skipping overwrite" # elseecho "writing $_LOCAL_PROPERTIES_FILE"echo -e "$_CONTENTS" > "$_LOCAL_PROPERTIES_FILE" # fi
这样,就彻底搞定了。
6./opt/cocos2d-x-2.2/cocos2dx/platform/android这个是cocos2dx引擎的代码,很多sample工程都要引用这个lib
7.HelloCpp的“梦书”(原来是"Hello World"修改了下)二字来自 /opt/cocos2d-x-2.2/samples/Cpp/HelloCpp/Classes 下的HelloWorldScene.cpp文件
CCLabelTTF* pLabel = CCLabelTTF::create("梦书", "Arial", TITLE_FONT_SIZE);
代码很像Object C不是。--- 不过目前每次clean都会连cocos2d-x的引擎代码C++部分一起重新编译,很慢哦
参考:
1. https://github.com/cocos2d/cocos2d-x
2.How to setup Cocos2d-x (Windows and Android)