Android Gradle 3.0.0-alpha2 plugin, Cannot set the value of read-only property 'outputFile'
i was using this code
applicationVariants.all { variant ->
variant.outputs.each { output ->
def SEP = "_"
def flavor = variant.productFlavors[0].name
def buildType =
variant.variantData.variantConfiguration.buildType.name
def version = variant.versionName
def date = new Date()
def formattedDate = date.format('ddMMyy_HHmm')
def newApkName = PROJECT_NAME + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"
def file = new File(newApkName)
output.outputFile = file
}
}
to change the name of apk file when i build new apk, but since i use the Android Studio 3.0 Canary 2 this error appear:
Cannot set the value of read-only property 'outputFile'....
As Android plugin 3.0 migration guide suggests:
- Use
all()
instead ofeach()
- Use
outputFileName
instead ofoutput.outputFile
if you change only file name (that is your case)
Example from the guide:
// 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 = "${variant.name}-${variant.versionName}.apk"
}
}
See below:
applicationVariants.all { variant ->
variant.outputs.all { output ->
def newApkName = applicationId + "-" + variant.versionName + "(" + variant.versionCode + ")" + ".apk";
outputFileName = new File("${project.projectDir}/../outputs/apks/" + variant.name, newApkName);
}
}
Below code is working for me on android studio canary 3.0.0-alpha3
android.applicationVariants.all {
variant.outputs.all {
def newApkName
newApkName = "APPLICATION_NAME-" + defaultConfig.versionName + "-" + defaultConfig.versionCode".apk"
outputFileName = newApkName;
}
}
This change the apk file name
This is the complete example of this question.
Only you have to paste in your gradle 3.0+ after productFlavours
android.applicationVariants.all { variant ->
variant.outputs.all {
def SEP = "_"
def flavor = variant.productFlavors[0].name
def buildType = variant.variantData.variantConfiguration.buildType.name
def version = variant.versionName
def versionCode = variant.versionCode
def date = new Date();
def formattedDate = date.format('ddMMyy_HHmm')
outputFileName = "${flavor}${SEP}${buildType}${SEP}${version}${SEP}${versionCode}${SEP}${formattedDate}.apk"
}
}