Compiling cocos2dx on an Android device
Written by miechu   
Saturday, 25 January 2014 00:00

I had a chance to buy a new Samsung Galaxy 3 Tab, really cheap, so I've bought it since I haven't had an Android device before (and that's a big shame for a mobile games developer like myself). I then had to figure out how to utilize. I've quickly found AIDE, an awesome, complete tool that allows you to write, compile and deploy code on the device alone, no need for a PC. Being an ambitious individual I decided to compile the renown Cocos2dx on my device.

Needless to say I haven't found any clues on the internetz on how to do it. Nor I had any experience with Android development. Yeah, let's do it!

I've started off by reading this article. It was a good read, it allowed me to get the initial grasp of the structure of Android projects, and how to handle different steps of development. And it made me install Android SDK, NDK and Eclipse version that came along with it. The following tutorial doesn't use Eclipse, but I did use it to figure it all out.

Then I've found this one. I followed the tutorial up until section Hello, Cocos2D-X for Android! where it says Now run the create-android-project.sh. I've searched through my trusty cocos2d-x-2.2.1 directory and it turned out the mentioned bash script is simply not there. WTF?!? Hacking mode engaged!

I've searched through my other copies of cocos2d-x and I've managed to find a file of specified name in one of the version I've got from gitHub long time ago. I get a feeling cocos2d-x 2.1 has the file, if anyone has any problems finding it. I've simply copied the file over to the analogous location in my cocos2d-x-2.2.1 directory (just put it in cocos' root dir).

I've run the before-mentioned script from inside cocos2d-x home directory. Script started complaining about android SDK directory and such. Lazy ass script! I had to set up the environment variables NDK_ROOT_LOCAL and ANDROID_SDK_ROOT_LOCAL (yeah, just like described in the tutorial :P).

I've run the script again and this time it started working. It asked me questions:

  • Input package path? - I gave it com.eyecog.NDKGame
  • input target id? - from the printed out list I've picked the one my one-and-only android device has: 1 or "android-16"
  • input your project name? - NDKGame, truthfully
It started to print out stuff and suddenly stopped with last line ending like so:
cocos2d-x-2.2.1/template/android/copy_files.sh: No such file or directory
Hack mode engaged! I've copied the file over from my mentioned git version of cocos2d-x along with the whole template/android directory. Deleted the generated NDKGame directory in cocos2d-x home and tried all over again. This time it run to the very end seemingly without any errors.

From cocos2d-x home directory I've copied over cocos2dx and newly created NDKGame directories to my android device. I've placed them in AIDE's AppProjects under NDKGameProject I've created specially for this occasion.

Ok, let's see what AIDE can do! I've opened it up, navigated to NDKGameProject/ and opened the project (my guess is it's using Android.mk file to detect a compilable project - but then again it's probably written in some documentation. Who would read documentation! It's more fun to discover stuff on your own, like in the old games! :D).

Run

I got this:

Here's my one problem with AIDE: when it encounters an error it also prints out, in the same error format, warnings as well. A bit confusing, but one can get used to it.

Anyway, clicking the latter highlighted the line:

<application android:label="@string/app_name"
This rung a bell. I remembered I've seen something like that in one of tutorials... Ah yes! This one! It basically said to change this
<application android:label="@string/app_name"
android:icon="@drawable/icon">
to this:
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
since it's the appropriate name, apparently.

Change done. Run.

Your project contains errors - it said. Please fix them before running the app - it advised.

Keep calm and carry on. Let's see. This is the actual problem that needed addressing:
NDK: Android NDK: jni/Android.mk: Cannot find module with tag 'CocosDenshion/android' in import path
I didn't really need Cocos Denshion here, but decided to handle that for completeness' sake. First CocosDenshion was simply missing from my code, so had to copy it over, analogously to where it resides in cocos2dx sources. Copying was not enough, trying to compile would result in the same error. This StackOverflow thread helped me solve the issue. All that had to be done was adding this:

$(call import-add-path,$(LOCAL_PATH)/../../..)
to NDKGame/proj.android/jni/Android.mk, just before $(call import-module lines. Also, I've removed this:
$(call import-module,extensions)
to save some compilation time since HelloWorld project doesn't use any of it anyway.

Run.Your project contains errors...

Seems same had to be done for cocos' Android.mk. I've added this line:

$(call import-add-path,$(LOCAL_PATH)/platform/third_party/android/prebuilt)

Run.
Many minutes later...
Success! It compiles! Running... aaand crash.

But this one's easy! Missing assets! I've just copied NDKGame's assets over from NDKGame/Resources to NDKGame/proj.android/assets, since that's where android expects its assets. Done!

Final FYI: on my Galaxy Tab 3 a clean build of whole cocos2dx with CocosDenshion and HelloWorld project takes approximately 30 minutes.

 

Add your comment

Your name:
Comment: