Skip to content

Instantly share code, notes, and snippets.

@Nilzor
Last active August 29, 2015 14:08
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nilzor/34a939891f343c968f5e to your computer and use it in GitHub Desktop.
Save Nilzor/34a939891f343c968f5e to your computer and use it in GitHub Desktop.
build.gradle snippet for automatically adding version code postfix to APK file name
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory
// Set output filename of APK based on version code from manifest when doing release build
// Note: This script will add a couple of seconds to the build script build time
gradle.projectsEvaluated {
preReleaseBuild.doFirst {
android.applicationVariants.all { variant ->
// Check version number configured in AndroidManifest
if (variant.name != "release") return
def dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder()
def manifestFile = "$project.projectDir" + "/src/main/AndroidManifest.xml"
def doc = dbf.parse(manifestFile)
def expr = XPathFactory.newInstance().newXPath().compile("/manifest/@versionCode")
def versionCode = expr.evaluate(doc, XPathConstants.STRING)
println "versionCode=" + versionCode
// Set output filename
variant.outputFile = file("../build/publish/myapp-v" + versionCode + ".apk")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment