前面一段時(shí)間只用Cocos2d-x在IOS平臺(tái)下開發(fā), 學(xué)習(xí)Cocos2d-x不就是為了他的跨平臺(tái)特性么? 所以必須也要掌握在Android下的開發(fā), 才能引爆Cocos2d-x的跨平臺(tái)特性, 發(fā)揮其最大長(zhǎng)處.
Cocos2d-x版本發(fā)展很快, 而在Android+Eclipse的開發(fā)環(huán)境配置超級(jí)復(fù)雜, 很坑爹有木有啊????
如果你發(fā)現(xiàn)有不一樣的地方, 很有可能是版本不同造成的, 當(dāng)然也可能是我搞錯(cuò)了, 望不吝賜教.
版本:
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
如果下文提及這些路徑, 請(qǐng)?zhí)鎿Q成你自己的路徑.
1.配置Cocos2d-x for Xcode.
這一塊比較簡(jiǎn)單, 可以參見其他文章.
重點(diǎn):install-templates-xcode.sh
2.配置普通Android開發(fā)環(huán)境
這一塊包括JDK安裝, Eclipse安裝, ADT安裝.教程也非常多.
3.Eclipse中安裝CDT, 即C++開發(fā)環(huán)境
4.設(shè)置環(huán)境變量, 即編輯 ~/.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.創(chuàng)建Android項(xiàng)目, 項(xiàng)目名稱"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.編譯默認(rèn)的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. 創(chuàng)建Eclipse項(xiàng)目, 并運(yùn)行默認(rèn)Demo
如圖, 選擇Android Project from Existing Code.
地址選擇為:/opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android (直接輸入可能會(huì)報(bào)錯(cuò), 請(qǐng)用鼠標(biāo)選擇目錄)
點(diǎn)擊下一步就將項(xiàng)目加入了Eclipse中.但是項(xiàng)目會(huì)報(bào)錯(cuò).
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
作為一個(gè)項(xiàng)目添加進(jìn)來, 即可解決報(bào)錯(cuò)問題.
8.編譯運(yùn)行
我嘗試在Android2.3.3虛擬機(jī)運(yùn)行項(xiàng)目失敗.
沒有關(guān)系, 我用真機(jī)測(cè)試. adb 無線連上真機(jī)(cm10真好, 自帶無線調(diào)試)
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, 運(yùn)行成功, 如圖.
FPS有點(diǎn)低哈, 我的ipad2, 都能全速60幀.
9.創(chuàng)建名稱為CCTest的Xcode項(xiàng)目.
創(chuàng)建后你可以運(yùn)行測(cè)試下.
刪除項(xiàng)目下Classes目錄
然后將目錄/opt/cocos2d-2.0-x-2.0.3/CCTest/Classes添加到項(xiàng)目.請(qǐng)確保Destination是沒有被選中的.這樣代碼將是以引用的方式存在在項(xiàng)目中的.
修改Classes下HelloWorldScene.cpp, 并運(yùn)行測(cè)試.
CCLabelTTF* pLabel = CCLabelTTF::create("Hello Cocos2d-x!", "Thonburi", 34);
成功.cocos2d-x 2.0.3的版本在ios6上有方向旋轉(zhuǎn)的問題, 應(yīng)該為橫屏才對(duì).
10.在Android中重新運(yùn)行.
首先運(yùn)行:
/opt/cocos2d-2.0-x-2.0.3/CCTest/proj.android/build_native.sh
在Eclipse菜單中選擇Project=>Clean清理下項(xiàng)目, 然后Run下, 可以在手機(jī)看到更改也已經(jīng)生效.
11. 覺得上一步繁瑣? 能不能點(diǎn)擊Run同時(shí)也編譯C++資源呢? 答案是可以的.
選中Eclipse左側(cè)CCTest項(xiàng)目, 右鍵點(diǎn)擊New=>Other,
在彈出對(duì)話框中選擇C/C++=>Convert to a C/C++Proejct(Adds C/C++ Nature)
在接下來一屏中選擇C++ Project以及 Project Type為Makefile project, Toolchains為Other
接下來Eclipse問是否切換到C++視圖, 選擇否. 如果不小心切換過去了, 再切換回java視圖即可.
打開項(xiàng)目屬性頁面, 設(shè)置自動(dòng)編譯C++, 如圖:
注意修改Build command為, 另外需要注意項(xiàng)目名稱.
bash ${workspace_loc:/com.young40.CCTest}/build_native.sh NDK_DEBUG=1 V=1
點(diǎn)擊Run,之后我這里報(bào)錯(cuò), 編譯提示說"please define NDK_ROOT", 需要在項(xiàng)目設(shè)置中添加環(huán)境變量.如圖
這個(gè)時(shí)候再點(diǎn)擊Run, 會(huì)看到Console窗口會(huì)有編譯的輸出, 編譯成功. 運(yùn)行成功.
12.消除Eclipse中的錯(cuò)誤提示.
如果打開項(xiàng)目jni目錄下hellocpp/main.cpp文件, 會(huì)看到大量的報(bào)錯(cuò). 我們需要講相關(guān)文件添加到項(xiàng)目中.
首先添加/opt/cocos2d-2.0-x-2.0.3/CCTest/Classes到項(xiàng)目中.如圖添加一個(gè)Link類型的文件夾.
用同樣方法將"/opt/cocos2d-2.0-x-2.0.3/cocos2dx"添加到項(xiàng)目中.
然后如圖添加引用.
/opt/android-ndk-r8b/platforms/android-8/arch-arm/usr/include
/opt/cocos2d-2.0-x-2.0.3/CocosDenshion/include
這里和子龍山人翻譯的老外的文章做法略有不同, 他那個(gè)方法還是要報(bào)很多錯(cuò), 可能是版本不同造成的.
我這個(gè)方法也不用忽略文件, 用什么類直接鼠標(biāo)點(diǎn)擊可以看到相應(yīng)聲明.
這樣初步的IOS&Android開發(fā)環(huán)境就集成好了.