How to setup Cocos2d-x (Windows and Android)

Notes

This is a tutorial on how to setup Cocos2d-x for Windows and Android development on Windows.

Last update: 16-11-2011

Windows

Building cocos2d for windows is pretty straight forward:

  1. Get the source from GitHub: https://github.com/cocos2d/cocos2d-x
  2. Use the provided solution files or run (recommended): build-win32.bat
  3. Binaries are placed in: “\Release.win32″
  4. More information can be found on the wiki

Android

Requirements

  1. Eclipse (with CDT plugin)
  2. Installed JDK (Java SE 6 Update 26) – I recommend downloading the x86 version. Please note that Java SE 7 won’t work.
  3. Android SDK
  4. Installed ADT Plugin for Eclipse
  5. Android NDK
  6. Android NDK r6b Windows (Note: If r7 fails to compile Cocos2d-x, try using the previous NDK, version r6b)

Install MinGW/MSYS

Install MinGW and MSYS according to this tutorial:
How to install MinGW, MSYS and Eclipse on windows
(Don’t forget to install the ADT plugin as well)

No need to install cygwin ;)

Building Cocos2d-x (MSYS/NDK)

My file locations (unzip the SDK and NDK to these locations)

  • NDK: C:\Development\android-ndk-r6b
  • SDK: C:\Development\android-sdk-windows
  • Source location (git): D:\projects\Github\cocos2d-x

Set the NDK root and COCOS2DX root path

Open up these files:

  • D:\projects\Github\cocos2d-x\HelloLua\android\build_native.sh
  • D:\projects\Github\cocos2d-x\HelloWorld\android\build_native.sh
  • D:\projects\Github\cocos2d-x\tests\test.android\build_native.sh

Change this:

ANDROID_NDK_ROOT=/cygdrive/e/android-ndk-r5
COCOS2DX_ROOT=/cygdrive/d/Work7/cocos2d-x

to:

ANDROID_NDK_ROOT=/c/Development/android-ndk-r6b
COCOS2DX_ROOT=/d/projects/Github/cocos2d-x

Run buildscripts
Open up Msys and use the following commands in the terminal to build the projects:

Build HelloLua project

cd /d/projects/Github/cocos2d-x/HelloLua/android/
./build_native.sh

Build HelloWorld project

cd /d/projects/Github/cocos2d-x/HelloWorld/android
./build_native.sh

Build tests project

cd /d/projects/Github/cocos2d-x/tests/test.android
./build_native.sh

It might take a while to build the tests example. On completion you should see this:

Importing projects (Eclipse/SDK)

Open up eclipse
Create a new workspace at D:\projects\Github\cocos2d-x\android

Create a new Android project

HelloWorld example

Create Android Project

  • Set projectname to: HelloWorld
  • Untick: default location
  • Click: Create project from existing source
  • Set location to: D:\projects\Github\cocos2d-x\HelloWorld\android
  • Click next

Select Build Target

  • Set target to Android 2.1
  • Click next

Application Info

  • Set application: HelloWorld
  • Set package name: org.cocos2dx.helloworld
  • Click finish

tests example

Create Android Project

  • Set projectname to: tests
  • Untick: default location
  • Click: Create project from existing source
  • Set location to: D:\projects\Github\cocos2d-x\tests\test.android
  • Click next

Select Build Target

  • Set target to Android 2.1
  • Click next

Application Info

  • Set application: tests
  • Set package name: org.cocos2dx.tests
  • Click finish

HelloLua example

Create Android Project

  • Set projectname to: HelloLua
  • Untick: default location
  • Click: Create project from existing source
  • Set location to: D:\projects\Github\cocos2d-x\HelloLua\android
  • Click next

Select Build Target

  • Set target to Android 2.1
  • Click next

Application Info

  • Set application: HelloLua
  • Set package name: org.cocos2dx.hellolua
  • Click finish

Fixing some java code

All projects should now be available in the package explorer. However, HelloLua and HelloWorld might show some red crosses. You will need to modify a few .java files. All you need to do is remove all lines with @Override.

Build APK and run

  • Click on the project name in the package explorer (for example HelloWorld or tests).
  • Click the green arrow
  • Select Android Application
  • Click OK

If your device is connected to your computer, eclipse will build the APK and install it on your phone/tablet. If no device is found, it will launch the emulator.

Emulator

Running Hello World

Running tests

7 Comment

    Mike

    Im getting an error when I try to compile HelloLua.sh, HelloWorld.sh & test.android. mysys gives me an error on line 1 and line 2 with unexpected end of file. Followed the steps a few times to verify and everything look right. working in Windows 7.

    Thanks,

    22 Dec 2011 | Reply

  1. andres

    Help..How do I ‘Set the NDK root and COCOS2DX root path’?
    I set in Enviroment variables, but I think it is not correct..
    Because it returns error when calling the build_native.sh using msys..
    thanks..

    21 Jan 2012 | Reply

    • Hi Andres, you don’t need to set those two values as Environment variables. You only need to modified the buildscripts (build_native.sh) and change the path in those files.

      21 Jan 2012 | Reply

      • andres

        Hi Laurence, I cannot even open build_native.sh, do I need to install something?thanks.

        22 Jan 2012 | Reply

    Leave a reply