Integrating Box2D in an Android project

Yeah. You’re alright.
Now, I’m using the old Box2D (cocos2d-1.0.1-x-0.9.2.zip) and everything goes right away.

Thanks you all.
Together we’re a good team.

I thought i would add to this.
I decided to not edit the android.mk file inside …android\jni\helloworld

and my game still worked

I only edited a the Android.mk and application mk files inside …Android\jni
and Android.mk inside …/Classes

also note that if your going to add anymore cpp files to your game be sure to add the to the LOCAL_SRC_FILES list inside the Android.mk inside the …/Clases folder and… possibly inside the android\jni\helloworld if you have to.

example if you have to add another cpp called building.cpp then your Android.mk file inside your …/Classes folder would look like:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_logic

LOCAL_SRC_FILES := AppDelegate.cpp building.cpp HelloWorldScene.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/cocos2dx $(LOCAL_PATH)/…/…/ $(LOCAL_PATH)/…/…/box2d $(LOCAL_PATH)/…/…/cocos2dx/platform $(LOCAL_PATH)/…/…/cocos2dx/include $(LOCAL_PATH)/…/…/CocosDenshion/include $(LOCAL_PATH)/…/…/cocos2dx/lua_support

LOCAL_LDLIBS := ~~L$/…/android/libs/$) ~~lcocos2d ~~lcocosdenshion ~~L$(call host-path, $(LOCAL_PATH)/…/…/cocos2dx/platform/third_party/android/libraries/$(TARGET_ARCH_ABI)) -lcurl

include $(BUILD_SHARED_LIBRARY)

I did not edit the Android.mk file inside the …folder

I copy Box2D to ProjectFolder,then it can be made.
question is , How to do to not copy the Box2D folder.
the folder path:
cocos2d-x
cocos2d-x\Box2D
cocos2d-x.
cocos2d-x\SpaceGame -> project folder

the mk files
1. SpaceGame\android\jni\Android.mk
`LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

subdirs := $(addprefix $(LOCAL_PATH)/…/…/…/,$(addsuffix /Android.mk,
Box2D
cocos2dx
CocosDenshion/android
))
subdirs += $(LOCAL_PATH)/…/…/Classes/Android.mk $(LOCAL_PATH)/helloworld/Android.mk

include $(subdirs)`

2. SpaceGame\android\jni\Application.mk
`APP_STL := stlport_static

APP_MODULES := cocos2d box2d cocosdenshion game_logic helloworld`

3. SpaceGame\android\jni\helloworld\Android.mk
`LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := helloworld

LOCAL_SRC_FILES := main.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/…/…/cocos2dx
$(LOCAL_PATH)/…/…/…/…/cocos2dx/platform
$(LOCAL_PATH)/…/…/…/…/cocos2dx/include
$(LOCAL_PATH)/…/…/…/…/Box2D
$(LOCAL_PATH)/…/…/…/Classes

LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/…/…/libs/$(TARGET_ARCH_ABI))
-lGLESv1_CM -lcocos2d -lbox2d -llog -lgame_logic

include $(BUILD_SHARED_LIBRARY)`

  1. SpaceGame\Classes\Android.mk

`LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_logic

LOCAL_SRC_FILES := AppDelegate.cpp
HelloWorldScene.cpp
CCParallaxNodeExtras.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/cocos2dx
$(LOCAL_PATH)/…/…/cocos2dx/platform
$(LOCAL_PATH)/…/…/cocos2dx/include
$(LOCAL_PATH)/…/…/Box2D
$(LOCAL_PATH)/…/…/CocosDenshion/include
$(LOCAL_PATH)/…/…/cocos2dx/lua_support

LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/…/android/libs/$(TARGET_ARCH_ABI))
-lcocos2d -lbox2d -lcocosdenshion
-L$(call host-path, $(LOCAL_PATH)/…/…/cocos2dx/platform/third_party/android/libraries/$(TARGET_ARCH_ABI)) -lcurl

include $(BUILD_SHARED_LIBRARY)`

the error is HelloWorldScene.h:6:25: error: Box2D/Box2D.h: No such file or directory
the SpaceGame\android\libs\armeabilibbox2d.so,libcocos2d.so and libcocosdenshion.so is made

if I copy Box2D to SpaceGame\Classes\Box2D ,it worked well.
how to set,keep the Box2D folder is cocos2d-x\Box2D, not copy. and why…

for this file

  1. SpaceGame\android\jni\helloworld\Android.mk
    `LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE := helloworld

LOCAL_SRC_FILES := main.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/…/…/cocos2dx
$(LOCAL_PATH)/…/…/…/…/cocos2dx/platform
$(LOCAL_PATH)/…/…/…/…/cocos2dx/include
$(LOCAL_PATH)/…/…/…/…/Box2D
$(LOCAL_PATH)/…/…/…/Classes

LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/…/…/libs/$(TARGET_ARCH_ABI))
-lGLESv1_CM -lcocos2d -lbox2d -llog -lgame_logic

include $(BUILD_SHARED_LIBRARY)`

try changing the line:

$(LOCAL_PATH)/…/…/…/…/Box2D
to

$(LOCAL_PATH)/…/…/…/Box2D

there is one less “…/”

wahoo… it’s working.

thank you very much.

Hi everyone,

I succeeded to compile my box2d project with the build_native.sh under cygwin, thanks to the instructions you gave in this post. The make command execute without errors and compile cocos2d sources, box2d sources and my project’s sources.

I am now trying to test the program on an android device. I did like in the tutorial “how to create a cocos2d-x project for android”, with eclipse. The .apk file is created, but when i am testing it on a virtual device ( 2.2 android version or 2.3.3) it crashes at startup, and nothing shows up except a brief black screen. When I try a simple android project without box2d it is working fine.

My box2d project is also working on win32 and iphone, but I’m stuck with android.

Do you have some advice to give me to solve this problem?

Thanks

Have you ever tried in a real android device?

No I thought it has a little chance to work on a real device because it was not working on the simulator… I will try soon and answer you if it works.

However I like the simulator because I can check all android versions with it, I would like to test my application extensively before submitting it to the android market.
Do you know if there is some special includes or declaration to make in eclipse to make box2d work?

thanks

Did you update the main java file with the

@ System.loadLibrary(“box2d”);@

call?

did you change APP_STL in androind/jni/application.mk to APP_STL := gnustl_static ?

did you change APP_STL in androind/jni/application.mk to APP_STL := gnustl_static ?

thank you! it was this part that was missing to my project. Now it is working on the simulator.

Hello! Im totally confused with still getting “Box2D.h: No such file…”.

My jni/Android.mk :

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

subdirs := $(addprefix $(LOCAL_PATH)/…/…/libs/,$(addsuffix /Android.mk, Box2D cocos2dx CocosDenshion/android ))
subdirs = $/helloworld/Android.mk
include $
My jni/Application.mk :
APP_STL := stlport_static
APP_MODULES := cocos2d cocosdenshion box2d game
My jni/helloworld/Android.mk :
LOCAL_PATH := $
include $
LOCAL_MODULE := game
LOCAL_SRC_FILES := main.cpp …/…/…/Classes/AppDelegate.cpp …/…/…/Classes/HelloWorldScene.cpp
LOCAL_C_INCLUDES := $/…/…/…/libs/cocos2dx $/…/…/…/libs/cocos2dx/platform $/…/…/…/libs/cocos2dx/include $/…/…/…/libs/CocosDenshion/include $/…/…/…/libs $/…/…/…/libs/Box2D $/…/…/…/Classes
# it is used for ndk-r5
# if you build with ndk-r4, comment it
# because the new Windows toolchain doesn’t support Cygwin’s drive
# mapping
LOCAL_LDLIBS := ~~L$/…/…/libs/armeabi) ~~lGLESv1_CM ~~lbox2d ~~lcocos2d lloglcocosdenshion ~~L$/…/…/…/libs/cocos2dx/platform/third_party/android/libraries)~~lcurl
include $
My .java file has at the end :
static {
System.loadLibrary;
System.loadLibrary;
System.loadLibrary;
System.loadLibrary;
}
Box2D folder is in libs folder next to cocos2dx folder in my project. And after building there is libbox2d.so in libs/armeabi folder.
But when it starts to build my application i get error:
Compile
+ thumb : game <= AppDelegate.cpp
In file included from jni/helloworld/…/…/…/Classes/AppDelegate.cpp:12:
jni/helloworld/…/…/…/Classes/HelloWorldScene.h:13:19: error: Box2D.h: No such file or directory

