Cocos crearor版本: 3.4.2
Android Studio Flamingo | 2022.2.1 Patch 2
1、配置构建安卓项目
2、 运行编译无报错
出现问题可尝试修改Gradle版本
修改jdk版本
3、对libservice打包成aar
打包完后 再build/outputs找到aar
如果看不到Tasks模块,在File -> Settings -> Experimental 选项,取消勾选 Do not build Gradle task list during Gradle sync 选项
Android Studio 右侧 Gradle 不显示 tasks 模块_右侧gradle更新完成后才会出现模块吗-CSDN博客
4、对libcocos2dx打包成aar
5、对app游戏内容打包成aar
修改游戏项的build.gradle,本身导出安卓项是打包apk的,所以需要gradle,将内容改成library,并去掉无用项,
修改后
import org.apache.tools.ant.taskdefs.condition.Os
//将game工程作为library
apply plugin: 'com.android.library'
RES_PATH = RES_PATH.replace("\\", "/")
COCOS_ENGINE_PATH = COCOS_ENGINE_PATH.replace("\\", "/")
buildDir = "${RES_PATH}/proj/build/$project.name"
android {
//有baseFeature 的话也注掉
//baseFeature true
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
buildToolsVersion PROP_BUILD_TOOLS_VERSION
ndkPath PROP_NDK_PATH
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
//去掉applicationId
// applicationId APPLICATION_ID
minSdkVersion PROP_MIN_SDK_VERSION
targetSdkVersion PROP_TARGET_SDK_VERSION
versionCode 1
versionName "1.0.0.1"
externalNativeBuild {
cmake {
targets "cocos"
arguments "-DRES_DIR=${RES_PATH}", "-DCOCOS_X_PATH=${COCOS_ENGINE_PATH}", "-DANDROID_STL=c++_static", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_NEON=TRUE", "-DANDROID_LD=gold"
cppFlags "-frtti -fexceptions -fsigned-char"
}
ndk { abiFilters PROP_APP_ABI.split(':') }
}
}
sourceSets.main {
java.srcDirs "../src", "src"
res.srcDirs "../res", 'res'
jniLibs.srcDirs "../libs", 'libs'
manifest.srcFile "AndroidManifest.xml"
assets.srcDir "${RES_PATH}/assets"
jniLibs {
// Vulkan validation layer
// srcDir "${android.ndkDirectory}/sources/third_party/vulkan/src/build-android/jniLibs"
}
}
externalNativeBuild {
cmake {
path "../CMakeLists.txt"
buildStagingDirectory "${RES_PATH}/proj/build"
}
}
signingConfigs {
release {
if (project.hasProperty("RELEASE_STORE_FILE") && !RELEASE_STORE_FILE.isEmpty()) {
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
}
// resValue "string", "app_name", PROP_APP_NAME
}
debug {
debuggable true
jniDebuggable true
renderscriptDebuggable true
// resValue "string", "app_name", "${PROP_APP_NAME}-dbg"
// applicationIdSuffix ".debug"
}
}
}
dependencies {
//去掉其他aar的添加
//implementation fileTree(dir: '../libs', include: ['*.jar','*.aar'])
//implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
//implementation fileTree(dir: "${COCOS_ENGINE_PATH}/cocos/platform/android/java/libs", include: ['*.jar'])
implementation project(':libservice')
implementation project(':libcocos')
}
修改AndroidManifest,去掉启动
编译出aar
6:其他项目组安卓工程,导入游戏aar资源,并加载呈现游戏
在lib中加入导出的aar包
在build.gradle中引入,game-release.aar不知道为啥引入失败,所以单独添加了
MainActivity继承AppActivity就能使用了,如果本身游戏有其他安卓项需求,也可在AppActivity中实现再打包到aar中