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:

Cocos2d-x: cocos2d-2.0-x-2.0.2 @ Aug 30 2012

XCode: v4.5

Android NDK: Android NDK, Revision 8b (July 2012)

Here's the basic outline:

  1. Install Cocos2d-x
  2. Install the XCode templates
  3. Create an iPhone project
  4. Create an Android project
  5. Move the necessary Android files
  6. Change the Android files to fit the new location

Install Cocos2d-x

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:

[code lang="shell"]
./install-templates-xcode.sh

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
NDK_ROOT_LOCAL="/Users/alex/android-ndk-r8b"
ANDROID_SDK_ROOT_LOCAL="/Users/alex/android-sdk-macosx"

Point them to your own NDK and Android SDK paths.

If you want to keep an NDK_ROOT environment variable:


export NDK_ROOT=/Users/alex/android-ndk-r8b

Next, create an android project. In Terminal, in the same directory as before:

./create-android-project.sh --box2d

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
com.boldit.testgame

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
Type: Add-On
Vendor: Google Inc.
Revision: 2
Description: Android + Google APIs
Based on Android 2.3.3 (API level 10)
Libraries:
* 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:
7

Last, give a name/directory for your project.


input your project name:
testgame

Now, test this to make sure it compiles


cd testgame
./build_native.sh

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:

[code title="build_native.sh"]
COCOS2DX_ROOT="$DIR/../libs"

And update the LOCAL_C_INCLUDES on line 13 to include Box2D:


LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
$(LOCAL_PATH)/../../libs/Box2d

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)


./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

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.

FAQ

This error:


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.