【tonyfield 2013.08.29 】
本人开发环境是 Linux tony-U2 3.2.0-52-generic #78-Ubuntu SMP Fri Jul 26 16:21:44 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux (uname -l 照录)
工具是 google上下载的 adt-bundle-linux-x86_64-20130729,Eclipse IDE比较熟,本来想试试xCode,听说Linux用不了,作罢。Help ->Install New Software... 添加 NDK,必须的,另外也去下载个NDK,我用的是android-ndk-r9,各种SDK包全装上。
再去 http://cocos2d-x.org/ 下载最新 Cocos2d-x-2.1.#。写这篇文章时,已经是 2.1.15了。但我这儿提到的代码内容都请参考 Cocos2d-x-2.1.14。解压缩到 /home/yourname/cocos2d-x-2.1.4,进入这个目录,运行 make-all-linux-project.sh,和Android没啥关系,但会提醒你安装一堆工具,大致是install-deps-linux.sh 提到的种种,你用下面命令搞定也行,不安装估计对Android 项目没有影响,我就不卸载了评测了,麻烦。
$sudo apt-get install libx11-dev libxmu-dev libglu1-mesa-dev libgl2ps-dev libxi-dev libglfw-dev g++ libzip-dev libcurl4-gnutls-dev libfontconfig1-dev libsqlite3-dev libglew*-dev
估计cocos2d项目不是在 Eclipse下开发的(可以印证的是cocos2d-x-2.1.4目录下有一个名为 install-templates-xcode.sh 的脚本文件),C/C++文件组织形式和Eclipse很不相同,可以说是更灵活了。可惜要引入死板的Eclipse Android项目框架,必须重新摆放文件位置,以HelloCpp为例。
打开Eclipse,假设 Eclipse 工作目录是 /home/yourname/workspace,新建 File -> New -> Project...,选择 Android Project from Existing Code,选择/home/yourname/cocos2d-x-2.1.4/samples/Cpp/HelloCpp目录,选中“copy projects into workspace”,导入项目。
将公用类代码 /home/yourname/cocos2d-x-2.1.4/samples/Cpp/HelloCpp/Classes 复制到 HelloCpp/jni;
将assets资源 /home/yourname/cocos2d-x-2.1.4/samples/Cpp/HelloCpp/Resources 复制到 HelloCpp/assets;
修改 Android.mk
将 LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
改为 LOCAL_C_INCLUDES := $(LOCAL_PATH)/Classes
修改 build_native.sh
COCOS2DX_ROOT="$DIR/../../../.."
改为
if [ -z "${COCOS2DX_ROOT}" ];then COCOS2DX_ROOT="$DIR/../../../.." echo "Warning: COCOS2DX_ROOT not defined. will use default value( $COCOS2DX_ROOT )" fi
之后配置一下局部环境变量,在项目属性C/C++ Build -> Environment 中添加
ProjDirPath ${PWD}
COCOS2DX_ROOT /home/yourname/cocos2d-x-2.1.4 或你的Cocos2dx路径
NDK_MODULE_PATH ${COCOS2D_ROOT}:${COCOS2D_ROOT}/cocos2dx/platform/third_party/android/prebuilt
NDK_ROOT <你的NDK 路径>
在项目属性C/C++ General -> Paths and Symbols 的 includes 标签页中修改
${ProjDirPath}/../../../../cocos2dx 改为 ${COCOS2D_ROOT}/cocos2dx
${ProjDirPath}/../../../../cocos2dx/include 改为 ${COCOS2D_ROOT}/cocos2dx/include
${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/lib/gcc/arm-linux-androideabi/4.4.3/include
改为 ${NDK_ROOT}/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.6/include
(如果你是在 32位系统上开发,改为 ${NDK_ROOT}/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.6/include)
在项目属性C/C++ General -> Paths and Symbols 的 symbols 标签页中修改
COCOS2DX_ROOT = ${ProjDirPath}/../../../../ 改为 COCOS2DX_ROOT = ${COCOS2DX_ROO$}
注意:Assembly,GNU C,GNU C++ 如果存在都要做同样修改
2.4.1 __android_log_print(ANDROID_LOG_DEBUG, "cocos2d-x debug info", buf);
/home/yourrname/cocos2d-x-2.1.4/Cocos2dx/platform/android/CCCommon.cpp:48:77: error: format not a string literal and no format arguments [-Werror=format-security]
改为 __android_log_print(ANDROID_LOG_DEBUG, "cocos2d-x debug info", "%s", buf);
编译器越来越不人性了,非要有个格式字串不行。类似这个报错还有好几处,改法都一样。
2.5.1 配置文件configs/config-example.plist 缺失
08-29 17:13:58.215: D/(759): HostConnection::get() New Host Connection established 0x2a112d08, tid 759
08-29 17:13:58.366: D/cocos2d-x debug info(759): Get data from file(configs/config-example.plist) failed!
08-29 17:13:58.386: D/cocos2d-x debug info(759): Assert failed: Invalid parameter!
08-29 17:13:58.386: E/cocos2d-x assert(759): /home/yourname/cocos2d-x-2.1.4/cocos2dx/support/data_support/ccCArray.cpp function:ccArrayAppendObject line:122
将 assets资源 /home/yourname/cocos2d-x-2.1.4/samples/Cpp/TestCpp/Resources/configs 复制到 HelloCpp/assets;
TestCpp是更有意思的一个例程,通过它我们可以进入Cocos2dx的世界。大致过程和 HelloCpp相同。Resource目录内容复制到assets后要把 Images文件夹下的 .gz 全删除,否则编译资源报告重名错误。
成功后模拟器画面
【转载请注明来自blog.csdn.net/tonyfield 谢谢 2013.08.29 】