Cocos2dx hybrid project iOS and Android
So I'm porting a game from iOS to Android. It's a Cocos2d game. Naturally I look to implementing the Android port using Cocos2dX, since it's a C++ port of Cocos2d.
I found many tutorials online of how to create a shared project for Android and iOS. By "shared", I mean the C++ code. The C++ code is common to iOS and Android. With some Objective-C code binding for the iOS project and some Java code for the Android project.
But the Objective-C and Java codes are small and do not change.
The ones that change is the C++ codes, shared by iOS and Android project.
Among all tutorials online, I find this one most concise and easy to follow: http://www.bold-it.com/ios/cocos2d-x-box2d-iosandroid-hybrid-tutorial/
It's just that the tutorial is written for Cocos2d-2.0-x-2.0.2. The most current version as of this writing is Cocos2d-2.0-x-2.0.4.
Only minor version change but I had to make some extra adjustment to compile and run both project successfully.
So, follow the steps in the above tutorial:
1. Download Cocos2dx.
2. Install the XCode template: ./install-templates-xcode.sh
3. Create XCode project for iOS.
4. Edit the create-android-project.sh file before creating the Android project. Set the NDK_ROOT_LOCAL and ANDROID_SDK_ROOT_LOCAL variables.
5. Execute create-android-project.sh --box2d to create Android box2D project.
6. Make sure it compiles by executing ./build_native.sh
7. Move the necessary Android files:
/android/build_native.sh, change line 40: COCOS2DX_ROOT="$DIR/../libs"
9. Next, in android/jni/Android.mk, add a LOCAL_C_INCLUDES path to Box2D and change “$(call import-module,external/Box2D)” to “$(call import-module,Box2D)”. To be able to see logs in Logcat, add LOCAL_CFLAGS += -DCOCOS2D_DEBUG=1. Delete this line or set the value to 0 for production to improve efficiency.
This is how my Android.mk looks like:
After the log is enabled, you can see the log output from console by typing: adb logcat | grep D/cocos2d-x. Don't forget to use CCLOG to print debugging messages.
10. Update android.library.reference.1 in android/project.properties:
# Project target.
target=android-10
android.library.reference.1=../libs/cocos2dx/platform/android/java
11. Define sdk.dir in libs/cocos2dx/platform/android/java/ant.properties as such:
sdk.dir=/Users/firman/Documents/android-sdk-macosx
12. Edit the target in libs/cocos2dx/platform/android/java/project.properties to match the target defined in android/project.properties:
target=android-10
Done! Now you can run the iOS project from inside XCode. As for the Android project:
I found many tutorials online of how to create a shared project for Android and iOS. By "shared", I mean the C++ code. The C++ code is common to iOS and Android. With some Objective-C code binding for the iOS project and some Java code for the Android project.
But the Objective-C and Java codes are small and do not change.
The ones that change is the C++ codes, shared by iOS and Android project.
Among all tutorials online, I find this one most concise and easy to follow: http://www.bold-it.com/ios/cocos2d-x-box2d-iosandroid-hybrid-tutorial/
It's just that the tutorial is written for Cocos2d-2.0-x-2.0.2. The most current version as of this writing is Cocos2d-2.0-x-2.0.4.
Only minor version change but I had to make some extra adjustment to compile and run both project successfully.
So, follow the steps in the above tutorial:
1. Download Cocos2dx.
2. Install the XCode template: ./install-templates-xcode.sh
3. Create XCode project for iOS.
4. Edit the create-android-project.sh file before creating the Android project. Set the NDK_ROOT_LOCAL and ANDROID_SDK_ROOT_LOCAL variables.
5. Execute create-android-project.sh --box2d to create Android box2D project.
6. Make sure it compiles by executing ./build_native.sh
7. Move the necessary Android files:
- Move the
/testgame/proj.android folder to the iphone project folder, next to ios. Rename it android. - Copy the
/external/Box2D folder into/libs/ , replace what is there. - Copy the
/cocos2dx folder into/libs/ , replace what is there. - Copy the
/CocosDenshion/android folder into/libs/CocosDenshion/ - Copy the
/extensions folder to/libs/ , replace what is there.
9. Next, in android/jni/Android.mk, add a LOCAL_C_INCLUDES path to Box2D and change “$(call import-module,external/Box2D)” to “$(call import-module,Box2D)”. To be able to see logs in Logcat, add LOCAL_CFLAGS += -DCOCOS2D_DEBUG=1. Delete this line or set the value to 0 for production to improve efficiency.
This is how my Android.mk looks like:
After the log is enabled, you can see the log output from console by typing: adb logcat | grep D/cocos2d-x. Don't forget to use CCLOG to print debugging messages.
10. Update android.library.reference.1 in android/project.properties:
# Project target.
target=android-10
android.library.reference.1=../libs/cocos2dx/platform/android/java
11. Define sdk.dir in libs/cocos2dx/platform/android/java/ant.properties as such:
sdk.dir=/Users/firman/Documents/android-sdk-macosx
12. Edit the target in libs/cocos2dx/platform/android/java/project.properties to match the target defined in android/project.properties:
target=android-10
Done! Now you can run the iOS project from inside XCode. As for the Android project:
./build_native.sh clean
./build_native.sh
ant debug
adb install -r bin/testgame-debug.apk
adb shell am start -a android.intent.action.MAIN -n com.boldit.testgame/.testgame
0 komentar:
Posting Komentar