Monday, February 23, 2015

Compile Cocos2D-x Project For Android Game Development

My first Cocos2D-X game development project Running on Android Mobile

The code we write in C++ is used as it is to generate a build for Android platform using the tools supplied by Cocos2D-x engine. The platform dependent layer is very thin and lightweight, and you don't have to mess with it to develop Android game.

Compile Cocos2D-x Project using NDK & Cocos2d-Console

First of all we need to compile our Android project using Android NDK(Native Development Kit). You will need to download Android NDK first as mentioned here. Once you have the Android NDK.

Environment Variable NDK_ROOT

You will need to define the environment variable NDK_ROOT in your environment variables.
  • If "My Computer" is shown on your desktop, right click it and select Properties.
  • Otherwise Launch Windows Explorer by pressing Windows + E keyboard shortcut.
  • Right click "This PC" in Windows 8 or "My Computer" in older versions of Windows, select Properties menu item.
Fig 1: My Compters/This PC Properties

  • Now click "Advanced System settings"
Fig 2: Advanced System Variables

  • Click "Environment Variables" button in "System Properties" dialog
Fig 3: Environment Variables

  • Under "System Variables" click "New" button.
  • In the new dialog type in variable name "NDK_ROOT" and paste the path of your NDK (Android Native Development Kit) folder.
  • Click OK.

Fig 4: New Environment Variable NDK_ROOT

Important: If you have a command prompt window open which you are using to run Cocos2d-Console or other script, you will need to close it and open it again in order to load new environment variable values.

Environment Variable ANDROID_SDK_ROOT

Make sure you also have this variable present in your Environment Variables System Variables section. If it is not present, follow the steps described for NDK_ROOT environment variable. Environment variable name and and path value will be different, common sense! The path will be the location where you Android SDK is installed. In my case it was "D:\adt\sdk".

Compiling the native source code with Cocos2d-Console

Suppose we have the project structure shown in the image below:
Fig 5: Cocos2D-x project folder

  • Open command prompt.
  • Change directory to the location of your project.
  • Once there, type following cocos command

cocos compile -s ".\FantasyWarrior3D" -p android --ndk-mode debug

A lot of ugly text will be output to the console window as a result of this command. If all goes well, the process will report "BUILD SUCCESSFUL" in all caps. The machine is yelling at you!
A screenshot is given below.
Fig 6: Compiling Cocos2D-X Project with Cocos Console

You will need to run this command either manually or configure Eclipse to do it for you. I recommend manual compiling since it can take a while for large projects.

Create Eclipse Project For you Cocos2D-X Game

I know Android Studio is the latest standard, we will create a project with later on. Right now I had Eclipse on my system and I was able to find a how-to build native project in Eclipse so I went ahead in order to make sure everything else was okay.
Steps are given below.

  • Run Eclipse ADT.
  • Click File - > New -> Other
  • Select "Android Project From Existing Code"
Fig 7: Creating new Android project

  • Browse to the folder path of your Android project.
  • Click Next, and click Finish.
Your Android project is in good shape, we only need to change a few more things.

Configure builder for Android NDK

  • In Workspace view right click you Android project and click Properties.

Fig 8: Eclipse ADT project properties

  • Click on "Builders" and click "New"
Fig 9: Eclipse Project Properties

Builder selection dialog will open as shown below, select "Program".
Fig 10: Program selection

  • Select Program and click OK.

Edit configuration dialog will open next. 

Fig 11: Edit Configuration

  • Supply the path of file "ndk-build.cmd", on my system it was "D:\Cocos\android-ndk-r10d-windows\android-ndk-r10d\ndk-build.cmd".

  • Go to "Refresh" tab and check the "Recursively include sub-folders" button.
  • Click "Specify Resources" button.
Fig 12: Refresh tab

  • In the next dialog scroll down to your project and open it, select the "Lib" folder. In case the folder is missing, create it yourself.

Fig 13: Lib selection

  • Go to build options tab and make sure all check boxes are clicked as shown in image below.
Fig 14: Build Options

  • Click "Specify Resources".
  • In the next dialog box, scroll around and expand your project, then select "jni" folder.

Fig 16: Eclipse ADT JNI selection

  • Now click OK, your Android Game project is ready to run on your Android device or emulator.
Important: The value specified in manifest tag
  <uses-sdk android:minSdkVersion="14"/>
must be same as the value specified in Eclipse properties menu.

This Mobile Pearls article was also very useful.

Please feel free to contact me, comment this post, and do follow on Twitter: na3mAkrm

No comments:

Post a Comment

Feel free to talk back...