The first time I came into contact with Cocos Creator small game development, I found that after building a project on the Android platform, I packaged it, and the compilation efficiency was very slow. It basically took 10-20 minutes. The computer was also rumbling, and the CPU and memory were working at full load. It is not complicated to create a new cocos creator project, why is the packaging so slow?
I remembered that I had been exposed to the cocos2dx c++ project before, and the packaging was also extremely slow. Combined with the cocos2dx engine and gradle project configuration, it is preliminarily estimated that the step of compiling the cocos2dx engine by ndk is the source of slow compilation.
Card point analysis
The sticking point in the packaging process is mainly that the compilation process of the cocos2dx basic library is slow. It needs to use ndk to fully compile the c++ code. The code is huge, so it takes up a lot of resources. However, there is no need to modify the cocos2dx basic library during game development, and the so files of each compilation product can be reused. The ndk compiled product so file can be used as a dependency for gradle packaging. According to this idea, the compilation of cocos2dx can be omitted, the compiled products can be reused, and the packaging efficiency of the game apk can be improved.
optimization steps
1. Perform a full compilation and extract the cocos2dx compilation product
First export the cocos creator project to the android studio project (as project).
Configure the target platform library that needs to be compiled in the as project:
Find the file in the root directory of the project gradle.properties
, modify PROP_APP_ABI
the properties, and generally configure it as
PROP_APP_ABI=arm64-v8a:x86_64
After compiling, the cocos2dx target so library of the arm64-v8a and x86_64 platforms can be generated, which can run on real machines and PC simulators respectively.
Run the gradle command to start a full compilation of the package:./gradlew assembleRelease
After the compilation is complete, find the compiled product of cocos2dx app/build/intermediates/merged_native_libs
below :
Copy the two folders in the red box above and copy them to the app/libs directory:
2. Modify the gradle configuration and remove the compilation of the cocos2dx library
In the build.gradle file of the app project, configure the libs directory in the previous step as a dependency of the jni library.
Specific operation: Add the following configuration jniLibs.srcDirs "libs" to the sourceSets.main element in android{}. Example:
sourceSets.main {
java.srcDirs "../src", "src"
res.srcDirs "../res", 'res'
jniLibs.srcDirs "../libs", 'libs'
manifest.srcFile "AndroidManifest.xml"
assets {
srcDirs 'assets', 'src/main/assets'
}
}
Next, comment the code block in the app project build.gradle
file :externalNativeBuild{...}
android{
defaultConfig {
//将以下代码块注释
externalNativeBuild {
ndkBuild {
if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
// skip the NDK Build step if PROP_NDK_MODE is none
targets 'cocos2djs'
arguments 'NDK_TOOLCHAIN_VERSION=clang'
def module_paths = [project.file("/Applications/CocosCreator/Creator/2.4.5/CocosCreator.app/Contents/Resources/cocos2d-x"),
project.file("/Applications/CocosCreator/Creator/2.4.5/CocosCreator.app/Contents/Resources/cocos2d-x/cocos"),
project.file("/Applications/CocosCreator/Creator/2.4.5/CocosCreator.app/Contents/Resources/cocos2d-x/external")]
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
arguments 'NDK_MODULE_PATH=' + module_paths.join(";")
}
else {
arguments 'NDK_MODULE_PATH=' + module_paths.join(':')
}
arguments '-j' + Runtime.runtime.availableProcessors()
abiFilters.addAll(PROP_APP_ABI.split(':').collect{
it as String})
}
}
}
}
}
Or add the following configuration to the gradle.properties file in the project root directory to skip the NDK compilation step:
PROP_NDK_MODE=none
So far, the time-consuming compilation of the cocos2dx library has been omitted.
Optimization effect
After the above optimization, the packaging time is basically controlled at about 30 seconds, which is a significant improvement in efficiency compared with the previous 10-20 minutes. The computer is not rumbling anymore. After modifying the cocos creator project code, package it again, and the modification will take effect, which fully shows that the compilation of cocos2dx is redundant.
Little knowledge: the cocos2d engine was originally developed in objective-c language, because it was acquired by Apple as a special engine for small games on the iphone platform, and it was also popularized by Apple. Later, the cocos2d c++ version engine was released in China and can be cross-compiled across platforms, and can run on linux, windows, mac and other systems, so it is called cocos2dx. The engine library file name compiled on cocos creator is libcocos2djs.so.