Android Code Won't Compile with Box2D

I just checked out the latest commits from the gles20 branch. I created an iPhone (cocos2dx_box2d) and an Android projects and merged them together. The Box2D code was copied into the merged project.

The merged project compiles well with Xcode. However, when I use the build_native.sh to build android. It’ won’t compile.

I understand that something must be missing in either the Android.mk or the build_native.sh or both. But I couldn’t figure out how to fix it.

Here’s the log when I tried to build.

Using prebuilt externals
make: Entering directory `/Users/diwwu/Desktop/Projects/tws/tws/proj.android’
Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
Compile++ thumb : game_shared <= main.cpp
In file included from jni/helloworld/main.cpp:8:
jni/…/…/Classes/HelloWorldScene.h:13:19: error: Box2D.h: No such file or directory
In file included from jni/helloworld/main.cpp:8:

It cant find the directory where Box2D root folder is. You usually have to add the path to your Box2D root folder on your Android.mk file, ex:

LOCAL_C_INCLUDES := …/Classes/ /Users/joe/Code/Cocos2dX/Dev/cocos2d-1.0.1-x-0.13.0-beta/Box2D

Simple error, been discussed many times on some threads, search if this wont help you, there are other solutions and things you might try! Cheers!

Yes it’s all about the compiler couldn’t find the Box2D root. But it’s more complicated than fixing that one line in .mk file.

I looked at the past threads on the same issues but all of them are a little out of date, since the .mk and the build_native.sh have been changed a lot in recent Cocos2d-X commits.

What I did was just, copy the files from BOX2D folder into your classed folder root. That should do the trick. Its stupid but it works to compile it :slight_smile:

That works because your compiler is set to look for the classes folder. You just need to include the box2d root path into the LOCAL C INCLUDES.

If i remove my local c includes pointing to my box2d foler, i get the same error.

jni/…/…/Classes/Game_PhysicsController.h:12:19: error: Box2D.h: No such file or directory

I dont see how it is complicated. Even though mk changed, you still can point your local c includes on the android.mk even on the 13 beta version. Thats just a compiler setting telling the compiler to look for headers on those folders… and your error is just the compiler telling you, you did not point to him where those headers are located. Simple as that.

Let us know if your compiler still cant find the header.

Hi joe espindola,

I followed your method. Now that the compiler could find Box2D.h, but there are some new errors.

jni/…/…/libs/Box2D/Box2D.h:34:37: error: Box2D/Common/b2Settings.h: No such file or directory
jni/…/…/libs/Box2D/Box2D.h:35:33: error: Box2D/Common/b2Draw.h: No such file or directory
jni/…/…/libs/Box2D/Box2D.h:36:34: error: Box2D/Common/b2Timer.h: No such file or directory
jni/…/…/libs/Box2D/Box2D.h:38:50: error: Box2D/Collision/Shapes/b2CircleShape.h: No such file or directory

It seems that there’s something wrong with my folder structure, or the way those files are included in the Box2D.h

Everything is correct, that is the way the folder works in Box2D… what is missing, is that you should point also to the root of where the folders are included (this includes, cocos2dx, CocosDenshion, Box2D etc…)

As you see, box2d header file expects that you also point to the root folder of where Box2D root folder is, thus, the Box2D folder as the first folder.

Funny enough, cocos2dx default project creation should have already done that for you. Are you sure you used the latest create-android-project.sh to create your code ? (beta 13)

You could try and add the root folder as well to your LOCAL_C_INCLUDES, ex;

LOCAL_C_INCLUDES := …/Classes/ /Users/joe/Code/Cocos2dX/Dev/cocos2d-1.0.1-x-0.13.0-beta/Box2D /Users/joe/Code/Cocos2dX/Dev/cocos2d-1.0.1-x-0.13.0-beta/

Let me know if this works out!

Finally, I did exactly what joe espindola said and the problem solved. Thanksjoe espindola!

And actually there’s one gotcha thing here.

If I use the build_native.sh to build the original android project created inside the Cocos2d-X folder, it compiles well.

However, if I copied and merged the same android project inside the newly created iPhone project, it started reporting missing Box2D.h errors. @joe espindola ’s solution was able to fix that.

As it turns out, the cpp files (let’s call them files A) in the /Classes folder inside the newly created iPhone project are different from the cpp files (let’s call them files B) in the /Classes folder in the original android project.

Specifically, the gotcha thing is, files A has code like include “Box2D.h” while files B doesn’t. As a result, if I compile the code in the original android folder, it compiles well and no errors are found (because it doesn’t try to link Box2D). But if I compile the code in the newly created and merged iPhone project folder, “Box2D.h” is reported missing.

Cheers. :slight_smile:

Joe I did what you said but there is error appearing in android.mk that the missing separator. Stop…
My path is correct then what m i doing wrong?

That error is gone now the error is box2d.h no such file or directory…:frowning:

If you move the ‘proj.android’ folder to xcode project, your xcode project folder might be like below.

…*proj.android
…`— jni

…*libs
….`— Box2D
…*Classes

Try edit jni/Android.mk file like below


LOCAL_C_INCLUDES := $/…/…/Classes $/…/…/libs $/…/…/libs/Box2D
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES*= cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES = cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES
= box2d_static

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) $(call import-module,extensions)
$(call import-module,external/Box2D)

Thanks gramulho! Your fix worked for me. :slight_smile: