Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'....

like image 817
Mohamd Ali Avatar asked May 29 '17 09:05

Mohamd Ali


2 Answers

As Android plugin 3.0 migration guide suggests:

  • Use all() instead of each()
  • Use outputFileName instead of output.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"
    }
}
like image 186
Paweł Nadolski Avatar answered Nov 05 '22 16:11

Paweł Nadolski


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);
    }
}
like image 7
Golan Shay Avatar answered Nov 05 '22 16:11

Golan Shay