Last active
August 29, 2015 14:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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