Android Project Gradle升級5.1遇到的坑

由于更換了設備,就安裝了最新的開發(fā)環(huán)境,從git上clone代碼時,同步失敗。項目為Java和Kotlin混編。遂經(jīng)歷了不短的一段調(diào)整時間調(diào)試,終于可以正常的run起來了,記錄一下。

本地環(huán)境

Android Gradle Plugin Version: 3.4.0
Gradle Version: 5.1.1

項目環(huán)境

Android Gradle Plugin Version: 3.0.1(clone到本地后被我修改為3.4.0)
protobuf-gradle-plugin: 0.8.5
kotlin-gradle-plugin: 1.2.50

遇到的問題

protobuf-gradle-plugin版本不兼容

  • 報錯日志1:
No such property: javaCompilerTask for class: com.android.build.gradle.internal.variant.TestVariantData
  • 報錯日志2:
 Directory '/<project_path>/build/extracted-include-protos/main' specified for property '$3' does not exist.

kotlin-gradle-plugin版本不兼容

  • 報錯日志:
Error:Execution failed for task ':app:kaptDebugKotlin'.
Internal compiler error. See log for more details.

設置mergedFlavor的versionCode/versionName出錯

  • 報錯日志:
ERROR: versionName cannot be set on a mergedFlavor directly.
versionNameOverride can instead be set for variant outputs using the following syntax:
android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.versionNameOverride = "x.x.x"
        }
    }
}
  • 解決辦法:
    按照日志里面的方法修改就行,具體如下:
// 修改前
applicationVariants.all { variant ->
        def flavor = variant.mergedFlavor
        // versionCode也一樣
        def versionName = flavor.getVersionName()
        flavor.versionName = versionName
}

// 修改后
applicationVariants.all { variant ->
    def flavor = variant.mergedFlavor
    def versionName = flavor.getVersionName()
    variant.outputs.each { output ->
        output.versionNameOverride = versionName
    }
}

其他與gradle版本無關的問題

git config配置出錯

我們的項目versionCode是動態(tài)配置的,類似于這樣:


Properties cusProperties = loadProperties("local.properties")

def getVersionCode = { ->
    if(cusProperties['version.code'])
        return cusProperties['version.code']
    else
        return 'git rev-list --count HEAD'.execute([], project.rootDir).text.trim()
}

def getVersionName = { ->
    if(cusProperties['version.name'])
        return cusProperties['version.name']
    else
        return "git describe --tags --dirty".execute([], project.rootDir).text.trim() + "." + getVersionCode()
}

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "xx.xx.xx"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode getVersionCode().toInteger()
        versionName getVersionName()
    }
}

然后在同步的時候,命令行'git rev-list --count HEAD'執(zhí)行報錯。當時不知道到底是什么原因,最后在該命令行的.execute()后面加了.err,使得整個return語句變?yōu)椋?/p>

return 'git rev-list --count HEAD'.execute([], project.rootDir).err.text.trim()

于是便可以查看輸出的錯誤信息。如下:

error: could not expand include path '~/.gitcinclude',
fatal: bad config file line 49 in /usr/local/git/etc/gitconfig

至此,所有問題解決完畢,可以正常運行了。

總結(jié)

Gradle升級時,很可能會引發(fā)老版本的項目同步/編譯出錯。所以沒有必要就暫時先別更新,如果一定要更新,最好將相關的所有插件版本全部更新至兼容版本。
其次遇到問題最好去Google,這些問題除非是天選之人,否則同樣的問題一定有人遇到過,而百度一般搜不到(至少我這次基本沒有用百度搜到過正確答案)。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容