Developing with cocos2d-x for android on Linux

These days I came across a project named cocos2d-x. It’s a port of cocos2d game engine to C++ that allow us to use the same source code to generate versions for iPhone, Android, MS Window (32 bits) and WoPhones.

In the wiki they explain how to develop for Android on both environments: MS Windows and MacOS.

On Windows you will need to install Cygwin. In my opinion, it’s not a good solution.

Cygwin will occupy a lot of space (around 2.6GB), it’s to slow to compile and it’s hard to debug android’s native code. It’s so hard that folks on cocos2d-x recommends you to develop you game for Windows and after that it gets up and running you compile it for android.

I love to develop on Linux, so when I decided to give a try on cocos2d-x I set up a Linux environment.

It’s what this post is about: how to compile cocos2d-x for android using a Linux distribution (as you may already guessed by the title of this post).

Note: if you don’t have a Linux machine and MS Windows is your primary development environment I strongly recommend you to download VirtualBox and install a Linux virtual machine.

You will need around 6GB of free disk space and 1650MB of RAM dedicated to the virtual machine. Believe me: it runs faster than Cygwin on my Intel Core2Duo.

From now on I assume you have either a Linux machine or a Linux virtual machine up and running.

Pre-requisites
You’ll need to install the following programs:

  • Java SDK.
  • Android NDK.
  • Android SDK.
  • cocos2d.
  • Eclipse IDE.

Downloading pre-requisites

I assume you will download all files to ~/Downloads and that you will install everything in the same place than me. You are free to chose different locations to save and install the files but you must update the paths in the instructions below.

  1. Go to java site and download java JDK. You must download the 32 bits version (Linux x86 – Self Extracting Installer).
  2. Go to developer’s site for Android  and download both Android NDK and SDK.
  3. Go to cocos2d-x site and download the latest version of cocos2d-x (1.0.0-x-0.9.0 at the moment I’m writing).
  4. You are free to use Eclipse IDE + ADT Plugin but I strongly recommend you to install Motorola’s MOTODEV Studio for Android.
    It’s the Eclipse with a bunch plug-ins put together by Motorola. It’s far better than Eclipse IDE + ADT Plugin.
    You can get it for free from here (login required, but free registration)

Installation and configuration

  1. Open the command prompt (see picture 1). All commands will be issued from it.
    Picture 1 – Opening console
  2. Go to the folder where you’ve downloaded files. In my case, it is ~/Downloads.
    Note: <ENTER> means that you must press the keyboard’s ENTER key.Type
  3. Installing java SDK.Type

    Note: I’ve downloaded Java SDK package jdk-6u26-linux-i586.bin. Adjust the command according to the version you’ve downloaded.
  4. Installing android NDK. It’s just a matter of extract it to the desired folder. Type

    Note: I’ve downloaded NDK package android-ndk-r6-linux-x86.tar.bz2. Adjust the command according to the version you’ve downloaded.
  5. Installing android SDK. It’s just a matter of extract it to the desired folder. Type

    Note: I’ve downloaded SDK package android-sdk_r12-linux_x86.tgz. Adjust the command according to the version you’ve downloaded.
  6. Installing cocos2d-x. Like android SDK and NDK you just need to extract it to the desired location. Type

    Note: I’ve downloaded cocos2d package cocos2d-1.0.0-x-0.9.0.zip. Adjust the command according to the version you’ve downloaded.
  7. Installing Motorola’s MOTODEV Studio for Android (Eclipse IDE personalized by Motorola). Type

    It will start a graphical installer. Follow its instructions in order to install MOTODEV Studio for Android.

    At this point everything is installed but still aren’t usable.

    Note: I’ve downloaded package MOTODEV_Studio_for_Android_2.2.1_Linux-x86.bin. Adjust the command according to the version you’ve downloaded.

Configuring environment variables

Type:

Add the folowin lines:

Type:

Save the file and close gedit.
Do Log off and then login again.

Add the folowin lines:

Save the file and close gedit.
Do Log off and then login again.

Adding native support

We need to add native support. It’s done by adding CDT plugin and Sequoyah Native plugin.

Go to MOTODEV->Download componentes as shown in picture 1.

Note: For plain Eclipse IDE,Go to Help->Install new software menu to install it. See this page for more instructions.

Picture 2 – Download components menu

Select native support, mark Enable native support checkbox, fill in the path to Java JDK path field and then click in the OK button. See picture 3.

Picture 3 – Selecting native support

A window will open. It’s the license agreement that you must accept in order to continue. See picture 4.

Picture 4 – License agreement window

Probably you will need to provide your login credentials. It’s the same you’ve used to download MOTODEV Studio for Android. See Picture 5.

Picture 5 – You must provide your login information

Your component will be downloaded and installed. After that you probably will need to restart IDE.

Configuring MOTODEV Studio for Android

Please, refer to this post Adding emulators to MOTODEV Studio for Android to see how to add emulators for Android.

Building the cocos2d’s HelloWorld sample for Android

Go to HelloWorld folder:

Inside HelloWorld folder you have one folder for each platform: android, iphone (ios), win32 and wophone
Go to android folder:

Here is the android’s folder content:

Let us edit build_native.sh to take advantage of the environment variables we’ve set:

Either remove or comment the lines bellow:

If you fail to do this step you will get the following error at the moment you try to execute the build_native.sh script:

Edited: If you’re using android NDK r7, read this post AND this post before proceeding.

You need to make build_native.sh script executable (you need to do this just once):

To build HelloWorld sample type:

When the compilation process got finished you will have the native source code built. The next step is to import the project to Eclipse (MOTODEV Studio for Android, in my case) to generate the apk file.

Importing the project into Eclipse workspace

cocos2d-x samples have a folder named android inside them. This folder has the same structure used by Eclipse when you create a new Android project.

On Eclipse, click on

It will open a window. See picture 6.

Picture 6 – Clcik File->New Project is the 1st step to import an android project into Eclipse

Choose

and click next button. See picture 7.

Picture 7 – Selecting Android Project is the 2nd step to import an android project into Eclipse

Another window will open. In project name type whatever you want (I would put my project name).

Click in the radio button Create project from existing source. It will enable a browse button where you will be able to select the directory. Click in int to select the directory

Note: In the build target select the at least android 2.1 and then click ok.

See picture 8 to have an idea of how this windows looks like and how to the required fill data.

Picture 8 – Filling New Android Project is the 3nd step to import an android project into Eclipse

You can see on picture 9 that I’m going to select folder android under cocos2d-x HelloWorld sample.

Picture 9 – Selecting android folder from cocos2d-x HelloWorld sample project into Eclipse

Once you’ve filled all required fields correctly and clicked on OK button you’ll got your project imported on Eclipse.

Taking in account you’re interested in cocos2d-x I think you will like the book Learning Cocos2D: A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk.

Now that your environment is up and running you should read How to debug cocos2d-x and Java code using Eclipse.

Disclaimer: Please note that the link for the book Learning Cocos2D: A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk in this post is an affiliate link and I do earn a commission through any purchases that you make. If you do make a purchase, I appreciate it greatly! If you wish, shoot me an email or let me know on the Facebook Page so I can thank you personally.
Rest assured that I do not recommend a product / service that I don’t trust and that I don’t use.

Please, try my games, play free on-line games on my site, tweet this post url and share it on facebook, google+ and other social medias.

This entry was posted in android, cocos2d, games and tagged , , , , , , , , , , . Bookmark the permalink.
  • Pingback: How to debug cocos2d-x and Java code using Eclipse | Plicatibu Software Development

  • Daniel Mechtchanenko

    I removed the lines you said to remove but I still got the error :((

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      It seems that the path /home/dany/local/sdk/cocos2d/HelloWorld/android/assetsdon’t exist. To be sure, open command prompt and copy and paste the command bellow:ls /home/dany/local/sdk/cocos2d/HelloWorld/android/assetsPlease, carefully review each step. I wrote this tutorial during the process of building my environment and everything is working fine for me.Moreover, you should be able to use new software versions if you adapt each step to reflect paths used as needed.Regards.

      • Suiram

        Check your read permissions on that files; sometimes doesn’t allow you to read or modify some files.

        • http://blog.plicatibu.com/ Marcio Andrey Oliveira

          Hi, Suiram.

          You’re write. That’s another good point to check.

          Thank you for helping.

  • Daniel Mechtchanenko

    no sorry I got
     mkdir: cannot create directory `/home/dany/local/sdk/cocos2d/HelloWorld/android/assets’: No such file or directory./build_native.sh: line 26: pushd: /home/dany/local/sdk/android-ndk: No such file or directory./build_native.sh: line 27: ./ndk-build: No such file or directory./build_native.sh: line 28: popd: directory stack emptywhat did I do wrong?

  • Daniel Mechtchanenko

    Thank you SO SO much I finally got it working!

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      I’m so glad it’s working for you. 

      I hope to see many games developed by you. Let me know when you release them.Happy coding.

  • Stretcher

    I discovered Cocos2D-x a few days ago. I just tried to install the HelloWorld project on my Android device. Thanks to your great documentation, this was as easy and straightforward that I could hope. Well done, sir !

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      Thanks, Stretcher.

      It’s my pleasure to help you.

      Regards.

  • http://www.facebook.com/osama.rao Osama Rao

    Exactly , why do you ask us to install MOTODEV? 

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      Hi, Osama.

      I prefer to install MOTODEV instead of Eclipse because when you install MOTODEV you have all required plugins for Android development. It’s a time saving. On plain Eclipse you have to install a bunch of plugins yourself and it’s more work. Yeah, I’m lazy.

      Moreover, you can install Motorola emulators from MOTODEV, you can submit your game to both Android Market and Shop4Apps (Motorola’s Android Market) right from MOTODEV IDE.

      One doesn’t need to use MOTODEV but I prefer to use it because in the end it’s easier for me.

      Regards.

  • http://www.facebook.com/ThisIsSourav Sourav Ray

    Hi I don’t think MOTODEV is available any more. I’m trying to use NDK8 on Ubuntu 10.10 (no graphics card – I have ATI Radeon card, but my os fails to detect it).

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      Unfortunately I don’t know how to help you.

      Sorry.

    • Tutuk

      On android developers page, there is currently a download under developer-tools called ‘Android Bundle’ or something like that. It comes with a complete android developing enviroment up-to-date (not NDK still)

  • Satheesh

    “Cygwin will occupy a lot of space(around 2.6GB)”
    So Yeah instead, install VirtualBox for which “you will need around 6GB” only.
    Lolzzz.

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      Why to waste almost 3GB with a tool when you can have a full O.S that won’t have any restriction ?

      Regards.

  • Akash

    Hi Thanks for your great tutorial. I follow your step and i install latest version of cocos 2d x. but when i open an HelloWorld android project it gives that it does not found Cocos2dxActivity package. how i can solve this please help me ?

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      Hi.

      Unfortunately I’m not using cocos2d-x since the version 2, so I have no idea about what can be the problem.

      Sorry.

  • Pingback: SysWerke | How to unbind cocos2d-x Eclipse project from cocos2d-x location

  • Donetsk

    I’m having some trouble with setting up linux development environment. I tried to follow cocos2d-x wiki’s documentation but I couldn’t find how to do some things:
    i.e.
    First, build all libraries
    cd $COCOS2DX
    ./build_linux.sh

    There is no build_linux.sh file in the source code archive
    __
    It’s about cocos2d-x 2.1.4
    I’d really appreciate some help.
    Sorry for raising comments from the dead just for this.
    Donetsk

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      I’m sorry but I don’t use cocos2d-x for almost 2 years.

      I don’t have any clue how is the cocos2d-x nowadays.

      • http://blog.plicatibu.com/ Marcio Andrey Oliveira

        I took a sneak on cocos2d-x 2.1.4 and it seems you just have extract the package and then get into the source folder and type ./make-all-linux-project.sh

        It should check you have all required dependencies and generate binaries for both release and debug versions of cocos2d-x API in order to you use them in your projects.

        Regards.

  • Arslan Ali

    Using prebuilt externals
    /home/arslanali/android/android-ndk-r8b/ndk-build: 185: /home/arslanali/android/android-ndk-r8b/ndk-build: /home/arslanali/android/android-ndk-r8b/prebuilt/linux-x86/bin/make: not found

    I get this error when I try to run “build_native.sh” from inside my project that resides in cocos2d-x folder. I’m really tired of it, it is not getting installed. Please help.

    • http://blog.plicatibu.com/ Marcio Andrey Oliveira

      Hi.

      What happens when your executes the command

      ls -l /home/arslanali/android/android-ndk-r8b/prebuilt/linux-x86/bin/make

      If you get an error message like

      No such file or directory

      it means your installation is not complete.

      Let me know the output of the command above that I’ll try to figure out what is happening.

      Regards.