將cocos項目構(gòu)建為Android工程并構(gòu)建出對應(yīng)的aar
構(gòu)建Android工程
1.配置原生開發(fā)環(huán)境(ndk路徑隶债、Android sdk路徑)
2.打開構(gòu)建發(fā)布窗口進行如下配置
將cocos Android工程構(gòu)建為aar
1.將Cocos Creator導(dǎo)出的Android工程(../build/jsb-default/frameworks/runtime-src/proj.android-studio)用Android Studio打開并編譯秆剪,成功編譯并正常運行app工程后進入第2步滞磺。
2.將依賴庫工程libcocos2dx打成aar菩混,build完成后aar的位置為../build/jsb-default/frameworks/cocos2d-x/cocos/platform/android/libcocos2dx/build/outputs/aar/libcocos2dx-release.aar
3 修改game工程的build.gradle鸯两、AndroidManifest.xml文件
build.gradle這些代碼需要修改:
import org.apache.tools.ant.taskdefs.condition.Os
// 將game工程作為library宣吱,這個feature應(yīng)該是什么instantApp的配置
// apply plugin: 'com.android.feature'
apply plugin: 'com.android.library'
android {
//注掉這里
//baseFeature true
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("../../../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.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'
}
}
}
}
}
//將featureVariants改成libraryVariants
android.libraryVariants.all { variant ->
// delete previous files first
delete "${buildDir}/intermediates/merged_assets/"
variant.mergeAssets.doLast {
def sourceDir = "${buildDir}/../../../../.."
copy {
from "${sourceDir}/assets"
into "${outputDir}/assets"
}
copy {
from "${sourceDir}/src"
into "${outputDir}/src"
}
copy {
from "${sourceDir}/jsb-adapter"
into "${outputDir}/jsb-adapter"
}
copy {
from "${sourceDir}/main.js"
from "${sourceDir}/project.json"
into outputDir
}
}
}
/*android.featureVariants.all { variant ->
// delete previous files first
delete "${buildDir}/intermediates/merged_assets/"
variant.mergeAssets.doLast {
def sourceDir = "${buildDir}/../../../../.."
copy {
from "${sourceDir}/assets"
into "${outputDir}/assets"
}
copy {
from "${sourceDir}/src"
into "${outputDir}/src"
}
copy {
from "${sourceDir}/jsb-adapter"
into "${outputDir}/jsb-adapter"
}
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: "../../../cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])*/
implementation 'com.google.android.instantapps:instantapps:1.1.0'
implementation 'com.google.android.gms:play-services-instantapps:16.0.0'
implementation project(':libcocos2dx')
}
找到game工程的AndroidManifest.xml注釋掉啟動頁標(biāo)簽(因為我們不希望我們的工程運行起來就直接進入游戲)
<activity
android:name="org.cocos2dx.javascript.AppActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"
android:label="@string/app_name"
android:launchMode="singleTask"
android:taskAffinity=""
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter android:order="1">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!--<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>-->
</activity>
4.第3步完成后同步gradle然后將工程game打成aar明未,build完成后aar的位置為../build/jsb-default/frameworks/runtime-src/proj.android-studio/game/build/outputs/aar/game-release.aar
集成到Android工程
1.將拿到的libcocos2dx-release.aar和game-release.aar放入我們自己的工程libs文件夾下
并在工程的build.gradle依賴它們槽华、同步
2.創(chuàng)建我們自己的游戲Activity繼承自aar包里面的AppActivity
package com.example.myapplication;
import org.cocos2dx.javascript.AppActivity;
public class MyAppActivity extends AppActivity {
}
在manifest里注冊這個Activity
<activity
android:name=".MyAppActivity"
android:launchMode="singleInstance"
android:taskAffinity=":MyAppActivity"
android:process=":MyAppActivity"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<!--<intent-filter android:order="1">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>-->
</activity>
這里注意一下,由于cocos引擎有自己的資源釋放策略(它會在Activity結(jié)束后干掉所在進程)趟妥,所以為了不影響我們自己工程的進程必須將Activity放到另一個進程猫态。另外,我這里將這個Activity放入了新任務(wù)棧披摄,因為AppActivity亲雪、Cocos2dxActivity有些地方會去調(diào)用isTaskRoot,如果不把Activity放入新棧會導(dǎo)致代碼執(zhí)行不正確疚膊;當(dāng)然义辕,你也可以重寫isTaskRoot。
ok寓盗,到這里就算完成了集成了灌砖,直接使用startActivity就能跳轉(zhuǎn)到游戲頁面了。
最后cocos和Android相互通信的實現(xiàn)見官方文檔
https://docs.cocos.com/creator/manual/zh/advanced-topics/java-reflection.html