Cocos2d-x game exports android project and extracts so file of cocos

Posted by liquidmind on Wed, 08 Sep 2021 07:41:03 +0200

Cocos2d-x game exports android project and extracts so file of cocos

When compiling the android project of the original cocos game, you need to compile the library files of Cocos. Most of these files are cpp files,

Compiling cpp files using NDK build tool is very slow and cpu consuming, and it takes about 10-20 minutes to complete the compilation. This is because I changed the effect of solid-state disk. Without solid-state disk, it may be gg

This waiting time may be a good water group time, or go up to see the news, which is good-looking and fun.

But it backfired!

The computer compilation project has occupied 80% of the CPU. What should we do? It's cool. We can only wait slowly. However, there are a lot of things waiting for us to deal with. It seems that the overtime has to be extended ~.

Many people look for it and think about it. It's a cocos library derived from cocos software. Why can't they share it? I don't think it makes sense. The only difference should be the js file

Let's take a screenshot of the normal export project first

This is the file in the JSB default folder. js backups (useful for debugging) this is a js source file, which is useless

A. game resources

jsb-adapter´╝î

res

src

main.js

project.json

These five files are different JS files of different games. There are JS scripts of the game, and res has game picture resources. main.js should be the main entrance of the game and be responsible for loading resources and scripts

After reading the game resources, look at the frameworksw folder

One is the cocos2d-x library file, the other is the runtime SRC, where the project files of each platform and the classes folder are placed

B.   Classes files required for the game

There are three platforms, android, mac and win

Just look at the Android project file proj.android-studio

You can use android   studio opens the project

Open the build.gradle file,

External native build is used to configure the cpu architecture for generating so packages

// Specifies which native libraries to build and package for this
          // product flavor. If you don't configure this property, Gradle
          // builds and packages all shared object libraries that you define
          // in your CMake or ndk-build project.
targets 'cocos2djs'

The targets parameter specifies which native library is used to build and package. If this attribute is not configured, gradle will   Build and package all native libraries,

The guess is to call the underlying library cocos2djs to compile

arguments is the method that gradle uses to configure ndk parameters. It is used to set the parameters when ndk build is packaged

The most important is abiFilters, which specifies the CPU architecture to be packaged,

abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String})

PROP_ APP_ The ABI property is obtained from the gradle.properties file of the Android project

PROP_TARGET_SDK_VERSION=19
PROP_APP_ABI=armeabi-v7a:x86:arm64-v8a

  Represented by: split,

In this way, the so files of three architectures will be compiled

C. Get so file

The so file we want comes out. As long as the compiled game can run normally, we can extract this so file.

note: what needs to be extracted is the so file in ndkBuild - < release - >... - > armeabi-v7a. The above screenshot shows the modal so

Now the resources we need are ready for the combination of projects

To be on the safe side, we will create a new folder to simplify the android project.

For example, for a basketball game, create a new basketball folder

Copy all files of the as project to the basketball. Note that the files in the red box do not need to be modified,

1, Modify the file under the app and change the A marked above   js resource files are placed in the assets directory

II   Put the so file obtained in C under the cpu architecture corresponding to the libs package

Also, you need to put the jar package that cocos depends on under lib

D:\android-workspace\frameworks\cocos2d-x\cocos\platform\android\java\libs

Put these four jar packages under the libs file of our app

Three.   Add additional java   cocos library file

  You need to extract the libs source file and put it in our app/src/main/java

Directory path

D:\android-workspace\frameworks\cocos2d-x\cocos\platform\android\java\src\org\cocos2dx\lib

Copy to src.main.java

  IV   Modify the app/build.gradle file

The main thing is to delete the code of packaging and compiling so files and remove useless dependency libraries

Before modification

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
    buildToolsVersion PROP_BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId "com.uelink.JewelBlockPuzzle"
        minSdkVersion PROP_MIN_SDK_VERSION
        targetSdkVersion PROP_TARGET_SDK_VERSION
        versionCode 1
        versionName "1.0"

        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=4.9'

                    def module_paths = [project.file("../../../cocos2d-x"),
                                        project.file("../../../cocos2d-x/cocos"),
                                        project.file("../../../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})
                }
            }
        }
    }

    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        jniLibs.srcDir "libs"
        manifest.srcFile "AndroidManifest.xml"
    }

    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
                path "jni/Android.mk"
            }
        }
    }

    signingConfigs {

       release {
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                storeFile file(RELEASE_STORE_FILE)
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                signingConfig signingConfigs.release
            }

            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=0'
                }
            }
        }

        debug {
            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=1'
                }
            }
        }
    }
}

android.applicationVariants.all { variant ->
    // delete previous files first
    delete "${buildDir}/intermediates/assets/${variant.dirName}"

    variant.mergeAssets.doLast {
        copy {
           from "${buildDir}/../../../../../res"
           into "${buildDir}/intermediates/assets/${variant.dirName}/res"
        }

        copy {
            from "${buildDir}/../../../../../src"
            into "${buildDir}/intermediates/assets/${variant.dirName}/src"
        }

        copy {
            from "${buildDir}/../../../../../jsb-adapter"
            into "${buildDir}/intermediates/assets/${variant.dirName}/jsb-adapter"
        }

        copy {
            from "${buildDir}/../../../../../main.js"
            from "${buildDir}/../../../../../project.json"
            into "${buildDir}/intermediates/assets/${variant.dirName}"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation fileTree(dir: "../../../cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])
    implementation project(':libcocos2dx')
}

  After modification

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
    buildToolsVersion PROP_BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId "com.uelink.JewelBlockPuzzle"
        minSdkVersion PROP_MIN_SDK_VERSION
        targetSdkVersion PROP_TARGET_SDK_VERSION
        versionCode 1
        versionName "1.0"

        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=4.9'

                    def module_paths = [project.file("../../../cocos2d-x"),
                                        project.file("../../../cocos2d-x/cocos"),
                                        project.file("../../../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})
                }
            }
        }
    }

    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        jniLibs.srcDir "libs"
        manifest.srcFile "AndroidManifest.xml"
    }

    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
                path "jni/Android.mk"
            }
        }
    }

    signingConfigs {

       release {
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                storeFile file(RELEASE_STORE_FILE)
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                signingConfig signingConfigs.release
            }

            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=0'
                }
            }
        }

        debug {
            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=1'
                }
            }
        }
    }
}

android.applicationVariants.all { variant ->
    // delete previous files first
    delete "${buildDir}/intermediates/assets/${variant.dirName}"

    variant.mergeAssets.doLast {
        copy {
           from "${buildDir}/../../../../../res"
           into "${buildDir}/intermediates/assets/${variant.dirName}/res"
        }

        copy {
            from "${buildDir}/../../../../../src"
            into "${buildDir}/intermediates/assets/${variant.dirName}/src"
        }

        copy {
            from "${buildDir}/../../../../../jsb-adapter"
            into "${buildDir}/intermediates/assets/${variant.dirName}/jsb-adapter"
        }

        copy {
            from "${buildDir}/../../../../../main.js"
            from "${buildDir}/../../../../../project.json"
            into "${buildDir}/intermediates/assets/${variant.dirName}"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation fileTree(dir: "../../../cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])
    implementation project(':libcocos2dx')
}

Topics: cocos2d