Skip to content

Instantly share code, notes, and snippets.

@jweaver
Last active June 2, 2017 15:55
Show Gist options
  • Save jweaver/2798bf6dc621a1a24c5b3ab6ad1ee8d7 to your computer and use it in GitHub Desktop.
Save jweaver/2798bf6dc621a1a24c5b3ab6ad1ee8d7 to your computer and use it in GitHub Desktop.
Version Scheme for Android Applications. This bridges the gap between a "version name" and Google's "version code" (a single integer). Using this along with your gradle build, you can convert a typical SNAPSHOT or RC based semantic version string into a monotonically increasing version code, guaranteeing that when an updated APK is installed, it…
ext {
/**
* Builds an Android version code from the version of the project.
* This is designed to handle the -SNAPSHOT and -RC format.
*
* I.e. during development the version ends with -SNAPSHOT. As the code stabilizes and release nears
* one or many Release Candidates are tagged. These all end with "-RC1", "-RC2" etc.
* And the final release is without any suffix.
*/
buildVersionCode = {
/**
* The rules is as follows:
*
* -SNAPSHOT counts as 0
* -RC* counts as the RC number, i.e. 1 to 98
* final release counts as 99.
*
* Thus you can only have 98 Release Candidates, which ought to be enough for everyone.
*
* Example configurations of VersionName to VersionNumber:
* "1.2.0-SNAPSHOT" => 1020000
* "1.2.0-RC1" => 1020001
* "1.2.0-RC2" => 1020002
* "1.2.0" => 1020099
* "1.2.1-SNAPSHOT" => 1020100
* -----
* "1.2.3-SNAPSHOT" => 1020300
* "1.2.3-RC4" => 1020304
* "1.2.3-RC5" => 1020305
* "1.2.3" => 1020399
* "1.2.4-SNAPSHOT" => 1020400
* "1.2.4-RC1" => 1020401
*/
def candidate = "99"
def (major, minor, patch) = version.toLowerCase().replaceAll('-', '').tokenize('.')
if (patch.endsWith("snapshot")) {
candidate = "0"
patch = patch.replaceAll("[^0-9]","")
} else {
def rc
(patch, rc) = patch.tokenize("rc")
if (rc) {
candidate = rc
}
}
(major, minor, patch, candidate) = [major, minor, patch, candidate].collect{it.toInteger()}
(major * 1000000) + (minor * 10000) + (patch * 100) + candidate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment