Setting up Cocos2D for Android, in Eclipse

puzzle

… This post is meant to help you set up your first Cocos2D Android Project.

Note : The Cocos2D for Android project using in my tutorials are a JAVA port of the Cocos2d-x project written in C/C++ . If you are interested in integrating the C/C++ version, then click this link to learn more about that. The benefit of using the C/C++ version is that it “sort of” cross platform, and its easier to modify your codebase in order to make your game run on iPhone/Blackberry/Android . However, you are not a C/C++ programmer and do not want to get involved in the dirty mess of pointers et al, then use the Cocos2D for android project below.

Before you proceed with this tutorial, ensure you have completed the following checklist

  1. Download Cocos2D-android library from Github
  2. Download Eclipse IDE for Java Developers
  3. Download the Android SDK , and  Install the Android Developer Tools (ADT Plugin) by following this tutorial by Google
  4. Setup an Android Virtual Device (AVD) Simulator for testing . In Eclipse, choose Window > Android SDK and AVD Manager. Select Virtual Devices in the left panel.Click New.The Create New AVD dialog appears.Type the name of the AVD, such as “my_avd”.Choose a target. The target is the platform (that is, the version of the Android SDK, such as 2.1) you want to run on the emulator. It is recommended to use Android 2.1 and above for Cocos2d development. Detailed information on testing android applications using an actual android device such as an android tablet pc or mobile phone can be found here .

Setting Up Cocos2d Source Package

After downloading Cocos2d (cocos2d-master.zip),  copy the cocos2d-android folder to your Eclipse workspace and import it as an Android project .  The cocos2d-android folder is a full fledged android project with sample code that demonstrates the capabilities of the cocos2d engine. It may be used as a library project (referenced by other full android projects) or run as a standard Android Project. The src folder contains all the java classes for each function within the game engine. To import, use the following steps

  1. File > Import > Existing Project into workspace
  2. Select root directory > Browse . Select your workspace folder and locate the cocos2d-android folder in itUntitled-4
  3. Now you should have your package explorer look like thisUntitled-5

 Running the Cocos2D Source Package

  • To run the project as an android app you must deselect the library option.Right Click on the project > Properties > Android (on the left) > Deselect the “Is Library” Options. Click Apply and OK.
  • Delete the cocos2d-android.jar file in the bin directory. Failing to delete this file will result in compile errors (both the src files and the .jar file contain the same classes … its like two files with the same name trying to reside in the same directory … a no no).
  • Right click on project > Run As > Android Application
  • You may be prompted to create an appropriate target AVD (simulator) to run your application. Follow the prompt .

You should have your simulator running and looking like this … cocos2 cocos1 Congratulations, you have set up Cocos2D for Android. In order to identify which part of the code is responsible for the different sections of the sample code running above, you can review the project code under package explorer .. src > org.cocos2d.tests Untitled-5

Next Steps

You may peruse the next articles on building your first Slider Puzzle game using Cocos 2D.

Step by Step Guide on How to build your first Slider Puzzle game in Cocos2d for Android – Part 1
http://denvycom.com/blog/step-by-step-guide-on-how-to-build-your-first-slider-puzzle-game-in-cocos2d-for-android-part-1/

Step by Step Guide on How to build your first Slider Puzzle game in Cocos2d for Android – Part 2 http://denvycom.com/blog/step-by-step-guide-on-how-to-build-your-first-slider-puzzle-game-in-cocos2d-for-android-part-2/

I have also included the final source code output for the Slider Puzzle Game here . Simply donwload it, unzip it into your eclipse workspace and import it from eclipse as an existing project. You should be good to go! which you can import and using the steps above. The code in the github repository above is a part of the Gidigames Android app [Now FULLY Open Sourced]  I published a while ago.  Good luck!

About Vykthur

Mobile and Web App Developer and Researcher. Passionate about learning, teaching, and recently - writing.
This entry was posted in Android Tutorials, Cocos2d, Cocos2D for android, Developer Tips, Programming, Tutorials and tagged , , , , , , . Bookmark the permalink.
  • Pingback: Step by Step Guide on How to build your first Slider Puzzle game in Cocos2d for Android – Part 1 | Denvycom()

  • Pingback: How to Slide a Sprite around based on touch/drag input in Cocos2D for Android | Denvycom()

  • John Rucel Ponferrada Domer

    good day Sir. I’m new in android development and in Cocos2D. And our thesis is about android and we proposed a sliding puzzle game. I saw your tutorial about the sliding puzzle and it really catches my eye. Can you please guide us in developing sliding puzzle game for our thesis. Thank you Sir

    • Vykthur

      HI John,
      I think the best way to assist you is to publish the second part of the tutorial. I will make out time to do just that. This way, you can study it and ask more direct and specific questions.

      Wishing you all the best with your thesis.

      V.

    • Vykthur

      Hi John,
      I have posted 3 more updates on this tutorial series.
      I hope they can be helpful with your thesis.

      Regards,

      V.

  • sanchit

    Hi , I followed the tutorial and completed the mentioned checklist. So when i build the project there are some errors:-

    -12 18:03:59 – Dex Loader] Unable to execute dex: Multiple dex files define Lorg/cocos2d/Cocos2D$1;

    [2014-01-12 18:03:59 – cocos2d-android] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lorg/cocos2d/Cocos2D$1;

    Please help regarding these errors.

    Thanks

    • Vykthur

      Hello Sanchit,

      Usually when this error occurs (multiple dex files), its because there are two or versions of the same class being referenced during the compile process. For example, you may be having both the cocos2d java class files and the cocos2d.jar file attached to your project. Note that both contain the same and you should delete one of them.
      You can try removing the cocos2d.jar file from the project library path and try to build the project again.
      You can also try out related solutions at this stackoverflow post…
      http://stackoverflow.com/questions/7870265/unable-to-execute-dex-multiple-dex-files-define-lcom-myapp-rarray

      Regards.
      V.

    • Turk

      This is probably because of your android dependincies library has same packets and classes that the packets in your src folder.I saw same problem and removing this library will solve the problem..right click project–>Properties–>Java Build path–>Remove android dependincies and it will work..

      • Vykthur

        Exactly! Its great that you figured it out!

  • Turk

    First thx for your good job about teaching how to start cocos2d on android…However i dowloaded your puzzle game it show a problem in Utility.java and not compiling..It says unable to resolve target..When i look in to utility it says it is undirectly referenced from required class.What should i do about that problem?

    • Vykthur

      Hi Turk,

      I have replaced the sample code with one that I have updated more recently.
      Its the final output from the step by step guide and should be very useful for you too. You can download or clone it on github.

      https://github.com/chuvidi2003/PuzzleGame

      regards.

      V.

      • Turk

        Thanks for that.

  • nika

    Thanks for this sample code. but how can i have the main.xml?

    • Vykthur

      Hi Nika,

      Normally, for game projects developed using Cocos2D, you do not usually need any xml files and I’ll explain why. In normal android apps, the function of xml files such as main.xml is to layout your user interface. However, in Cocos2D projects you utilize a GLSurfaceView object to create scenes and layers on which you layout your game interface.
      The above being said, you can find your main.xml file in the res/layout folder in your main project folder.

      let me know if this is helpful.

      Victor.

  • Jayson Pinzon

    I have problem with this 🙁 I have errors in src folder and I dont have the jar file in the bin file.. I dont know what to do please help me

  • Mohit Purbey

    can someone help me setting up cocos2d for android in android studio
    i did whats above explained but got these errors
    Error:Error converting bytecode to dex:
    Cause: com.android.dex.DexException: Multiple dex files define Lorg/cocos2d/Cocos2D$1;

    Error:Execution failed for task ‘:app:transformClassesWithDexForDebug’.
    > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘C:Program FilesJavajdk1.7.0_25binjava.exe” finished with non-zero exit value 2