cocos项目集成到已有的Android工程


将cocos项目构建为Android工程并构建出对应的aar

构建Android工程

1.配置原生开发环境(ndk路径、Android sdk路径)

2.打开构建发布窗口进行如下配置


将cocos Android工程构建为aar

1.将Cocos Creator导出的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应该是什么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不能设置这个参数
            //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注释掉启动页标签(因为我们不希望我们的工程运行起来就直接进入游戏)



            
                

                
                
            
            
        

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.创建我们自己的游戏Activity继承自aar包里面的AppActivity

package com.example.myapplication;
import org.cocos2dx.javascript.AppActivity;

public class MyAppActivity extends AppActivity {

}

在manifest里注册这个Activity



            

这里注意一下,由于cocos引擎有自己的资源释放策略(它会在Activity结束后干掉所在进程),所以为了不影响我们自己工程的进程必须将Activity放到另一个进程。另外,我这里将这个Activity放入了新任务栈,因为AppActivity、Cocos2dxActivity有些地方会去调用isTaskRoot,如果不把Activity放入新栈会导致代码执行不正确;当然,你也可以重写isTaskRoot。

ok,到这里就算完成了集成了,直接使用startActivity就能跳转到游戏页面了。


最后cocos和Android相互通信的实现见官方文档

https://docs.cocos.com/creator/manual/zh/advanced-topics/java-reflection.html

你可能感兴趣的:(cocos项目集成到已有的Android工程)