For those of you who have followed my Creating an iPhone and Android Cocos2d-x hybrid project tutorial please pay attention to the paths. When you see a path that references the libraries directory (the folder that contains the cocos2dx, Box2D and CocosDenshion folders) you need to change the path from

$(LOCAL_PATH)/../../../cocos2dx

to

$(LOCAL_PATH)/../../libs/cocos2dx

Notice that the last ../ has been replaced by libs/.

Creating the project

To create the project, fire up a Terminal, navigate to the Cocos2D-x directory and create a new Android project:

./create-android-project.sh

Of course if you are on Windows, you are probably going to use create-android-project.bat.

Give the project a package path such as:

org.cocos2dx.boxtest

Choose an Android version (Android 2.1-update1 works well enough, that should be option 5).

Finally, input the project name:

BoxTest

A new folder called BoxTest will now show up in the Cocos2D-x directory with the following contents:

Now, you are going to need some Box2D code in there. You can take that code by creating a new Box2D project using the wizard for iPhone or Win32 and copying over the files in the Classes folder. Make sure you also copy the image files in the Resources folder.

Updating the makefiles

There are three makefiles that we need to modify in order to get this running:

android/jni/Android.mk

In this file we are going to add Box2D makefile into the subdirs variable (line 5):

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3.  
  4. subdirs := $(addprefix $(LOCAL_PATH)/../../../,$(addsuffix /Android.mk, \
  5.            Box2D \
  6.            cocos2dx \
  7.            CocosDenshion/android \
  8.     ))
  9. subdirs += $(LOCAL_PATH)/helloworld/Android.mk
  10.  
  11. include $(subdirs)

android/jni/Application.mk

Add the box2d module (line 3):

  1. APP_STL := stlport_static
  2.  
  3. APP_MODULES := cocos2d cocosdenshion box2d game

android/jni/helloworld/Android.mk

Add the folder that contains the Box2D directory and the actual Box2D directory to the LOCAL_C_INCLUDES variable and add the library to the LOCAL_LDLIBS variable as well (lines 13, 14 and lines 18, 19 below). The GLESv1_CM is needed because the code autogenerated by the Cocos2D-x Box2D wizard directly uses some OpenGL specific methods.

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_MODULE := game
  4.  
  5. LOCAL_SRC_FILES := main.cpp \
  6. ../../../Classes/AppDelegate.cpp \
  7. ../../../Classes/HelloWorldScene.cpp
  8.  
  9. LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../cocos2dx \
  10.                     $(LOCAL_PATH)/../../../../cocos2dx/platform \
  11.                     $(LOCAL_PATH)/../../../../cocos2dx/include \
  12.                     $(LOCAL_PATH)/../../../../CocosDenshion/include \
  13.                     $(LOCAL_PATH)/../../../../ \
  14.                     $(LOCAL_PATH)/../../../../Box2D \
  15.                     $(LOCAL_PATH)/../../../Classes
  16.  
  17. LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../../armeabi) \
  18.                 -lGLESv1_CM \
  19.                 -lbox2d \
  20.                 -lcocos2d -llog -lcocosdenshion \
  21.                 -L$(call host-path, $(LOCAL_PATH)/../../../../cocos2dx/platform/third_party/android/libraries) -lcurl
  22.  
  23. include $(BUILD_SHARED_LIBRARY)

Running ./build_native in the android folder should now yield a successful compilation.

Updating the Java code

One more step before you can start working on your Angry Birds/Tiny Wings killer!

You need to tell the Android Java code to load your newly generated Box2D library as well, so open up the file below and add the relevant loadLibrary call.

android/src/org/cocos2dx/application/ApplicationDemo.java

static {
    System.loadLibrary("cocosdenshion");
    System.loadLibrary("box2d");
    System.loadLibrary("cocos2d");
    System.loadLibrary("MyGame");
}

That’s it! If you deploy the code to your device or virtual device you should now see the standard Box2D demo:

Share this article

Tagged on:         

20 thoughts on “Creating a Cocos2D-x Box2D Android project

  • November 24, 2011 at 2:34 am
    Permalink

    Yes! Thank you. Exactly what I needed to get the hybrid setup working!

    Reply
  • December 11, 2011 at 11:52 am
    Permalink

    I am still currently having problems with trying to compile a android project with Box2D.
    Here’s where the point where it’s unable to compile

    In file included from jni/../../Classes/AppDelegate.cpp:4:
    jni/../../Classes/HelloWorldScene.h:6:25: error: Box2D/Box2D.h: No such file or directory
    In file included from jni/../../Classes/AppDelegate.cpp:4:
    jni/../../Classes/HelloWorldScene.h:31: error: ISO C++ forbids declaration of ‘b2Body’ with no type
    jni/../../Classes/HelloWorldScene.h:31: error: expected ‘;’ before ‘*’ token
    jni/../../Classes/HelloWorldScene.h:32: error: ISO C++ forbids declaration of ‘b2World’ with no type
    jni/../../Classes/HelloWorldScene.h:32: error: expected ‘;’ before ‘*’ token
    /cygdrive/c/android-ndk-r7/build/core/build-binary.mk:241: recipe for target `obj/local/armeabi/objs-debug/game_logic/AppDelegate.o’ failed
    make: *** [obj/local/armeabi/objs-debug/game_logic/AppDelegate.o] Error 1
    make: Leaving directory `/cygdrive/c/cocos2d-1.0.1-x-0.10.0/test8/android’

    If anyone could help me out I’d greatly appreciate it

    Reply
    • February 3, 2012 at 1:04 pm
      Permalink


      jni/../../Classes/HelloWorldScene.h:6:25: error: Box2D/Box2D.h: No such file or directory
      In file included from jni/../../Classes/AppDelegate.cpp:4:
      jni/../../Classes/HelloWorldScene.h:31: error: ISO C++ forbids declaration of ‘b2Body’ with no type
      jni/../../Classes/HelloWorldScene.h:31: error: expected ‘;’ before ‘*’ token
      jni/../../Classes/HelloWorldScene.h:32: error: ISO C++ forbids declaration of ‘b2World’ with no type
      jni/../../Classes/HelloWorldScene.h:32: error: expected ‘;’ before ‘*’ token
      /cygdrive/c/android-ndk-r7/build/core/build-binary.mk:241: recipe for target `obj/local/armeabi/objs-debug/game_logic/AppDelegate.o’ failed
      make: *** [obj/local/armeabi/objs-debug/game_logic/AppDelegate.o] Error 1
      make: Leaving directory `/cygdrive/c/cocos2d-1.0.1-x-0.10.0/test8/android’

      I’ve got the same problem as yours!!!
      If not using Box2D, there would be no problem.
      Who could help us ??

      Reply
      • February 3, 2012 at 1:14 pm
        Permalink

        The issue is that the builder cannot find the Box2D library because either the path is wrong or it is not present on this.

        Make sure you copy it next to the cocos2dx folder inside your project if it is not already there (copy it from the cocos2dx zip file). If you followed my previous guide, the libs folder should have the following folders:
        – Box2D
        – cocos2dx
        – CocosDenshion

        Reply
        • March 1, 2012 at 11:09 am
          Permalink

          i’ve got the same problem, and folder Box2D is in the “libs” folder. And i’ve done all the things from this post. At least i think that ))

          Reply
  • February 8, 2012 at 8:08 pm
    Permalink

    Thanks for this tutorial. Could you also tell us what you write in the Android.mk file located in the Classes folder ? Thanks !

    Reply
  • February 8, 2012 at 9:07 pm
    Permalink

    And can you explain in more details the background of your remark relative to the paths ($(LOCAL_PATH)/../../libs/cocos2dx or $(LOCAL_PATH)/../../…/cocos2dx)? I am moreover not sure to understand which notation we should use to do this tutorial: should we copy/paste what you write in the tutorial or change “…” to “libs”? Thanks.

    Reply
    • February 8, 2012 at 9:32 pm
      Permalink

      Change to libs.

      Reply
  • February 8, 2012 at 10:04 pm
    Permalink

    I followed exactly your tutorial and I get the following error:
    SharedLibrary : libgame.so
    /Applications/Android/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lbox2d
    collect2: ld returned 1 exit status
    make: *** [obj/local/armeabi/libgame.so] Error 1

    Any idea from where it comes? Sorry for all my questions…

    Reply
    • February 8, 2012 at 10:10 pm
      Permalink

      Have you copied the Box2D folder next to the cocos2d folder in libs?

      Reply
      • August 8, 2012 at 12:44 pm
        Permalink

        Hi..
        i have copied the Box2D folder but still getting the same error… 🙁
        plz help me…
        thanks in advance

        Reply
        • August 8, 2012 at 12:48 pm
          Permalink

          /Users/temp/Desktop/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: cannot find -lbox2d
          /Users/temp/Desktop/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: cannot find -lcocos2d
          /Users/temp/Desktop/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: cannot find -lcocosdenshion
          /Users/temp/Desktop/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../../arm-linux-androideabi/bin/ld: cannot find -lcurl
          collect2: ld returned 1 exit status
          make: *** [obj/local/armeabi/libgame.so] Error 1

          Reply
  • November 30, 2012 at 9:35 am
    Permalink

    hi, i m getting this error when i run my app on eclipse for android “make: *** No rule to make target `jni_’/testcpp/main.cpp’, needed by `obj/local/armeabi/objs-debug/testcpp_shared/testcpp/main.o’. ”
    what’s wrong with my mk file?
    here is my mk file :

    TOP_LOCAL_PATH :=$(call my-dir)
    include $(call all-subdir-makefiles)
    LOCAL_PATH := $(TOP_LOCAL_PATH)

    include $(CLEAR_VARS)
    LOCAL_LDLIBS:=-llog
    LOCAL_MODULE := testcpp_shared
    LOCAL_MODULE_FILENAME := libtestcpp
    LOCAL_SRC_FILES := testcpp/main.cpp \
    carTest.cpp \
    test.cpp \
    ScreenLayer.cpp \
    track1.cpp \
    GameOverScene.cpp \
    MyContactListener.cpp \
    story.cpp \
    AdvanceSprite.cpp \
    Hello.cpp \

    LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

    LOCAL_STATIC_LIBRARIES := curl_static_prebuilt

    LOCAL_WHOLE_STATIC_LIBRARIES := cocos_testcpp_common
    LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
    LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
    LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
    LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
    LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static

    include $(BUILD_SHARED_LIBRARY)

    $(call import-module,samples/TestCpp)
    $(call import-module,cocos2dx)
    $(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
    $(call import-module,CocosDenshion/android)
    $(call import-module,extensions)
    $(call import-module,external/Box2D)
    $(call import-module,external/chipmunk)

    Reply
  • September 16, 2013 at 6:39 pm
    Permalink

    thanks a lot~!! this problem confusing me a few days~ thank you very much!!!

    Reply
  • December 17, 2013 at 6:50 pm
    Permalink

    Hi could you write an updated tutorial for cocos2d-x 2.2 ??? it is really becoming a pain 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Email
Print