一个是cocos2d-x库文件,还有一个runtime-src这里放着各个平台的工程文件还有一个classes文件夹
B. 游戏需要的Classes文件
有三个平台,android,mac,win
只看android工程文件proj.android-studio
可以使用android studio打开这个工程
打开build.gradle文件,
externalNativeBuild是用来配置生成so包的cpu架构
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}) } } } }
// 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-workspaceframeworkscocos2d-xcocosplatformandroidjavalibs
这四个jar包放到我们app的libs文件下
三。 添加额外的java 的cocos库文件
需要将libs源码文件提取出来放到我们的app/src/main/java中
目录路径
D:android-workspaceframeworkscocos2d-xcocosplatformandroidjavasrcorgcocos2dxlib
复制到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" } sourceSets.main { java.srcDir "src" res.srcDir "res" jniLibs.srcDir "libs" manifest.srcFile "AndroidManifest.xml" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' externalNativeBuild { ndkBuild { arguments 'NDK_DEBUG=0' } } } debug { externalNativeBuild { ndkBuild { arguments 'NDK_DEBUG=1' } } } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) }
可能还需要修改下java文件包名,修改写报错,程序就可以正常运行了。