Skip to content

Instantly share code, notes, and snippets.

@kibotu
Last active May 1, 2018 11:01
Show Gist options
  • Save kibotu/b7d308ad67c1d74d75b7 to your computer and use it in GitHub Desktop.
Save kibotu/b7d308ad67c1d74d75b7 to your computer and use it in GitHub Desktop.
gradle build increases android versionName and versionCode automatically
// major.branchName.revId.commitCount
def pattern = Pattern.compile('versionName=\"(\\d+)\\.(\\w+)\\.(\\w+)\\.(\\d+)\"')
task incrementMajor << {
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "branch"
standardOutput = stdout;
}
def currentBranch = stdout.toString().trim();
def manifestFile = file('src/main/AndroidManifest.xml')
def matcher = pattern.matcher(manifestFile.getText())
matcher.find()
def versionName = String.format("%s.%s.%s.%s",
Integer.parseInt(matcher.group(1)),
matcher.group(2),
matcher.group(3),
Integer.parseInt(matcher.group(4)));
def manifestContent = matcher.replaceAll('versionName=\"' + versionName + '\"')
manifestFile.write(manifestContent)
}
task incrementMinor << {
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "rev-parse", "--abbrev-ref", "HEAD"
standardOutput = stdout;
}
def currentBranch = stdout.toString().trim();
def manifestFile = file('src/main/AndroidManifest.xml')
def matcher = pattern.matcher(manifestFile.getText())
matcher.find()
def versionName = String.format("%s.%s.%s.%s",
Integer.parseInt(matcher.group(1)),
currentBranch,
matcher.group(3),
Integer.parseInt(matcher.group(4)));
def manifestContent = matcher.replaceAll('versionName=\"' + versionName + '\"')
manifestFile.write(manifestContent)
}
task incrementPatch << {
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "log", "-n", "1", "--format='%h'"
standardOutput = stdout;
}
def revid = stdout.toString().trim();
def manifestFile = file('src/main/AndroidManifest.xml')
def matcher = pattern.matcher(manifestFile.getText())
matcher.find()
def versionName = String.format("%s.%s.%s.%s",
Integer.parseInt(matcher.group(1)),
matcher.group(2),
revid.substring(1, revid.length() - 2),
Integer.parseInt(matcher.group(4)));
def manifestContent = matcher.replaceAll('versionName=\"' + versionName + '\"')
manifestFile.write(manifestContent)
}
task incrementBuild << {
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "rev-list", "HEAD", "--count"
standardOutput = stdout;
}
def commitCount = stdout.toString().trim();
def manifestFile = file('src/main/AndroidManifest.xml')
def matcher = pattern.matcher(manifestFile.getText())
matcher.find()
def versionName = String.format("%s.%s.%s.%s",
Integer.parseInt(matcher.group(1)),
matcher.group(2),
matcher.group(3),
commitCount);
def manifestContent = matcher.replaceAll('versionName=\"' + versionName + '\"')
manifestFile.write(manifestContent)
}
task incrementVersionCode << {
def manifestFile = file('src/main/AndroidManifest.xml')
def matcher = Pattern.compile('versionCode=\"(\\d+)\"').matcher(manifestFile.getText())
matcher.find()
def manifestContent = matcher.replaceAll('versionCode=\"' +
++Integer.parseInt(matcher.group(1)) + '\"')
manifestFile.write(manifestContent)
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig' || task.name == 'generateDebugBuildConfig') {
task.dependsOn 'incrementMinor'
task.dependsOn 'incrementPatch'
task.dependsOn 'incrementBuild'
task.dependsOn 'incrementVersionCode'
}
}
@kibotu
Copy link
Author

kibotu commented Apr 17, 2015

produces on build e.g.:

android:versionCode="1"
android:versionName="1.master.431a96.300"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment