更新Android Studio 3.0碰到的问题
更新完后试下运行正在维护的旧项目,出现各种错误,因为后来发现问题不在这,所以没记完整,大概如下:
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" }
项目的其它代码不用改,文章开头的错误全没了,还好没浪费太多时间