Definetly my mistake right in front of my eyes and i can not see it :slight_smile:

my jni/android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

subdirs := $(addprefix $(LOCAL_PATH)/../../../,$(addsuffix /Android.mk, \
        Box2D \
        chipmunk \
          cocos2dx \
          CocosDenshion/android \
          ))

subdirs += $(LOCAL_PATH)/../../Classes/Android.mk $(LOCAL_PATH)/helloworld/Android.mk

include $(subdirs)

jni/application.mk:

APP_STL := gnustl_static
APP_CPPFLAGS += -frtti

APP_MODULES := cocos2d cocosdenshion chipmunk box2d game_logic game

jni/helloworld/android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game

LOCAL_SRC_FILES := main.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../cocos2dx \
                    $(LOCAL_PATH)/../../../../cocos2dx/platform \
                    $(LOCAL_PATH)/../../../../cocos2dx/include \
                    $(LOCAL_PATH)/../../../Classes \
                    $(LOCAL_PATH)/../../../../ 


LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/$(TARGET_ARCH_ABI)) \
                -lGLESv1_CM \
                -lcocos2d -llog -lgame_logic

include $(BUILD_SHARED_LIBRARY)

classes/android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_logic

LOCAL_SRC_FILES := AppDelegate.cpp \
                   HelloWorldScene.cpp \
                   UserInterfaceLayer.cpp \
                   Person.cpp \
                   bullet.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../cocos2dx \
                    $(LOCAL_PATH)/../../ \
                    $(LOCAL_PATH)/../../cocos2dx/platform \
                    $(LOCAL_PATH)/../../cocos2dx/include \
                    $(LOCAL_PATH)/../../CocosDenshion/include \
                    $(LOCAL_PATH)/../../cocos2dx/lua_support 

LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../android/libs/$(TARGET_ARCH_ABI)) \
                -lcocos2d -lbox2d -lcocosdenshion \
                -L$(call host-path, $(LOCAL_PATH)/../../cocos2dx/platform/third_party/android/libraries/$(TARGET_ARCH_ABI)) -lcurl

include $(BUILD_SHARED_LIBRARY)

going off to classes now, be back in about 8 hours, i’ll try to help if you will need it by then :slight_smile: the problem should have something to do with the directories, check the file paths, /…/…/ means two levels higher from the folder in which the file that you are reading is in.

hi, this is my cygwin compilation error
need help.

