背景
公司,想优化掉,在app中,以webview方式,加载游戏的方式。以安卓项目为例,改成:游戏项目导出安卓工程,可直接使用的aar资源。
第一步:cocos项目,构建安卓工程
安装配置原生开发环境,可以参考官方文档:https://docs.cocos.com/creator/2.4/manual/zh/publish/setup-native-development.html
备注:本人使用cocoscreator版本为:2.4.11版本,在构建发布页面,选择Android选项,构建成功后,不要点击后面的编译、运行按钮。直接用androidstudio打开构建好的安卓工程即可。
第二步:构建成功的安卓工程,导出aar资源
1、将CocosCreator导出的安卓工程,用AndroidStudio打开,并修改JDK版本。
2、将依赖库工程libcocos2dx打包成aar
3、修改instantapp工程下的build.gradle、AndroidManifest.xml文件
3.1、build.gradle文件修改后如下:
import org.apache.tools.ant.taskdefs.condition.Os
//apply plugin: 'com.android.application'
// 将工程作为library
apply plugin: 'com.android.library'
android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
buildToolsVersion PROP_BUILD_TOOLS_VERSION
defaultConfig {
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=clang'
def module_paths = [project.file("/Applications/Creator/2.4.11/CocosCreator.app/Contents/Resources/cocos2d-x"),
project.file("/Applications/Creator/2.4.11/CocosCreator.app/Contents/Resources/cocos2d-x/cocos"),
project.file("/Applications/Creator/2.4.11/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()
}
}
ndk {
abiFilters PROP_APP_ABI.split(':')
}
}
}
sourceSets.main {
java.srcDirs "../src", "src"
res.srcDirs "../res", 'res'
jniLibs.srcDirs "../libs", '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 {
debuggable false
jniDebuggable false
renderscriptDebuggable false
minifyEnabled true
//作为library不能设置这个参数
//shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
if (project.hasProperty("RELEASE_STORE_FILE")) {
signingConfig signingConfigs.release
}
externalNativeBuild {
ndkBuild {
arguments 'NDK_DEBUG=0'
}
}
}
debug {
debuggable true
jniDebuggable true
renderscriptDebuggable true
externalNativeBuild {
ndkBuild {
arguments 'NDK_DEBUG=1'
}
}
}
}
}
//将applicationVariants,改成libraryVariants
android.libraryVariants.all { variant ->
// delete previous files first
delete "${buildDir}/intermediates/merged_assets/"
variant.mergeAssets.doLast {
def sourceDir = "${buildDir}/../../../../.."
copy {
from "${sourceDir}"
include "assets/**"
include "src/**"
include "jsb-adapter/**"
into outputDir
}
copy {
from "${sourceDir}/main.js"
from "${sourceDir}/project.json"
into outputDir
}
}
}
dependencies {
//注释掉这些依赖,不然打成aar后会运行会报冲突
// implementation fileTree(dir: '../libs', include: ['*.jar','*.aar'])
// implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
// implementation fileTree(dir: "/Applications/Creator/2.4.11/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])
implementation project(':libcocos2dx')
}
3.2、AndroidManifest.xml文件修改如下:
3.3、同步gradle,然后将工程打包成aar。截图如下:
第三步:其他项目组安卓工程,导入游戏aar资源,并加载呈现游戏
至此,结束。