Mac OS X下配置Cocos2d-x for Android(Eclipse)&IOS(Xcode)开发环境
Mac OS X下配置Cocos2d-x for Android(Eclipse)&IOS(Xcode)开发环境
前面一段时间只用Cocos2d-x在IOS平台下开发, 学习Cocos2d-x不就是为了他的跨平台特性么? 所以必须也要掌握在Android下的开发, 才能引爆Cocos2d-x的跨平台特性, 发挥其最大长处.
Cocos2d-x版本发展很快, 而在Android+Eclipse的开发环境配置超级复杂, 很坑爹有木有啊????
如果你发现有不一样的地方, 很有可能是版本不同造成的, 当然也可能是我搞错了, 望不吝赐教.
版本:
Cocos2d-x: cocos2d-2.0-x-2.0.3
OS X: 10.8
Xcode: 4.5.1
Eclipse: 4.2.1
NDK: android-ndk-r8b
Device: Samsung T959 cm10(Android 4.1.2)
文件路径:
Cocos2d-x:/opt/cocos2d-2.0-x-2.0.3
Android SDK:/opt/android-sdk-macosx
Android NDK:/opt/android-ndk-r8b
如果下文提及这些路径, 请替换成你自己的路径.
1.配置Cocos2d-x for Xcode.
这一块比较简单, 可以参见其他文章.
重点:install-templates-xcode.sh
2.配置普通Android开发环境
这一块包括JDK安装, Eclipse安装, ADT安装.教程也非常多.
3.Eclipse中安装CDT, 即C++开发环境
4.设置环境变量, 即编辑~/.bash_profile
在中添加
export
COCOS2DX_ROOT=/opt/cocos2d-2.0-x-2.0.3
export
ANDROID_SDK_ROOT=/opt/android-sdk-macosx
export
ANDROID_NDK_ROOT=/opt/android-ndk-r8b
export
NDK_ROOT=/opt/android-ndk-r8b
export
PATH=$PATH:$ANDROID_NDK_ROOT
export
PATH=$PATH:$ANDROID_SDK_ROOT
|
5.创建Android项目, 项目名称"CCTest".
Youngs-MacBook-Air:~ young40$ cd /opt/cocos2d-2.0-x-2.0.3/ Youngs-MacBook-Air:cocos2d-2.0-x-2.0.3 young40$ ./create-android-project.sh use global definition of NDK_ROOT: /opt/android-ndk-r8b use global definition of ANDROID_SDK_ROOT: /opt/android-sdk-macosx Input package path. For example: org.cocos2dx.example com.young40 Now cocos2d-x supports Android 2.2 or upper version Available Android targets: ---------- id: 1 or "android-3" Name: Android 1.5 Type: Platform API level: 3 Revision: 4 Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P ABIs : armeabi ---------- id: 2 or "android-10" Name: Android 2.3.3 Type: Platform API level: 10 Revision: 2 Skins: HVGA, QVGA, WQVGA400, WQVGA432, WVGA800 (default), WVGA854 ABIs : armeabi ---------- id: 3 or "android-15" Name: Android 4.0.3 Type: Platform API level: 15 Revision: 3 Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800 ABIs : armeabi-v7a, x86 ---------- id: 4 or "android-16" Name: Android 4.1 Type: Platform API level: 16 Revision: 2 Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in ABIs : armeabi-v7a input target id: 2 input your project name: CCTest Created project directory: /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android Created directory /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/src/com/young40 Added file /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/src/com/young40/CCTest.java Created directory /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/res Created directory /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/bin Created directory /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/libs Created directory /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/res/values Added file /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/res/values/strings.xml Created directory /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/res/layout Added file /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/res/layout/main.xml Added file /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/AndroidManifest.xml Added file /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/build.xml Added file /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/proguard-project.txt Resolved location of library project to: /opt/cocos2d-2.0-x-2.0.3/cocos2dx/platform/android/java Updated project.properties Updated local.properties Updated file /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/proguard-project.txt Youngs-MacBook-Air:cocos2d-2.0-x-2.0.3 young40$
6.编译默认的C++资源
Youngs-MacBook-Air:proj.android young40$ cd /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/ Youngs-MacBook-Air:proj.android young40$ ./build_native.sh NDK_ROOT = /opt/android-ndk-r8b COCOS2DX_ROOT = /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/../.. APP_ROOT = /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/.. APP_ANDROID_ROOT = /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android Using prebuilt externals make: Entering directory `/opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android' Gdbserver : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver Gdbsetup : libs/armeabi/gdb.setup Compile++ thumb : game_shared <= main.cpp In file included from /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/http://www.cnblogs.com/cocos2dx/platform/android/jni/JniHelper.h:27:0, from jni/hellocpp/main.cpp:2: /opt/android-ndk-r8b/platforms/android-14/arch-arm/usr/include/jni.h:592:13: note: the mangling of 'va_list' has changed in GCC 4.4 Compile++ thumb : game_shared <= AppDelegate.cpp Compile++ thumb : game_shared <= HelloWorldScene.cpp Prebuilt : libgnustl_static.a <= <NDK>/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/ Compile++ thumb : cocos2dx_static <= CCConfiguration.cpp .....中间编译输出省略 Prebuilt : curl.a <= /opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/http://www.cnblogs.com/cocos2dx/platform/third_party/android/prebuilt/libcurl/libs/armeabi/ StaticLibrary : libextension.a SharedLibrary : libgame.so Install : libgame.so => libs/armeabi/libgame.so make: Leaving directory `/opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android'
7. 创建Eclipse项目, 并运行默认Demo
如图, 选择Android Project from Existing Code.
地址选择为:/opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android (直接输入可能会报错, 请用鼠标选择目录)
点击下一步就将项目加入了Eclipse中.但是项目会报错.
The import org.cocos2dx.lib cannot be resolved CCTest.java /com.young40.CCTest/src/com/young40 line 26 Java Problem Cocos2dxActivity cannot be resolved to a type CCTest.java /com.young40.CCTest/src/com/young40 line 30 Java Problem Cocos2dxActivity cannot be resolved to a type CCTest.java /com.young40.CCTest/src/com/young40 line 33 Java Problem
我的解决办法是将:
/opt/cocos2d-2.0-x-2.0.3/cocos2dx/platform/android/java
作为一个项目添加进来, 即可解决报错问题.
8.编译运行
我尝试在Android2.3.3虚拟机运行项目失败.
没有关系, 我用真机测试. adb 无线连上真机(cm10真好, 自带无线调试)
Youngs-MacBook-Air:proj.android young40$ /opt/android-sdk-macosx/platform-tools/adb connect 192.168.8.139:5555 connected to 192.168.8.139:5555
OK, 运行成功, 如图.
FPS有点低哈, 我的ipad2, 都能全速60帧.
9.创建名称为CCTest的Xcode项目.
创建后你可以运行测试下.
删除项目下Classes目录
然后将目录/opt/cocos2d-2.0-x-2.0.3/CCTest/Classes添加到项目.请确保Destination是没有被选中的.这样代码将是以引用的方式存在在项目中的.
修改Classes下HelloWorldScene.cpp, 并运行测试.
CCLabelTTF* pLabel = CCLabelTTF::create("Hello Cocos2d-x!", "Thonburi", 34);
成功.cocos2d-x 2.0.3的版本在ios6上有方向旋转的问题, 应该为横屏才对.
10.在Android中重新运行.
首先运行:
/opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/build_native.sh
在Eclipse菜单中选择Project=>Clean清理下项目, 然后Run下, 可以在手机看到更改也已经生效.
11. 觉得上一步繁琐? 能不能点击Run同时也编译C++资源呢? 答案是可以的.
选中Eclipse左侧CCTest项目, 右键点击New=>Other,
在弹出对话框中选择C/C++=>Convert to a C/C++Proejct(Adds C/C++ Nature)
在接下来一屏中选择C++ Project以及 Project Type为Makefile project, Toolchains为Other
接下来Eclipse问是否切换到C++视图, 选择否. 如果不小心切换过去了, 再切换回java视图即可.
打开项目属性页面, 设置自动编译C++, 如图:
注意修改Build command为, 另外需要注意项目名称.
bash ${workspace_loc:/com.young40.CCTest}/build_native.sh NDK_DEBUG=1 V=1
点击Run,之后我这里报错, 编译提示说"please define NDK_ROOT", 需要在项目设置中添加环境变量.如图
这个时候再点击Run, 会看到Console窗口会有编译的输出, 编译成功. 运行成功.
12.消除Eclipse中的错误提示.
如果打开项目jni目录下hellocpp/main.cpp文件, 会看到大量的报错. 我们需要讲相关文件添加到项目中.
首先添加/opt/cocos2d-2.0-x-2.0.3/CCTest/Classes到项目中.如图添加一个Link类型的文件夹.
用同样方法将"/opt/cocos2d-2.0-x-2.0.3/cocos2dx"添加到项目中.
然后如图添加引用.
/opt/android-ndk-r8b/platforms/android-8/arch-arm/usr/include
/opt/cocos2d-2.0-x-2.0.3/CocosDenshion/include
这里和子龙山人翻译的老外的文章做法略有不同, 他那个方法还是要报很多错, 可能是版本不同造成的.
我这个方法也不用忽略文件, 用什么类直接鼠标点击可以看到相应声明.
这样初步的IOS&Android开发环境就集成好了.
如果有什么问题, 欢迎交流
参考:
1.(译)cocos2d-x跨android&ios平台开发入门教程
http://www.cnblogs.com/zilongshanren/archive/2012/04/28/2473282.html