更新Android Studio 3.0之后,出现Cannot set the value of read-only property ‘outputFile’报错,也就是用来打包后重命名的gradle出现了问题
旧版本
AS 2.3时用的代码:
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "driver_${variant.productFlavors[0].name}_v${defaultConfig.versionName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
修改之后:
flavorDimensions 'api'
productFlavors {
...
}
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "driver_${variant.productFlavors[0].name}_v${variant.versionName}.apk"
}
}
关于flavorDimensions可查看官方文档
https://developer.android.google.cn/studio/build/build-variants.htm
参考资料
官方文档解析:https://developer.android.google.cn/studio/build/gradle-plugin-3-0-0-migration.html#variant_api