How to start a Cocos2d-X project designed for both iPhone/iPad and Android
The Cocos2d-x project is always updating, so the latest tutorials on setting up a hybrid work environment aren't completely accurate.
Versions for this tutorial:
Here's the basic outline:
- Install Cocos2d-x
- Install the XCode templates
- Create an iPhone project
- Create an Android project
- Move the necessary Android files
- Change the Android files to fit the new location
Download cocos2d-2.0-x-2.0.2 @ Aug 30 2012 and unzip it somewhere, such as your home directory.
Install the XCode templates
In the Terminal, go to the Cocos2d-x directory, then type:
This will give you a few options in the XCode New Project window.
Create an iPhone project
In XCode, click File->New->Project, create a new cocos2dx_box2d project, and name it anything (for example, 'testgame').
This will create a test game using Cocos2d-x and Box2D. Test it to make sure it runs on your system.
Create an Android project
First you need to set some environment variables. In create-android-project.sh, edit the following lines near the top:
# set environment paramters
Point them to your own NDK and Android SDK paths.
If you want to keep an NDK_ROOT environment variable:
Next, create an android project. In Terminal, in the same directory as before:
The --box2d parameter makes sure to include the Box2D library.
You will be asked three questions:
Identify your project
Input package path. For example: org.cocos2dx.example
Choose what Android version you want. It will list what SDKs are available on your system. If you don't have any, install one.
Now cocos2d-x supports Android 2.2 or upper version
Available Android targets:
id: 7 or "Google Inc.:Google APIs:10"
Name: Google APIs
Vendor: Google Inc.
Description: Android + Google APIs
Based on Android 2.3.3 (API level 10)
* com.android.future.usb.accessory (usb.jar)
API for USB Accessories
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: WVGA854, WQVGA400, HVGA, WQVGA432, WVGA800 (default), QVGA
ABIs : armeabi
input target id:
Last, give a name/directory for your project.
input your project name:
Now, test this to make sure it compiles
Move the necessary Android files
The XCode template only copied enough over for iOS devices. We are going to include the Android files as well.
- Move the <COCOS2DX_ROOT>/testgame/proj.android folder to the iphone project folder, next to ios. Rename it android.
- Copy the <COCOS2DX_ROOT>/external/Box2D folder into <IPHONE_PROJECT>/libs/, replace what is there.
- Copy the <COCOS2DX_ROOT>/cocos2dx folder into <IPHONE_PROJECT>/libs/, replace what is there.
- Copy the <COCOS2DX_ROOT>/CocosDenshion/android folder into <IPHONE_PROJECT>/libs/CocosDenshion/
- Copy the <COCOS2DX_ROOT>/extensions folder to <IPHONE_PROJECT>/libs/, replace what is there.
Change the Android files to fit the new location
We moved the relative location of the cocos2dx folder for the Android project. Instead of going two directories back, the cocos2dx folder is now one directory back and in the libs folder. In <IPHONE_PROJECT>/android/build_native.sh, change line 40:
And update the LOCAL_C_INCLUDES on line 13 to include Box2D:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
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)". The result should look like this:
Test and run
Everything should be set up! Build the project from XCode to test iOS. Use these commands to build for android from the command line (in the new android directory)
adb install -r bin/testgame-debug.apk
adb shell am start -a android.intent.action.MAIN -n com.boldit.testgame/.testgame
The first two commands clean and build the cpp into java. The `ant` command builds a debug apk for us. The last two commands install and run the apk, respectively.
Android NDK: /Users/alex/testgame/testgame/android/../libs/cocos2dx/platform/third_party/android/prebuilt/libcurl/Android.mk: Cannot find module with tag 'Box2D' in import path
Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?
was caused because Box2D was not completely configured.