Adding box2D source files to Android Studio project

Adding box2D source files to Android Studio project
0

For various reason I had to remove the prebuilt box2d files and replace them with the source files. My app modifies the engine slightly and the newer prebuilt version actually disables properties which I require access to. I have it working in XCode/iOS, but I can’t seem to figure out how to add it to the Android project. I created a cocos2d-x project from an older version that had the box2d source files and yet I can’t find any reference to box2d in the Android project. Inside the external/box2d folder there is an Android.mk file which seems to be responsible for adding the search paths to the project, but I don’t know how that .mk file is accessed by Android Studio. Does any know how I can add the .mk file to the project or some other way to get box2d in the Android project.

Depending of PROP_BUILD_TYPE in proj.android\gradle.properties you should add box2d files to different place.

PROP_BUILD_TYPE:

  • cmake - CMakeLists.txt
  • ndk-build - Android.mk
1 Like

If you’re using Cocos2d-x 3.17+, then you should use the CMake method of building your application, because it’ll save you from having to maintain different project files for each platform you’re targeting.

Assuming Box2D already has a CMakeLists.txt file to build it, then adding it to your own application is trivial. Open the CMakeLists.txt file in the root folder of your application, and add something like this:

set(box2d_INCLUDE_DIRS [path to box2D header files])
add_subdirectory([path to box2d cmakelists.txt] ${PROJECT_BINARY_DIR}/box2d)

Now, where you see the following line:
target_link_libraries(${APP_NAME} cocos2d)

add this after it:

target_include_directories(${APP_NAME}
	PRIVATE ${box2d_INCLUDE_DIRS}
)

target_link_libraries(${APP_NAME} box2d)
3 Likes

@R101 Thank you very much. This is very helpful. So when you build in Android Studio, a .gradle file uses CMake/CMakeLists.txt to add the dependencies. Is this correct?

Yes, there is a CMakeLists.txt for box2d. However, I’m using v3.17.0, and the following line is absent:

target_link_libraries(${APP_NAME} cocos2d)

I’m not really sure where to put the subsequent code.

@Apollo18 I strongly recommend you update to 3.17.2, because there are quite a lot of fixes in there, including improvements in how CMake is used to build the application.

If you can’t move from 3.17 for now, then the section you’re looking for is here:

cocos_build_app(${APP_NAME}
                APP_SRC "${APP_SRC}"
                DEPEND_COMMON_LIBS "cocos2d" "box2d"
                DEPEND_ANDROID_LIBS "cocos2d_android"
                )

If you’re unsure of how something works, then is there any reason you haven’t looked into what is currently there? Cocos2d-x uses a lot of different libraries, so they must be included in one of the CMakeLists.txt files. You can just search for CMakeLists.txt files in the cocos2d folder, and you would see many examples of how it is done. If you want to know what cocos_build_app() does, then search inside the cocos2d\cmake\*.cmake files, and you’re sure to learn a lot from it.

2 Likes

@R101 Again, I appreciate it. And your gentle nudge toward autonomy :slight_smile: I don’t have a great response except that I haven’t found a comprehensive overview of what is happening, which makes it hard to know where to look. Generally speaking the documentation explains how to do something, but rarely does it explain what is happening. Seeing as I didn’t have the CMake command line tools installed until two days ago, I wasn’t even sure if CMake actually had anything to do with my workflow (in a .gradle file there is a if (PROP_BUILD_TYPE == 'cmake') condition which led me to wonder if my project, for reasons I don’t yet know, doesn’t use CMake).

And since you strongly recommend updating to 3.17.2, is updating simply a matter of copying over an updated cocos2d_libs.xcodeproj file? [EDIT: I updated to 3.17.2 and replicated the changes I made to use the box2d source files and now I’m going to edit the CMakeLists.txt files as you indicated. Thanks]

I don’t actually know what the cocos2d_libs.xcodeproj file is, so I’m not entirely sure what you mean by that.

The upgrade steps that have worked for me are as follows:

  1. After setting up the new version of cocos2d-x, generate a new game project (let’s say ProjectA).
  2. Compare the files in ProjectA to your current project files, which includes AppDelegate.cpp/.h, CMakeLists.txt etc. Merge any changes that are required for the new cocos2d-x. If you use some form of source control, then this step becomes a bit easier.
  3. Follow the guide here on how to generate the project files for the specific platform you’re targeting. The only platform that doesn’t require this step is Android, where you simply set PROP_BUILD_TYPE=cmake in gradle.properties, and it will use your CMakeLists.txt for the build, instead of the Android*.mk files.

Aside from that, it’s probably best not to have a monolithic CMakeLists.txt file that contains references to all your source code, so you’re free to create a CMakeLists.txt file in each folder/module, and then include a reference to each of those files in the parent CMakeLists.txt, using include(path/to/folder/CMakeLists.txt.

1 Like

Ah, I followed the README.md which instructed me to use the cocos new command. This created a project with Android, iOS/Mac, Linux, and Windows project folders. Inside the XCode project is an embedded XCode project, cocos2d_libs.xcodeproj, which includes all the Cocos2d-x libraries. I guess the idea there is that you can more or less just update that embedded project to update Cocos2d-x. If it weren’t for my changes to Box2D, that would’ve likely worked.

Thanks for your help. I am getting a better understanding of the project structure and workflow.