September 11, 2012

Creating a cross-platform project on Eclipse and Xcode

Picking up from where we left off on the previous post (click here if you still haven’t seen it), we’re now going to make a new Cocos2dx project that works on both Eclipse and Xcode and eventually, on Android and iOS device. This tutorial will assume you have followed the previous posts. I will refer to the same directories whenever you see Cocos2dx Root, Android SDK Root, and Android NDK Root.



Create an Eclipse Android Project using Cocos2dx
Cocos2dx still has no templates for Eclipse, so we will have to do it ourselves. But don’t worry, Cocos2dx will automate most of the processes we need to do.

1. Go to Cocos2dx Root folder. You should see a create-android-project.sh file. Open that file in the Terminal. On the Terminal, type in cd and a space then drag the Cocos2dx Root folder in the Terminal. Press Return and type in ./create-android-project.sh. That will run the script.

2. The Wizard will prompt you several questions. First is the package name. You can use reverse domain name like com.lancedev.lancegames.

3. Next you will be asked for an Android Version. Read the list that pops up and look at the number after id: You will need to input that ID. Choose the lowest Android app version your app will run on. Android 2.2 is a good choice. NOTE: The list depends on the Android APIs you have installed. You might not see Android 2.2 in your list. If the version you want is not in your list, you might need to open up the AVD Manager (either on Eclipse or on the Terminal) and download the Android API you want.

4. After that, you will be asked for the Project Name. Name it as you wish but make sure not to use spaces and / or special characters. Like MyFirstGame or SpaceFightPRO.

5. Wait for it to finish and look for the line that says Created project directory. It should point to a directory that looks like cocos2dx/<PROJECT NAME>/proj.android. Take note of this path as I will be referring to it as Project Root.

6. In the Terminal, type in cd <PROJECT NAME>/proj.android. Remember to replace <PROJECT NAME> to the name of your project. Hit Return and you will enter the proj.android folder. Type in ./build_native.sh.

7. Wait for the process to finish.

8. Open Eclipse. Go to File > New > Other ( ⌘N ). Select Create Android Project From Existing Code. In the window that appears, select Project Root folder.

9. If you try to run the project using the AVD ( On the left pane, right-click the project and select Run As > Android Application. If you don’t have an AVD set up, Eclipse will prompt you to make one. It’s pretty self explanatory, so there won’t be problems ), you will only get the AVD Home Screen but not the Project itself. To fix this, open up the AVD Manager in Eclipse and edit your existing AVD ( or create a new one ). Set the Android version to 4.1. On the hardware part, select New and choose GPU Emulation then press OK. Run the project and you will see the Cocos2dx Hello World Project appear on the AVD.

If you want to test it, connect an Android Device of your choice and run the project. You should see the Cocos2dx Hello World Project appear on your Android Device.

Now that we’ve set up an Android Project, we will now synchronize it with Xcode so that we can work on the same project in both Xcode and Eclipse.

1. Go back to Eclipse and open the Android project we just made. On the Project Explorer, right-click the project and select New > Other.

2. On the window that appears, select Convert to a C/C++ Project.

3. You will be shown yet another window. Select C++ Project then Makefile Project. On the right side select Other Toolchain. Select No when prompted to open C++ Perspective.

4. Right-click the Project on the Project Explorer and select Properties. Under C/C++ Build, uncheck Use default build command. On the text box, add this:

bash ${workspace_loc:/<PROJECT_NAME>}/build_native.sh NDK_DEBUG=1 V=1

Where <PROJECT_NAME> is the project name you used in step 4 in the earlier part of the tutorial.

5. Click Apply and OK. This should rebuild the project you are working on.

6. Right-click the project again and open up the Properties window. Under C/C++ General -> Paths & Symbols, select GNU C++ and click Add. Navigate through the Cocos2dx Root and to the include folder inside the cocos2dx folder. It should look something like this:

/Users/LGDev/Documents/cocos2d-x/cocos2dx/include

7. For this step, it will depend on the android platform you have used for your project. You should add the include folder for whatever android platform you have used. For example:

/Users/LGDev/Documents/AndroidNDK/platforms/android-9/arch-arm/usr/include

This is the platform for Android 3.2 (android-9). You should take note of the platform (in this case android-9) when you create the project.

8. Click Apply. Open up the Properties window again and go to C/C++ General -> Paths & Symbols -> Source Location. Click Link Folder and select File System when it appears. Navigate to the Classes folder in the Project Root folder. Name it Classes.

With that, we now have a C/C++ Android Project. Now to set up so that Xcode and Eclipse can synchronize with each other.

1. Open up Xcode and create a new project using the Cocos2d-x template. I suggest placing it inside the Project Root’s parent folder and name it proj.ios.

2. After the project is made, click the Groups Pane and right-click on the Classes Group. Select Delete and Move to Trash.

3. Next, add a new Group and name it Classes.

4. Right-click on the newly created group and select Add Files. Navigate to the Project Root’s parent folder and open the Classes folder. Select all the files in the Classes folder. Make sure to NOT check the Copy items checkbox.

5. Try running the project and it should work.

After following all these steps, you should now be able to test in both IDEs. What I usually do is to code in Xcode and when I finish something, I test it on iOS devices and simulator. If it works, I then test it on Android devices using Eclipse.

Thanks for reading this tutorial. It kinda went out late, lol sorry about that.

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey,
    I followed the steps and i found that i cant build in eclipse, in the console it shows
    "21:46:51 **** Build of configuration Default for project SpaceGame ****
    bash /Users/JordanLim/Documents/Cocos2D-X/SpaceGame/proj.android/build_native.sh NDK_DEBUG=1 V=1 all
    please define NDK_ROOT

    21:46:52 Build Finished (took 560ms)"

    Than i tried to do the steps for Xcode and the build keep failing, it shows that are errors in every c++ files

    How?

    ReplyDelete
    Replies
    1. Xcode build keep failing only after i delete the classes file, and create a new one then added in the c++ files from the parent root folders

      Delete
    2. managed to fix the error in Xcode by changing some codes in it, but when i change example Hello World to My Hello! on Xcode, i tried to build on android, but the Hello World doesnt change on the android, but it does on the iphone simulator

      Delete
    3. 1. For the "Please define NDK_ROOT" error, you need to change the "build_native.sh" file on your proj.android folder and add this line on the top:

      export NDK_ROOT="/YOUR/NDK/ROOT/DIR"

      Eclipse does not use the bash profile.

      2. The Classes folder you need to point Xcode to is the Classes folder where your proj.android resides. I suggest taking your iOS project folder into the same directory as proj.android folder. So your project set up would look like this:

      - Cocos2d-x Root
      - - MyProject
      - - Classes
      - - proj.android
      - - proj.ios
      - - Resources

      3. Doing the above would likely fix the issue of changes not being reflected on both builds. Do comment again if you still haven't got it.

      Delete
    4. i dont understand the part 1. for part 2, my project folder is already in that order, i checked the linked classes file on android and the file have already been edited, it just that i cant build it to be the "changed" application

      Delete
    5. On the proj.android folder, there is a "build_native.sh" file which you can open using TextEdit. Add the script on the top line.

      Your Android project might not be properly configured. First, do the NDK_ROOT fix and then open your "proj.android/jni/Android.mk" using TextEdit or Eclipse and then on the LOCAL_SRC_FILES add new cpp files followed with a backslash ('\') for every CPP file except for the last one.

      Delete
    6. ISO C++ forbids declaration of 'LAYER_CREATE_FUNC' with no type [-fpermissive] HelloWorldScene.h /SpaceGame/Classes line 19 C/C++ Problem

      make: *** [obj/local/armeabi/objs-debug/game_shared/hellocpp/main.o] Error 1 SpaceGame C/C++ Problem

      Android NDK: WARNING: APP_PLATFORM android-9 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml SpaceGame line 128, external location: /Users/JordanLim/Documents/AndroidNDK/build/core/add-application.mk C/C++ Problem

      This are the 3 errors in eclipse i get after fixing the NDK_ROOT.
      The codes run on xcode though

      Delete
  3. Hi, I have setup the environment on both X-Code and Eclipse. Now made some changes in the X-Code project, which ran successfully on the ios simulator, But when i ran my eclipse project it didn't showed up any changes. Also, when i opened up the class folder in my eclipse project, it showed me error, which was "cannot resolve the import CCApplication.h". Please help me finding where I am doing wrong.
    Regards,
    Bakshish Singh

    ReplyDelete
  4. Hello,
    The Article on Creating a cross-platform project on Eclipse and Xcode is amazing .It give detail information about Cross Platform App Development.Thanks for sharing this valuable information. Its really useful for me.Xamarin Apps Development

    ReplyDelete