【Cocos2dx】如何将CocosCreator构建发布的Android平台工程整合到已有的Android项目中

本文详细介绍了如何将CocosCreator 2.4.2开发的游戏整合到已有Android项目中,包括CocosCreator构建发布、导入libcocos2dx、修改Gradle配置、复制JNI文件夹、复制定向Activity、解决闪退问题以及数据交互等关键步骤。通过这些步骤,开发者可以顺利地在现有Android应用中启动和运行CocosCreator游戏。
摘要由CSDN通过智能技术生成

一,前言

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这里要修改路径

  • 下面的externalNativeBuildbuildTypes照常复制过去
    在这里插入图片描述

  • 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') != 
  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 30
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值