Cocos2d-X Universal Android Build Script

For godsake android developers are left in lurch to find out what the problem is when it comes to cocos2d-x, and even though people say few things here and there, nothing works finally and developers end up in dropping the source code in search of new way to develop the games.

One thing that comes to mind when we thing of android build is the Android.MK file that resides in the jni folder.

The problems we face as android cocos2d-x developers is that, we have to

1. Everytime a new game is created, it copies whole cocos2dx folder to the game folder, making it a huge burden.

2. For every new class file added, we have to add the cpp file name into the Android.MK file to make the build pick up.

SO I came up with an idea below to make it work without the above issues.

1. Set a environment variable / IDE variable so that the build picks up the cocos2d libraries from the default drive location rather than copying it everytime.

2. Add the items into the build scripts.

3. Iteratively pick the cpp files in Classes folder and add it to the Android.MK file during the build dynamically instead of manually adding the filenames.

 

So here are the few changes required to make it work as below.

 

FOR COCOS2D-X : 2.x.x  :

First

1. Add a environment variable or IDE variable COCOS2DX_ROOT to point to the cygwin cocos2dx folder. (Note it should be cygwin not windows format).

e.g In My Case its /cygdrive/c/cocos2dsdk/cocos2d-x-2.2.3/

2. Remove the entry COCOS2DX_ROOT=”$DIR/../../..” in the build_native.sh

3.  Now go to Android.MK file and remove the contents which is similar to below lines

 

LOCAL_SRC_FILES := hellocpp/main.cpp \
 ../../Classes/AppDelegate.cpp \
 ../../Classes/base/BaseMenuItemSprite.cpp \
 ../../Classes/base/BaseScene.cpp \

 

Then add the below lines instead

 

FILE_LIST := $(wildcard $(LOCAL_PATH)/../../Classes/**/*.cpp $(LOCAL_PATH)/../../Classes/*.cpp )
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) \
hellocpp/main.cpp

 

Now you can build your Android Game by removing that extra cocos2dx folder copied and running the build script.

You can even modify the scripts to the main template so that it creates similar build artifacts for new games also.

The same can be applied to Cocos2d-x 3.x also.

Leave a Reply