Android Studio 3.0升级遇到问题总结

升级Android Studio 3.0后遇到的文通总结

1、升级3.0之后 编译项目出现这个错误

1
2
3
Error:(26, 0) Cannot set the value of read-only property 'outputFile' for
ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=devDebug, filters=[]}}
of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

这个错误是因为3.0之后我们需要修改 each()和outputFile()方法为all()和outputFileName

比如

1
2
3
4
5
6
7
8
9
10
11
12
//3.0之前这样写
android.applicationVariants.all { variant ->
variant.outputs.each {
output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为XXXX_v1.0_2016-01-15_wandoujia.apk
def fileName = "RxHttpUtils_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
//3.0之后需要修改为这样
android.applicationVariants.all { variant ->
variant.outputs.all {
output ->
outputFileName =
rootProject.getName()
+"_v"+defaultConfig.versionName
+"_"+releaseTime()
+"_"+variant.productFlavors[0].name
+".apk"
}
}

2、经过上边修改之后编译发现还会报错

1
2
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

这个错误是因为我们上边使用了Flavors设置多渠道的配置(variant.productFlavors[0].name)

需要加上flavorDimensions属性
Flavor Dimensions:每一个 Dimensions 代表一个维度,并且 flavor 都被分配到一个指定的 Dimensions 中。

android.flavorDimensions 中定义的 Dimensions 排序非常重要(Variant 命名和优先级等)。

flavorDimensions 中的排序决定了哪一个 flavor 覆盖哪一个,这对于资源来说非常重要,因为一个 flavor 中的值会替换定义在低优先级的 flavor 中的值。

flavorDimensions 使用最高的优先级定义,因此在上面例子中的优先级为:

price > store > defaultConfig

在 Android Gradle Plugin 2.0 版本中,flavor 中的 flavorDimension 字段改为 dimension ,flavorDimension 无法继续使用,Android Gradle Plugin 1.3 已经支持 dimension 了,所以建议使用 dimension 。

例如这样写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
flavorDimensions 'xxx'
productFlavors {
// 线上环境
pro {
}
// 上线前的预览版本
releases {
}
// 提测后(测试阶段)
master {
}
// 开发阶段
dev {
}
}

3、其他错误

1
2
Error:(30, 0) No signature of method: java.lang.String.positive() is applicable for argument types: () values: []
Possible solutions: notify(), size(), tokenize(), size()

这个错误是因为配置中出现了空格,仔细检查一下上边几个配置去掉空格就没问题了