Android / gradle: include version name from flavor in apk file name

I found similar questions, but nothing works for what I want to do. I am developing with Android Studio and gradle, and I have several options in my build file, each with the name versionName. Is there a way to include the name of the flavor version in the APK file?

+10
android gradle
Aug 12 '14 at 16:33
source share
2 answers

EDIT: for modern versions of Gradle tools, use the @ptx variation (use output.outputFile instead of variant.outputFile :

 android { applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parentFile, output.outputFile.name.replace(".apk", "${variant.versionName}.apk")); } } } 



As with the Gradle plugin for tools version 0.14, you should use the following instead to properly support multiple output files (e.g., APK Splits):

 android { applicationVariants.all { variant -> variant.outputs.each { output -> variant.outputFile = new File(variant.outputFile.parentFile, variant.outputFile.name.replace(".apk", "${variant.versionName}.apk")); } } } 

For older versions:




Yes, we do this in our application. Just add the following to your android tag in build.gradle:

 android { applicationVariants.all { variant -> def oldFile = variant.outputFile def newPath = oldFile.name.replace(".apk", "${variant.versionName}.apk") variant.outputFile = new File(oldFile.parentFile, newPath) } } 
+12
Aug 12 '14 at 16:51
source share

Kcoppock's answer is correct, but there is a small typo. It should be:

 android { applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parentFile, output.outputFile.name.replace(".apk", "${variant.versionName}.apk")); } } } 

(I wrote this as an answer because I have no reputation to make a comment)

+5
Feb 04 '15 at 11:38
source share



All Articles