Last active
January 9, 2020 06:42
-
-
Save zsiegel/7813239 to your computer and use it in GitHub Desktop.
Android manifest versioning with gradle and git
This file contains hidden or 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
task('increaseVersionCode') << { | |
def manifestFile = file("src/main/AndroidManifest.xml") | |
def pattern = Pattern.compile("versionCode=\"(\\d+)\"") | |
def manifestText = manifestFile.getText() | |
def matcher = pattern.matcher(manifestText) | |
matcher.find() | |
def versionCode = Integer.parseInt(matcher.group(1)) | |
android.defaultConfig.versionCode = versionCode + 1 | |
println "Setting version code to ${android.defaultConfig.versionCode}" | |
def manifestContent = matcher.replaceAll("versionCode=\"" + android.defaultConfig.versionCode + "\"") | |
manifestFile.write(manifestContent) | |
} | |
task('setVersionName') << { | |
def git = 'git describe --abbrev=7 --tags' | |
def proc = git.execute() | |
proc.waitFor() | |
def desc = "${proc.in.text}".trim() | |
if (desc.isEmpty()) { | |
println "ERROR setting version name" | |
return | |
} | |
def manifestFile = file("src/main/AndroidManifest.xml") | |
def pattern = Pattern.compile("versionName=\"(.*)\"") | |
def manifestText = manifestFile.getText() | |
def matcher = pattern.matcher(manifestText) | |
matcher.find() | |
println "Setting version name to ${desc}" | |
def manifestContent = matcher.replaceAll("versionName=\"" + desc + "\"") | |
manifestFile.write(manifestContent) | |
} | |
tasks.whenTaskAdded { task -> | |
if (task.name == 'compileRelease') { | |
task.dependsOn 'increaseVersionCode' | |
task.dependsOn 'setVersionName' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment