Development Environment Preparation for Porting IOS Game Made Using the Cocos2D Framework to Android Part 2

Posted by | No Tags | Uncategorized | No Comments on Development Environment Preparation for Porting IOS Game Made Using the Cocos2D Framework to Android Part 2

Aka How Do We Get the Project Created in Part 1 to Work in ADT Eclipse

Based on the ideas given in the first entry, I prepared the next step in the process. This next step naturally presupposes successful completion of the tasks described in the first entry. 🙂

4. ADT Eclipse and Project Settings

adt-logo

Open ADT Eclipse and select $HOME/workspace as your workspace, unless it’s already the default option.

4.1 IDE Settings

4.1.1 Plugins

Start ADT (run it using the following command: $HOME/android/eclipse/eclipse&).
Go to the Help Menu and click on Install New Software.
Select “Android Developer Tools Update Site – http://dl-ssl.google.com/android/eclipse/” from the scroll down menu.
Click on both plugin groups available for installment. Then click on “Next” and work your way through the following steps (accept license agreement, etc).
After this, install the Sequoyah plugins using the method described here.

4.1.2 Android NDK Path

Adjust settings for the Android NDK Library in ADT under Window/Preferences, in the Android/NDK submenu.
Type in (or paste) the $HOME/android/ndk folder.

4.1.3 Fine tuning ADT C++ Code Analysis

ADT hasn’t reached perfection yet in using Android NDK tools, and there are three types of error messages that come up over the course of every build that don’t really exist. These error messages are: “Method cannot be resolved”, “Symbol is not resolved” and “Type cannot be resolved.”
We can simply set Eclipse to ignore these in the Window/Preferences window, in the C/C++ / Code Analysis menu by removing the checkboxes next to the options indicated below. And this takes care of the problem.

code-analysis123

4.2 Import project

Select General/Existing Projects into Workspace in the File/Import menu, then click Next, browse in the $HOME/workspace/HelloCocos2dx folder, uncheck the checkboxes next to the two projects with name HelloCpp and click Finish.

import-project

The two projects should appear in the workspace on the left. (Yay!)
A number of error messages will come up, which will be solved step by step.

4.3. Setting the Java Project as the linked project to Java Build Path

Go to the HelloCocos project, right click/properties menu and when you see the JavaBuild Path/Project screen, click Add. Then select project named “java” and click Ok.

required-project

4.4. Tell ADT that this is NDK project

Right click on the project, choose New/Other menu and select C/C++ / Convert to C/C++ Project when the window comes up. Make sure the only selected project is HelloCocos. Set Project type to Makefile project, and Toolchain to Android GCC.


add-c-nature-1
   add-c-nature-2

4.5. Set up the custom building process suggested by cocos2d-x

Not surprisingly, we have come up with our own version of this, because it often happens that we are not satisfied with already available solutions.

Our version uses a set location for Android SDK and NDK ($HOME/android/sdk,ndk). Our version, unlike Android NDK and cocos2d-x, also dynamically includes all the source files created in our project in the building process. This modified script is included in the previously mentioned, downloadable compressed file or at the following location.
If you right click on the project and open the Properties window, then choose C/C++Build screen. Remove the checkbox next to “Use default build command” and enter the following:


/bin/bash ${workspace_loc:/HelloCocos}/build_native.sh

Note: in the path ${workspace_loc:/HelloCocos} the project is called “HelloCocos” and this can be substituted with the name of the given project.

build

Once we start the translation of our project (right click on the project and then click “build project”) we will see that its works fine. There’s no reason for alarm; the process doesn’t always last this long. The entire cocos2d-x library is compiled upon first building, and it will only rebuild the library if we clean the project or make changes to the files of the cocos2d-x library.
If we click “run”, the HelloCpp sample application will start on the attached device or the running emulator.
If all goes well, this is what we should see on our device screen:

Screenshot_2013-03-11-16-22-04

4.6. But where are my source files????

This is a great question.

Within the project settings (right click/properties) on the “Source Location” page of the “C/C++ General / Paths and Symbols” menu click “Link Folder,” then tick “Link to folder in the file system” and type:


${WORKSPACE_LOC}/HelloCocos2dx/Classes

Then click “Finish” and “OK”. Next, a “Classes” folder will show up in our project where you will find a copy of the cocos2d-x HelloCpp sample. These are the C/C++ source files, which belong to the project. In addition to this, there is a main.cpp file in the project’s jni folder. This is where the Java code is linked to the C/C++ code. Java sources can be found in their usual place in the src folder.

4.7 How to get code completion in Eclipse

This settings below can bring code completion to life in Eclipse. Code completion is one of our biggest, if not the biggest, helps in development.
The way to do this is to type the following on the “Source Location” page of the “C/C++ General / Paths and Symbols” menu after clicking “Link Folder”:


${WORKSPACE_LOC}/HelloCocos2dx/cocos2d-x

After this step, we will have access to the source files of cocos2d-x. You can learn a lot if you take time to browse them.
As soon as the C/C++ indexer starts functioning (to be on the safe side, run a build to refresh the indexes), the code completion (which is context-based) will start once we hit ctrl+space. Voilà!

Summary

Using the methods described above, we have set our ADT Eclipse development environment and imported our previously created project.
By fine tuning the project settings, we can develop and build within our IDE.
Last but not least we have gotten the context based code completion to start working.
We plan three more entires on the same topic: the first on useful external 3rd party libraries, the second on speed up the building process and the third on useful and unique tools and scripts used in the development.
I have shared all my experience in this entry on prepare and constructing the development environment, in fact, there were a number of solutions I realized during the writing process.
The entry might have turned out a bit long, but whoever takes the trouble to go through it step by step will end up with a functional basic Android cocos2d-x project.
Those who wish to put together the framework using Windows will need install cygwin. There are many other differences that such a developer will have to work his or her way through. A few months ago, I also tried to put together the above tools using Windows and decided to avoid future headaches by changed to Xubuntu. One of my colleagues constructed the same framework using the OSX 10.8 Mountain Lion operating system, and has shared with me his invaluable insights. Thanks so much, Vili!
And this is where the fun begins, when porting your software becomes your only concern.


No Comments

Leave a comment