由于更換了設備,就安裝了最新的開發(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或以上。
- 參考:
設置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版本無關的問題
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)文件只讀,被上鎖了。于是右鍵顯示簡介,右下角有個鎖子圖標,打開,然后最下面的共享與權限中將用戶權限改為讀寫。
然后用文本編譯器打開,將其中所有的~/路徑都更換為/Users/<yourusername>/后保存,然后還原文件權限,再次同步文件,問題解決了。 - 參考:
至此,所有問題解決完畢,可以正常運行了。
總結(jié)
Gradle升級時,很可能會引發(fā)老版本的項目同步/編譯出錯。所以沒有必要就暫時先別更新,如果一定要更新,最好將相關的所有插件版本全部更新至兼容版本。
其次遇到問題最好去Google,這些問題除非是天選之人,否則同樣的問題一定有人遇到過,而百度一般搜不到(至少我這次基本沒有用百度搜到過正確答案)。