Compiling your Cocos2d-x 3.0 Android project on Mac

In my last post I discussed installing Cocos2d-x and creating a new project. You will find that the project should work in Xcode without any issues at all for both iOS and OSX. Anytime you add or create new files through Xcode just make sure you add them to both targets and you should be all set. To get everything working for Android is not quite as easy”.

I should also note that I absolutely despise Eclipse.  So I will be compiling the Android project using a command terminal.  I may revisit this process with Eclipse later, but not unless anyone asks.

First you are going to need both the Android SDK and the Android NDK, I suggest downloading the latest versions.  On my system I install these under “/mobile/lib” along with any other mobile related libraries.  I keep the version name in the directory because at times I do quite a bit of switching library versions.

You’ll likely need to make some changes in the Android project.  If you’ve added files to Classes you will need to make a modification to the “jni/Android.mk” file by adding or deleting you classes from the LOCAL_SRC_FILES variable.

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/GameLayer.cpp \
                   ../../Classes/GameSprite.cpp

You will also need to set NDK_ROOT in your environment, usually this is set in your “~/.bash_profile”. You will also need to set ANDROID_HOME to point to the Android SDK.  We need this later but might as well do it now as well.

export NDK_ROOT=/mobile/lib/android-ndk-r9d
export ANDROID_HOME=/mobile/lib/adt-bundle-mac-x86_64-20140321/sdk

Now you should be able to execute “python build_native.py” in the root of the Android project. The few lines of output should show that the libcocos2dcpp.so shared library is installed without any errors. Anytime you modify your project and rebuild you will have to re-execute this script.

$ python build_native.py 
...
[armeabi] Install        : libcocos2dcpp.so => libs/armeabi/libcocos2dcpp.so

To create an APK for installation on your device simply you must first update the project with your own settings.  You can do this via the “android update project” command.  I struggled with getting this working properly for Cocos2d-x 3.0 so let me know if you have any troubles.  To update and build your project:

$ $ANDROID_HOME/tools/android update project --path ../cocos2d/cocos/2d/platform/android/java --target android-19
Updated project.properties
Updated local.properties
Updated file ../cocos2d/cocos/2d/platform/android/java/proguard-project.txt
$ ant -Dsdk.dir=$ANDROID_HOME debug
...
BUILD SUCCESSFUL
Total time: 5 seconds
$ ls bin/*debug.apk
bin/AirHockey-debug.apk

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>