Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

AndroidAndroid Gradle-Plugin

Android Problem Overview


I was using the following code in my gradle script to rename the apks generated with AndroidStudio:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(output.outputFile.parent, defaultConfig.versionCode + "_" + output.outputFile.name)
    }
}

So it was generating apks with names like: 345-app-release.apk, where 345 is the versionCode.

But after updating to AndroidStudio 3.0 it returns the following 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.

How can I achieve a similar renaming with the new build tools.

Android Solutions


Solution 1 - Android

Use output.outputFileName instead of output.outputFile

Solution 2 - Android

2019 - Simple Solution for Gradle 3.0+** and 3.1.0

To change the name of the APK in Android

 android { //add inside the android {}
   ......
   applicationVariants.all { variant ->
       variant.outputs.all {
           def flavor = variant.name
           def versionName = variant.versionName
           outputFileName = "prefix_${flavor}_${versionName}.apk"
       }
   }
}

> prefix_release_1.0.1.apk

Solution 3 - Android

Try this code :

buildTypes {

       applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def name = "myapp_v${variant.versionName}(${variant.versionCode}).apk"
                output.outputFileName = name
            }
        }

    }

Solution 4 - Android

> In or After gradle 3.1.0

try below code

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = new File(
                    "release_build", // here you can change the name
                    output.outputFile.name)
        }
}

Solution 5 - Android

In my case I resolved it by just refusing to update to Gradle 4.4 (in my case).So when Studio asks (when you open your project for the first time) you to update Gradle to support instant run,etc just simply refuse and you should be fine.

Solution 6 - Android

What worked for me was to

  1. change each to "all"
  2. change output.outputFile to "outputFileName"
  3. Run $./gradlew clean build in terminal

For example, if your artifacts.gradle settings were this:

android.applicationVariants.all { variant ->

variant.outputs.each { output ->
    def finalVersionCode = 10000 + versionCode
    output.versionCodeOverride = finalVersionCode
    output.outputFile = new File(
         output.outputFile.parent,       output.outputFile.name.replace(".apk","-${finalVersion}.apk"))
}

}

Then you would want to change it to this:

android.applicationVariants.all { variant ->

variant.outputs.all { output ->
    def finalVersionCode = 10000 + versionCode
    output.versionCodeOverride = finalVersionCode
    outputFileName = new File(
         output.outputFile.parent,
         outputFileName.replace(".apk", "-${finalVersionCode}.apk"))
}

}

Solution 7 - Android

change your code into:-

    applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(output.outputFile.parent, "${variant.applicationId}-${variant.versionName}.apk")
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAddevView Question on Stackoverflow
Solution 1 - AndroidAndrzejScView Answer on Stackoverflow
Solution 2 - AndroidZumry MohamedView Answer on Stackoverflow
Solution 3 - AndroidnaitbrahimView Answer on Stackoverflow
Solution 4 - AndroidGhanshyam NaymaView Answer on Stackoverflow
Solution 5 - AndroidSurbhit RaoView Answer on Stackoverflow
Solution 6 - AndroidnotyourguruView Answer on Stackoverflow
Solution 7 - AndroidChetanView Answer on Stackoverflow