63

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.

Vishal Senjaliya
  • 454
  • 6
  • 21
Addev
  • 31,819
  • 51
  • 183
  • 302

7 Answers7

120

Use output.outputFileName instead of output.outputFile

AndrzejSc
  • 1,316
  • 1
  • 8
  • 4
  • 2
    It was created error afterwards, I needed to solve it like here: https://stackoverflow.com/a/46959050/2061089 – oli Nov 30 '17 at 13:10
  • try this https://stackoverflow.com/questions/46949311/cannot-set-the-value-of-read-only-property-outputfile-for-apkvariantoutputimpl/50464200#50464200 – GS Nayma May 22 '18 at 09:30
  • 1
    This causes an error: **ERROR: No such property: parent for class: java.lang.String** – IgorGanapolsky Aug 09 '19 at 12:58
31

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

Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
17

Try this code :

buildTypes {

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

    }
9

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)
        }
}
GS Nayma
  • 1,745
  • 19
  • 24
  • I got this error Could not set unknown property 'outputFileName' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl. – Yogesh Rathi Nov 19 '18 at 11:55
1

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.

Surbhit Rao
  • 625
  • 6
  • 14
1

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"))
}

}

0

change your code into:-

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