5

I am trying to use android build tools "com.android.tools.build:gradle:3.0.0-alpha4" in my project. In my build script I rename the output apk which worked fine in the past but does not seem to be supported any more.

applicationVariants.all { variant ->

    def filename = "foo-${variant.baseName}-${variant.versionName}-(${android.defaultConfig.versionCode}).apk"

    variant.outputs.all { output ->
        output.outputFile = new File(
                output.outputFile.parent,
                filename
        )
    }
}

Now the propery I am trying to change became immutable:

Error: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=stageDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

Is there a new or an alternate way how to do this?

Hendrik
  • 103
  • 7
  • Possible duplicate of [Grade Plugin 3-alpha1 outputFile causes error](https://stackoverflow.com/questions/44044031/grade-plugin-3-alpha1-outputfile-causes-error) – Bertrand Martel Jun 20 '17 at 18:38
  • The error I get is actually different and the provided solution does not work for me. Seems to be a new issue in the alpha-4. Edited my sample to reflect that the solution from the official doc does not work. – Hendrik Jun 21 '17 at 05:15
  • Possible duplicate of https://stackoverflow.com/questions/44239235/android-gradle-3-0-0-alpha2-plugin-cannot-set-the-value-of-read-only-property – 崔乐天 Jul 27 '17 at 03:49

2 Answers2

1
  1. Change each -> to all
  2. Change output.outputFile -> to outputFileName

before:

android.applicationVariants.all { variant ->

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

after:

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"))
    }
}
Axel
  • 3,331
  • 11
  • 35
  • 58
jp1017
  • 81
  • 11
0

Add e.g. versionName to apk by adding

 setProperty("archivesBaseName", archivesBaseName + "-" + versionName)

in defaultConfig closure.

jayeffkay
  • 1,229
  • 1
  • 16
  • 22