更新完後試下運行正在維護的舊項目,出現各種錯誤,因為後來發現問題不在這,所以沒記完整,大概如下: A larger heap for the Gradle daemon is recommended for running jack. It currently has 512 MB.For fast ...
更新完後試下運行正在維護的舊項目,出現各種錯誤,因為後來發現問題不在這,所以沒記完整,大概如下:
A larger heap for the Gradle daemon is recommended for running jack.
It currently has 512 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to at least 1536 MB.
To do this set org.gradle.jvmargs=-Xmx1536M in the project gradle.properties.
然後查看gradle.properties我的org.gradle.jvmargs已經是2048了,另外還有個Error:UNEXPECTED TOP-LEVEL ERROR:錯誤
搜索了一下並沒找到要點,於是Build一下項目,提示
Error:Failed to complete Gradle execution.
Cause:
The version of Gradle you are using (3.3) does not support the forTasks() method on BuildActionExecuter. Support for this is available in Gradle 3.5 and all later versions.
於是更新使用Gradle最新版,新建一個項目看看預設用的什麼版本,修改以下文件
gradle-wrapper.properties里
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
APP的build.gradle里
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
編譯後又有錯
Cannot set the value of read-only property 'outputFile'....
因為使用了編譯輸出文件名的代碼
applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile def fileName if (outputFile != null && outputFile.name.endsWith('.apk')) { if (variant.buildType.name.equals('release')) { // 輸出apk名稱為Test_v_1.0_15-09-15 11:12:32_official_release.apk fileName = "Test_v_${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}_release.apk" } else if (variant.buildType.name.equals('debug')) { // 輸出apk名稱為Test_v_1.0_15-09-15 11:12:32_official_debug.apk fileName = "Test_v_${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}_debug.apk" } output.outputFile = new File(outputFile.parent, fileName) } } }
出錯在最後一行,應該是新版Gradle的問題,最後在Stackover Flow找到解決辦法,參考
https://stackoverflow.com/questions/44239235/android-gradle-3-0-0-alpha2-plugin-cannot-set-the-value-of-read-only-property
把 variant.outputs.each 改成
variant.outputs.all
最後一行改成
outputFileName = fileName
繼續編譯繼續出錯
Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
百度找到以下文章,修改後就可以正常編譯運行了
http://blog.csdn.net/syif88/article/details/75009663
看了幾個文章也搞不清楚這個是什麼來的,照著改項目的build.gradle
defaultConfig { targetSdkVersion:*** minSdkVersion :*** versionCode:*** versionName :*** //版本名後面添加一句話,意思就是flavor dimension 它的維度就是該版本號,這樣維度就是都是統一的了 flavorDimensions "versionCode" }
項目的其它代碼不用改,文章開頭的錯誤全沒了,還好沒浪費太多時間