由于更換了設(shè)備渣淤,就安裝了最新的開發(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.
- 解決辦法:將protobuf-gradle-plugin版本號升至0.8.8或以上千贯。
- 參考:https://github.com/google/protobuf-gradle-plugin/issues/253
kotlin-gradle-plugin版本不兼容
- 報錯日志:
Error:Execution failed for task ':app:kaptDebugKotlin'.
Internal compiler error. See log for more details.
- 解決辦法:將kotlin-gradle-plugin版本號升至1.3.21或以上屯仗。
- 參考:
設(shè)置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
}
}
- 參考:
https://stackoverflow.com/questions/52924175/versionname-cannot-be-set-on-a-mergedflavor-directly
其他與gradle版本無關(guān)的問題
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
- 解決辦法:這個錯誤是由于系統(tǒng)gitconfig文件中的默認路徑
~/
出了點問題。這里我用的是macOS X右钾。
找到/usr/local/git/etc/gitconfig文件蚁吝,打開,發(fā)現(xiàn)文件只讀舀射,被上鎖了窘茁。于是右鍵顯示簡介,右下角有個鎖子圖標脆烟,打開山林,然后最下面的共享與權(quán)限中將用戶權(quán)限改為讀寫。
然后用文本編譯器打開邢羔,將其中所有的~/
路徑都更換為/Users/<yourusername>/
后保存驼抹,然后還原文件權(quán)限,再次同步文件拜鹤,問題解決了砂蔽。 - 參考:
至此,所有問題解決完畢署惯,可以正常運行了左驾。
總結(jié)
Gradle升級時,很可能會引發(fā)老版本的項目同步/編譯出錯。所以沒有必要就暫時先別更新诡右,如果一定要更新安岂,最好將相關(guān)的所有插件版本全部更新至兼容版本。
其次遇到問題最好去Google帆吻,這些問題除非是天選之人域那,否則同樣的問題一定有人遇到過,而百度一般搜不到(至少我這次基本沒有用百度搜到過正確答案)猜煮。