11

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

1
  • Your question title refers to a version number; your question body refers to a version name. In Android, those are separate things. Which one are you referring to? Aug 12, 2014 at 16:48

2 Answers 2

12

EDIT: For modern versions of the Gradle tools, use @ptx's variation (use output.outputFile in place 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 of Gradle tools plugin version 0.14, you should start using 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:


Yeah, we do this in our app. Just add the following in 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)
    }
}
0
5

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 don't have the reputation to make a comment)

1
  • I'm not sure it was a mistake, I think with the change to Android Studio 1.0, or perhaps a newer version of gradle or the build tools, the details of how to do this changed. I have been using basically what you wrote and it's working great.
    – nasch
    Feb 4, 2015 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.