gradle打包的时候,自定义apk名称的时候,遇到这个问题:
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. Ope
搜到一个答案,解决了问题:
//**Use all() instead of each()
//Use outputFileName instead of output.outputFile if you change only file name (that is your case)**
// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "xxx.apk"
}
}
答案出处:https://blog.csdn.net/CHITTY1993/article/details/78667069
我原来的方法是这样的:
//修改生成的文件名
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
def buildName
variant.productFlavors.each { product ->
buildName = product.name
}
//这里修改文件名
def fileName = "XXX" + "_" + buildName + "_" + variant.buildType.name + "_" +
"${defaultConfig.versionName}_${releaseDate()}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}