Change apk file name in gradle

I am trying to use the Android build tools "com.android.tools.build: gradle: 3.0.0-alpha4" in my project. In my build script, I will rename the apk output, which worked fine in the past, but seems to be no longer supported.

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 property I'm trying to change has become unchanged:

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

Is there a new or alternative way to do this?

+5
source share
2 answers
  • Change each → to all
  • 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")) } } 
+1
source

Add for example. versionName in apk by adding

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

in defaultConfig closure.

0
source

Source: https://habr.com/ru/post/1269033/


All Articles