背景
公司,想優(yōu)化掉,在app中,以webview方式,加載游戲的方式。以安卓項(xiàng)目為例,改成:游戲項(xiàng)目導(dǎo)出安卓工程,可直接使用的aar資源。
第一步:cocos項(xiàng)目,構(gòu)建安卓工程
安裝配置原生開發(fā)環(huán)境,可以參考官方文檔:https://docs.cocos.com/creator/2.4/manual/zh/publish/setup-native-development.html
備注:本人使用cocoscreator版本為:2.4.11版本,在構(gòu)建發(fā)布頁面,選擇Android選項(xiàng),構(gòu)建成功后,不要點(diǎn)擊后面的編譯、運(yùn)行按鈕。直接用androidstudio打開構(gòu)建好的安卓工程即可。
第二步:構(gòu)建成功的安卓工程,導(dǎo)出aar資源
1、將CocosCreator導(dǎo)出的安卓工程,用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不能設(shè)置這個參數(shù)
//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后會運(yùn)行會報(bào)沖突
// 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。截圖如下:
第三步:其他項(xiàng)目組安卓工程,導(dǎo)入游戲aar資源,并加載呈現(xiàn)游戲
文章來源:http://www.zghlxwxcb.cn/news/detail-447921.html
至此,結(jié)束。文章來源地址http://www.zghlxwxcb.cn/news/detail-447921.html
到了這里,關(guān)于cocos2dx游戲項(xiàng)目,集成到其他安卓項(xiàng)目工程之中!的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!