Skip to content

Instantly share code, notes, and snippets.

@dodgex
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodgex/ac418740431c1347def7 to your computer and use it in GitHub Desktop.
Save dodgex/ac418740431c1347def7 to your computer and use it in GitHub Desktop.
/**
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details.
*
* --------------------------------------------------------------------
*
* This method takes a SemVer (http://semver.org/) compatible Version String
* and generates an int value of it, that is usable as versionCode
* for Android Apps or Libraries. As second parameter you can provide an
* Integer value that is used to increment the generated versionCode.
* This is usefull if your current versionCode is higher than the value
* that would be generated by this method.
*
* The versionCode is composed of the major version as first digit(s) and
* then the minor and patch versions with three digits each. The last digit
* is either 1 or 0, depending on the presence of a pre-release version.
* When a pre-release version is given, the last digit is 0. This ensures
* that a release version has always a higher versionCode than a pre-release.
*
* NOTE: Currenlty it is not possible to have diffrent versionCodes for
* 1.0.0-ALPHA and 1.0.0-BETA.
*
*
* Examples:
* 1.0.0 -> 10000001
* 3.15.99 -> 10150991
* 13.0.0-ALPHA -> 130000000
* 13.0.0-BETA -> 130000000
*
* Usage:
* android {
* defaultConfig {
* versionName project.version
* versionCode getVersionCode(project.version)
* }
* }
*
*/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "com.github.zafarkhaja:java-semver:0.9.0"
}
}
gradle.allprojects {
ext.getVersionCode = { versionString, increment = 0 ->
def version = com.github.zafarkhaja.semver.Version.valueOf(versionString)
int versionInt = 0;
String preReleaseVersion = version.getPreReleaseVersion();
if (preReleaseVersion.isEmpty()) {
versionInt = 1;
}
versionInt += version.getMajorVersion() * 10000000;
versionInt += version.getMinorVersion() * 10000;
versionInt += version.getPatchVersion() * 10;
versionInt += increment;
return versionInt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment