文章目录
一,前言
CocosCreator可以将开发的游戏发布到Android平台,使用Android Studio可以构建和运行编译目录下(frameworks\runtime-src\proj.android-studio)的工程。但其生成的工程比较复杂,当我们需要从一个已有的Android项目启动我们开发出的游戏时,整合的过程极其繁琐。笔者整理了整合过程,供有需要的同志参考。
Cocos Creator 版本:2.4.2
Java 1.8
NDK 20.0.5594570
android gradle plugin version 3.2.0
gradle version 4.10.3
二,教程
1. CocosCreator构建发布
发布平台选择Android,模板选择default,方便后续整合。APP ABI笔者选择的是arm64-v8a。依次点击下方的“构建”、“编译“,编译过程会比较慢。
编译成功后会在发布路径生成jsb-default文件夹。
2. 导入libcocos2dx
将编译路径下的:frameworks\cocos2d-x 整个文件夹整个拷贝到我们自己的安卓项目根目录,然后在自己的安卓项目根目录下的setting.gradle中添加如下代码:
include ':libcocos2dx'
project(':libcocos2dx').projectDir = new File('cocos2d-x\\cocos\\platform\\android\\libcocos2dx')
3.修改gradle配置
将proj.android-studio\gradle.properties里的内容复制到我们自己的安卓项目对应的gradle.properties里:
# Android SDK version that will be used as the compile project
PROP_COMPILE_SDK_VERSION=29
# Android SDK version that will be used as the earliest version of android this application can run on
PROP_MIN_SDK_VERSION=16
# Android SDK version that will be used as the latest version of android this application has been tested on
PROP_TARGET_SDK_VERSION=29
# Android Build Tools version that will be used as the compile project
PROP_BUILD_TOOLS_VERSION=28.0.3
# List of CPU Archtexture to build that application with
# Available architextures (armeabi-v7a | arm64-v8a | x86)
# To build for multiple architexture, use the `:` between them
# Example - PROP_APP_ABI=arm64-v8a
PROP_APP_ABI=arm64-v8a
#这部分的内容注释掉不用,有需要的可以自行探索
# fill in sign information for release mode
#RELEASE_STORE_FILE=D:/software/cocosDashboard/resources/.editors/Creator/2.4.2/resources/static/build-templates/native/debug.keystore
#RELEASE_STORE_PASSWORD=123456
#RELEASE_KEY_ALIAS=debug_keystore
#RELEASE_KEY_PASSWORD=123456
android.injected.testOnly=false
然后sync
将proj.android-studio\app\build.gradle里的内容复制到我们安卓目录下的app\build.gradle
-
Os
-
externalNativeBuild
externalNativeBuild这里要修改路径
-
下面的externalNativeBuild和buildTypes照常复制过去
-
android.applicationVariants.all 需要修改一下,并且修改sourceDir的路径:
android.applicationVariants.all { variant ->
// delete previous files first
delete "${buildDir}/intermediates/merged_assets/${variant.dirName}"
variant.mergeAssets.doLast {
def sourceDir = "D:/build/jsb-default"//这里改成你的编译路径
// 以下几行是旧的代码,在新版Gradle下有问题
// copy {
// from "${sourceDir}/res"
// into "${outputDir}/res"
// }
//
// copy {
// from "${sourceDir}/src"
// into "${outputDir}/src"
// }
//
// copy {
// from "${sourceDir}/jsb-adapter"
// into "${outputDir}/jsb-adapter"
// }
// 新的拷贝文件的方法,在新版Gradle可用
copy{
from "${sourceDir}"
include "assets/**"
include "res/**"
include "src/**"
include "jsb-adapter/**"
into outputDir
}
copy {
from "${sourceDir}/main.js"
from "${sourceDir}/project.json"
into outputDir
}
}
}
android.applicationVariants.all是将我们的游戏资源,包括代码和图片资源都复制到编译输出目录。如果你的游戏启动后资源没加载出来,可能就是这里的路径没有配置好。
- 在dependencies里加入下面两行导入libcocos2dx(在这里是为项目导入libcocos2dx,名字可以在第二步修改):
此处贴一下我复制好后的app\build.gradle
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.example.testmw"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
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})
}
}
}
}
externalNativeBuild {
ndkBuild {
if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') !=