Android on Mac: Cocos2dx, Android SDK and CrystaX NDK

My main goal in this series is to develop Android and iOS apps with Cocos2dx. I only want to use Xcode. I have no interest at all in trying to use Eclipse.

First step for developing for Android on the Mac is to install all the required libraries.  We will need the latest versions of Cocos2dx (cocos2d-rc2-x-2.0.1), Android SDK (android-sdk_r20.0.1-macosx) and CrystaX NDK (android-ndk-r7-crystax-5.beta2-darwin-x86).  The versions in the parenthesis are the ones I have used for this exercise.  I have chosen the CrystaX NDK over the standard Android NDK for the various added features such as C++11 and Obj-C support. Feel free to pick your preference.

Create a “src” directory off you home directory. Place the three packages you downloaded into the ~/src directory and unpack them.  The “~” represents you home directory (/Users/yourname/). Bring up a terminal window and create a few links as shown here.

$ cd ~/src
$ ls -F
android-ndk-r7-crystax-5.beta2/
android-sdk-macosx/
cocos2d-2.0-rc2-x-2.0.1/

$ ln -s android-ndk-r7-crystax-5.beta2 android-ndk
$ ln -s android-sdk-macosx android-sdk
$ ln -s cocos2d-2.0-rc2-x-2.0.1 cocos2dx

$ ls -F
android-ndk@                    
android-ndk-r7-crystax-5.beta2/
android-sdk@
android-sdk-macosx/
cocos2dx@
cocos2d-2.0-rc2-x-2.0.1/

The links help keep the library paths a little more readable and easier to manage.  I keep various older versions of all these libraries around for troubleshooting purposes. It’s easy to swap them out.  I just create a link to the version of the library I want to use.

Complete the install of Cocos2dx as follows:

$ cd ~/src/cocos2dx
$ ./install-templates-xcode.sh -u

Add the following to your ~/.bash_profile, create this file if it does not exist. This will setup your environment for the remainder of this series.

export NDK_ROOT=~/src/android-ndk
export ANDROID_SDK_ROOT=~/src/android-sdk
export NDK_ROOT_LOCAL=$NDK_ROOT
export COCOS2DX_ROOT_LOCAL=~/src/cocos2dx
export PATH=${PATH}:${ANDROID_SDK_ROOT}/tools:${ANDROID_SDK_ROOT}/platform-tools

Now either bring up a NEW terminal window or type “source ~/.bash_profile” at the command prompt. If everything was done correctly entering “android” at the command prompt will bring up the sdk installer. Select and install the following packages below.

Installing the packages will take a while. There is no installation required for the CrystaX NDK (or Android NDK) other than what we have already done.  Once complete let’s test that things are working properly. We’ll create an Android Cocos2d project, compile the C++ files and assemble an apk file.

Create a new Android project (with my responses as well):

$ cd ~/src/cocos2dx
$ ./create-android-project.sh

Input package path. For example: org.cocos2dx.example
com.company.project

# I chose the target id that corresponded to "android-16"
input target id:
1

input your project name:
project

Build apk file:

$ cd ~src/cocos2dx/project/proj.android
$ ./build_native.sh
$ ant debug

If you get a failure similar to “Error: No resource found that matches the given name (at ‘icon’ with value ‘@drawable/icon’)” you will need to edit the AndroidManifest.xml file and change “drawable/icon” to “drawable/ic_launcher”.  I had to do this for the specific library versions I used to create this post.

Test in Android emulator (not working for me using all these newer libraries, app crashes when I try to start it):

$ emulator @project &
$ adb install -r bin/project-debug.apk

Test on real device! Make sure you enabled USB Debugging (Settings > Applications > Development) on the device:

$ ant debug install

Great! If you’ve made it this far you have a working Andriod build system. Coming up in the next couple of posts we will create a hybrid iOS/Android project, integrate an Android target into Xcode and finally test on a real device (hopefully the emulator as well).


Comments

Android on Mac: Cocos2dx, Android SDK and CrystaX NDK — 1 Comment

  1. Pingback: Android On Mac: The Hybrid iOS/Android Project | Andy Tarkinson

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>