更新AS之后,编译出现如下错误
Error:Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
老版本的写法
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "XXX.apk"
output.outputFile = new File(outputFile.getParent(),fileName)
}
}
}
以及使用each
applicationVariants.all { variant ->
variant.outputs.each{ output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "xiaoyueyue.apk"
output.outputFile = new File(outputFile.parent,fileName)
}
}
}
解决办法
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "xiaoyueyue.apk"
outputFileName = fileName
}
}
}