Can't link to cocos2dx using eclipse for android on OSX

I’ve never spent so much time on initial setup for a development environment before, this is getting kind of ridiculous.

I’m trying to run the demo code that comes with cocos2dx. I’m using:

the eclipse flavor that comes with the ADK.
cocos2d-2.1beta3-x-2.1.0
ndk-r8c
Ive tried building an app template using create-android-project.sh which I had the same problems with but for this question I’ll reference the hello.cpp sample app.

I run the build_native.sh script and then import it in eclipse. Next is where all the different tutorials I’ve tried vary. Some say to ‘convert to a c++ project’ some say you can already ‘run as android project’ at this point already.

For me it seem like the android java project can’t link to the cocos2dx project.

I get:

The import org.cocos2dx.lib cannot be resolved
for

import org.cocos2dx.lib.Cocos2dxActivity
No tutorial really made any mention of having to build the cocos2dx libraries except for one that said you now have to import the cocos libraries as projects in to the eclipse workspace. So Ive done that and built them but there are some errors in them after building.

I’ve also tried adding the folder path to link to in the project which for me is at /cocos2d-2.1beta3-x-2.1.0/cocos2dx/platform/android/java/src/org/cocos2dx/lib but that didn’t help.

Any idea why I can’t build/run the examples that come with cocos2dx even when following the build instructions that are on the cocos2dx website to the T?

This is happening because, Eclipse is not able to find the sources of Cocos2D-x. It can be solved as follows:-

Step 1: In Eclipse, right-click on your project and select the last option “properties”.
Step 2: In Project Properties window, select “Java Build Path” in the left pane.
Step 3: Under Java Build Path, select tab “source”. It should be selected by default.
Step 4: Click on button “Link Source”
Step 5: Browse it to following location-> COCOS2D-X_INSTALLATION_FOLDER/cocos2dx/platform/android/java/src

In step 5, replace COCOS2D-X_INSTALLATION_FOLDER with your cocos2d-x directory

Step 6: Give any folder name. For example, I gave: Cocos2d-x-source
Step 7: Click Ok.

It should resolve your problem.

Good Luck!
Paras Mendiratta

1 Like

Thanks Paras. This worked for me.

Great! thanks

This is also found here: http://www.cocos2d-x.org/wiki/How_to_Build_an_Android_Project_with_Eclipse

Thank you!