Cocos2d-x游戏导出android工程,提取cocos的so文件

原本cocos游戏的android工程编译时,需要将cocos的库文件进行编译,这些文件大部分是cpp文件,

使用ndk-build工具编译cpp文件非常慢,而且非常耗cpu,大概需要10-20分钟才能编译完成,这个还是因为我换了固态硬盘后的效果,没固态硬盘可能就gg

这个等待时间可能是一个不错的水群时间,或者上去看看新闻,有哪个好看的,好玩的。

但事与愿违啊!

电脑编译这个工程已经占据的80%cpu,这叫人怎么办啊,凉凉,只能慢慢等待,然而后面还有一大堆事情等着我们去处理,看来加班时间又得延长~ 。

众里寻它千百度,想的又想,都是一个cocos软件导出来的cocos库,怎么就不可以共用呢,觉得没道理啊。唯一不同的应该就是js文件吧

先来个正常导出工程截图

这是jsb-default文件夹内的文件。js backups(useful for debugging)这个是js源码文件,没什么用处

A .游戏资源

jsb-adapter,

res

src

main.js

project.json

这五个文件是不同游戏的不同js文件,有游戏的js脚本,res有游戏图片资源,main.js应该是游戏的主入口,负责加载资源及脚本

看完游戏资源,再看下frameworksw文件夹

一个是cocos2d-x库文件,还有一个runtime-src这里放着各个平台的工程文件还有一个classes文件夹

B. 游戏需要的Classes文件

有三个平台,android,mac,win

只看android工程文件proj.android-studio

可以使用android studio打开这个工程

打开build.gradle文件,

externalNativeBuild是用来配置生成so包的cpu架构

复制代码

复制代码

// 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'

这个targets参数,指定哪个native库用来构建和打包,如果没有配置这个属性,gradle会 将所有native库进行构建和打包,

猜测应该是调用cocos2djs这个底层库进行编译

arguments是gradle配置ndk参数的方法,用来设置ndk-build打包时的参数

最重要的是abiFilters这个过滤,规定需要打包的CPU架构,

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

PROP_APP_ABI属性是从Android工程的gradle.properties文件中获取到了

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

 表示以:分割,

这样编译后会出来三个架构的so文件

C,获得so文件

我们想要的so文件就出来了,只要编译出来的游戏可以正常运行,我们就可以提取这个so文件。

note:需要提取的是ndkBuild-》release->...->armeabi-v7a中的so文件,上面截图中的是调式的so

现在我们需要的资源都已经准备好的,可以进行工程的组合

,保险起见,我们新建一个文件夹进行android工程简化。

比如一个篮球游戏,新建basketball文件夹

将as工程所有文件复制到basketball中,注意,红框中的文件不需要修改,

一. 修改app下的文件,将上面标注的A js资源文件放到assets目录中

二. 将C中获得的so文件放到libs包对应cpu架构下面

还有需要将cocos依赖的jar包也放到lib下面

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

这四个jar包放到我们app的libs文件下

三。 添加额外的java 的cocos库文件

 需要将libs源码文件提取出来放到我们的app/src/main/java中

目录路径

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

复制到src.main.java

 四. 修改app/build.gradle文件

主要就是删除掉打包编译so文件的代码,去掉无用的依赖库

修改前

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')
}

 修改后

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')
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