cocos2dLua3.17.2 + AS3.5.2 建立工程成功
阿新 • • 發佈:2021-11-11
最近一個童鞋問我,他用的是cocoslua3.17.2 建立新工程,各種問題出現,不知道怎麼辦了,希望我能協助一下,然後,我就把我線上跑的工程所用到的一些版本告訴他,沒想到,被鄙視了: 你還用AS3.2呀,太早了呀,我現在用的3.5.2,我不想用太老的呀。能幫我搞一下不?
就這樣,我就被鄙視了。好吧,也閒著,看一下,自己跑去下了個(你這也不是新的啊,還鄙視我,只比我用的新了那麼一捏捏而已嘛)
既然用新的AS了,那麼Gradle也要升級了,事實證明,gradle也不能隨便想升到幾就是幾,遇到一些問題後,最終確定一個版本
classpath 'com.android.tools.build:gradle:3.4.0'
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
NDK版本:android-ndk-r19c
由於google上架要求android api版本必須在30以上,也改了吧
# Android SDK version that will be used as the compile project PROP_COMPILE_SDK_VERSION=30 # 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=30
編譯時還要更改專案級build.gradle 檔案variant.mergeAssets.doLast 替換為variant.mergeAssetsProvider.get().doLast
編譯成功了,找個android手機,跑一下。
很好,不出所料,黑屏。gradle升級後,assets資料夾無法拷貝了。繼續修改專案級 build.gradle檔案
修改後的程式碼
android.applicationVariants.all { variant -> // delete previous files first delete "${buildDir}/intermediates/assets/${variant.dirName}" delete "${buildDir}/intermediates/merged_assets/${variant.dirName}" variant.mergeAssetsProvider.get().doLast { copy { from "${buildDir}/../../../../../res" into "${buildDir}/intermediates/assets/${variant.dirName}/res" } copy { from "${buildDir}/../../../../../src" into "${buildDir}/intermediates/assets/${variant.dirName}/src" } if (variant.name.compareTo('release') == 0) { copy { from "${buildDir}/../../../../../res" into "${buildDir}/intermediates/merged_assets/release/out/res" } copy { from "${buildDir}/../../../../../src" into "${buildDir}/intermediates/merged_assets/release/out/src" } }else{ copy { from "${buildDir}/../../../../../res" into "${buildDir}/intermediates/merged_assets/debug/out/res" } copy { from "${buildDir}/../../../../../src" into "${buildDir}/intermediates/merged_assets/debug/out/src" } } // copy { // from "${buildDir}/../../../../cocos2d-x/cocos/scripting/lua-bindings/script" // into "${buildDir}/intermediates/assets/${variant.dirName}/src/cocos" // } def compileScript = false // compile & encrypt the scripts if necessary //def compileScript = (variant.name.compareTo('release') == 0) if (project.hasProperty('PROP_COMPILE_SCRIPT')) { compileScript = (PROP_COMPILE_SCRIPT.compareTo('1') == 0) } def encryptLua = project.hasProperty('PROP_LUA_ENCRYPT') && (PROP_LUA_ENCRYPT.compareTo('1') == 0) if (compileScript || encryptLua) { // -1 means not build bytecode // 0 means build 32bit only // 1 means build 64bit only // 2 means build both 32bit & 64bit def buildType = -1 if (compileScript) { def need64 = false def need32 = false def abis = PROP_APP_ABI.split(':').collect{it as String} abis.each{ abi-> if (abi.compareTo('arm64-v8a') == 0) { need64 = true } else { need32 = true } } if (need64 && need32) { buildType = 2 } else if (need64) { buildType = 1 } else { buildType = 0 } } // invoke cocos command to compile & encrypt the lua files switch (buildType) { case -1: compileLua("${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src", "${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src", false, false, encryptLua) break case 0: compileLua("${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src", "${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src", true, false, encryptLua) break case 1: compileLua("${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src", "${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src/64bit", true, true, encryptLua) // remove the lua files in src dir delete fileTree("${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src") { include '**/*.lua' } delete "${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src/cocos" break case 2: compileLua("${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src", "${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src/64bit", true, true, encryptLua) compileLua("${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src", "${buildDir}/intermediates/merged_assets/${dirName}/${mergeAssets}/out/src", true, false, encryptLua) break } } } }
由於我不直接用他的加密,所以加密那部分我沒太留意改的對不對。
繼續跑一下,OK 了,介面就這樣出來了,assets資料夾也打入到apk中了。全部完成。以上過成功遇到過很多問題,例如:Execution failed for task ':testdemo:externalNativeBuildDebug'. 這樣的問題。通過更改Gradle和NDK版本,基本都解決了,如果還有問題,可以在聯絡我,我們一起探討。