$ ./build_native.sh
make: Entering directory `/cygdrive/c/Development/cocos2d-x12/jp/android'
Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
Install        : libcocosdenshion.so => libs/armeabi/libcocosdenshion.so
Compile++ thumb  : game_logic_static <= GameScene.cpp
Compile++ thumb  : game_logic_static <= SettingScene.cpp
Compile++ thumb  : game_logic_static <= SplashScene.cpp
StaticLibrary  : libgame_logic.a
SharedLibrary  : libgame.so
./obj/local/armeabi/libgame_logic.a(GameScene.o): In function `b2CircleShape':
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/../../Box2D/Collision/Shapes/b2CircleShape.h:70: undefined reference to `vtable for b2CircleShape'
./obj/local/armeabi/libgame_logic.a(GameScene.o): In function `b2PolygonShape':
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/../../Box2D/Collision/Shapes/b2PolygonShape.h:87: undefined reference to `vtable for b2PolygonShape'
./obj/local/armeabi/libgame_logic.a(GameScene.o): In function `~b2PolygonShape':
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/../../Box2D/Collision/Shapes/b2PolygonShape.h:29: undefined reference to `vtable for b2PolygonShape'
./obj/local/armeabi/libgame_logic.a(GameScene.o): In function `GameScene::init()':
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:154: undefined reference to `b2World::b2World(b2Vec2 const&)'
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:155: undefined reference to `b2World::SetAllowSleeping(bool)'
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:161: undefined reference to `b2World::CreateBody(b2BodyDef const*)'
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:167: undefined reference to `b2PolygonShape::SetAsBox(float, float, b2Vec2 const&, float)'
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:168: undefined reference to `b2Body::CreateFixture(b2Shape const*, float)'
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:170: undefined reference to `b2PolygonShape::SetAsBox(float, float, b2Vec2 const&, float)'
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:171: undefined reference to `b2Body::CreateFixture(b2Shape const*, float)'
./obj/local/armeabi/libgame_logic.a(GameScene.o): In function `~b2CircleShape':
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/../../Box2D/Collision/Shapes/b2CircleShape.h:26: undefined reference to `vtable for b2CircleShape'
./obj/local/armeabi/libgame_logic.a(GameScene.o): In function `GameScene::gameLogic(float)':
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:315: undefined reference to `b2World::CreateBody(b2BodyDef const*)'
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:331: undefined reference to `b2Body::CreateFixture(b2FixtureDef const*)'
./obj/local/armeabi/libgame_logic.a(GameScene.o): In function `GameScene::gameLogic3(float)':
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:357: undefined reference to `b2World::Step(float, int, int)'
./obj/local/armeabi/libgame_logic.a(GameScene.o): In function `GameScene::onExit()':
C:\Development\cocos2d-x12\jp\android/jni/../../Classes/GameScene.cpp:538: undefined reference to `b2World::~b2World()'
./obj/local/armeabi/libgame_logic.a(SplashScene.o): In function `SplashScene::callNext(float)':
SplashScene.cpp:(.text._ZN11SplashScene8callNextEf+0xe): undefined reference to `HomeScene::scene()'
collect2: ld returned 1 exit status
/cygdrive/c/Development/android-ndk-r7b/build/core/build-binary.mk:314: recipe for target `obj/local/armeabi/libgame.so' failed
make: *** [obj/local/armeabi/libgame.so] Error 1
make: Leaving directory `/cygdrive/c/Development/cocos2d-x12/jp/android'

the code is working fine on other platform (marmalade)
don’t know the cause of error here.

thanks

Looks like you haven’t referenced box2d properly in one of the mk files

hi, thanks for the reply. I rechecked all *.mk files.
din’t found anything missing.
so, I am attaching those files.

android-support-v4.jar download (eclipse extension at add compatibility layer )

SharedLibrary : libgame_logic.so
./obj/local/armeabi/objs-debug/game_logic/AppDelegate.o: In function `AppDelegate::applicationDidFinishLaunching()‘:
C:2d-1.0.1-x-0.10.0mk\TestMT\android/jni/…/…/Classes/AppDelegate.cpp:116: undefined reference to `AnimationManager::initAnimationMap()’
C:2d-1.0.1-x-0.10.0mk\TestMT\android/jni/…/…/Classes/AppDelegate.cpp:118: undefined reference to `GameScene::playNewGame()‘
./obj/local/armeabi/objs-debug/game_logic/AppDelegate.o: In function `Singleton::instance()’:
C:2d-1.0.1-x-0.10.0mk\TestMT\android/jni/…/…/Classes/util/Singleton.h:22: undefined reference to `AnimationManager::AnimationManager()‘
C:2d-1.0.1-x-0.10.0mk\TestMT\android/jni/…/…/Classes/util/Singleton.h:24: undefined reference to `Singleton::_instance’
collect2: ld returned 1 exit status
/cygdrive/d/programe/android/ndk/android-ndk-r7/build/core/build-binary.mk:312: recipe for target `obj/local/armeabi/libgame_logic.so’ failed
make: * [obj/local/armeabi/libgame_logic.so] Erro

HELP!

Hello,kanrinin_kanmasato(hirokazu_ono).
I ported cocos2d-x’s “chipmunk” & “BOX2D” to the Android Apps(applications) as to work.
Prease also see this url of git site below.

Please ,

do something relate to this …. if it’s possible … :slight_smile: