The latest cocos2d-x comes with a new gen-libs
command,which I think is really useful
What this command will do is to generate a prebuilt lib file for cocos2d-x and increase your productivity immensely by cut compile time into seconds instead of 10+ minutes.
To make everyone’s life easier, I created a repo with my sample projects here.
You can download it and skip the following steps if you just want to try it out.
And this is how you use it for your own project
Note
- the following method only works with 3.8 version and later
- This tutorial only covers ios and android, will update on how to use it with windows projects later
###1. Generate cocos libs
Simply run gen-libs
script, and it will generate prebuilt libs under prebuilt
folder
cocos gen-libs -m debug
I choose to generate debug version because release version will not output CCLOG
it will make debugging much harder
###2. Create project
Create a new project so you can try it out.
cocos new test -l cpp
And delete cocos2d
folder, because you don’t need them.
I recommend to put this project into a git repo at this point
###3. Modify Android project
There are two files you need to modify here
proj.android/build-cfg.json
proj.android/jni/Android.mk
Here are the samples:
###build-cfg.son
Remember to replace <COCOS2D-X_ROOT>
with your cocos2d-x root folder
{
"ndk_module_path" :[
"<COCOS2D-X_ROOT>",
"<COCOS2D-X_ROOT>/cocos/prebuilt-mk",
"<COCOS2D-X_ROOT>/cocos",
"<COCOS2D-X_ROOT>/external"
],
"copy_resources": [
{
"from": "../Resources",
"to": ""
}
]
}
###Android.mk
Replace
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)
with
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos/prebuilt-mk)
In the end, your Android.mk
should look like something like this.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos/prebuilt-mk)
LOCAL_MODULE := cocos2dcpp_shared
LOCAL_MODULE_FILENAME := libcocos2dcpp
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2dx_static
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END
Q&A
If you have problem building android projects, try the following
####project.properties
Make sure to reference the libcocos2dx project in project.properties
android.library.reference.1=<COCOS2D_X_ROOT>/cocos/platform/android/java
###4. Modify iOS project
You need to do the following
-
drag&drop
libcocos2d iOS.a
to your project, and make sure you link with it for your iOS project
-
Update
Header Search Paths
so all the header file points to the ones in your cocos2d-x root directory
And you are done, try it out and now your build time will be in seconds. Just keep in mind you’ll have to regenerate the prebuilt libs once you change any code in the engine.
I think this is the hidden gem in 3.8 version.
Feel free to ask me any questions.