Skip to content

Instantly share code, notes, and snippets.

@dekalo-stanislav
Created March 21, 2017 10:21
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dekalo-stanislav/9ad5f76cc2b49828acbf0634f6586b6c to your computer and use it in GitHub Desktop.
Save dekalo-stanislav/9ad5f76cc2b49828acbf0634f6586b6c to your computer and use it in GitHub Desktop.
Semantic Versioning for android application
/**
* Will generate versionCode from versionName that follows Semantic Versioning
*/
ext {
/**
* Application version is located version variable.
* And should follow next policy:
* X1.X2.X3-type-flavor, where X - any digits and type is optional alphabetical suffix.
* X1 - major version
* X2 - minor version
* X3 - patch
* There are next supported type:
* dev - code 0
* alpha - code 1
* beta - code 2
* no suffix - code 9 - means final production build from market.
* flavors currently disabled, value (0)
*/
buildVersionCode = { String versionName ->
/**
* Here is representation of Version code generation from version name.
*
* *--------- major version
* | *------ minor version
* | | *--- patch version
* | | |*-- buildType (dev/alpha/beta/rc/release)
* | | ||*- flavor - disabled.
* | | |||
* X00X00XXX
* so
* 1.13.20-alpha-v19 = 10130201
* see details below:
* *--------- 1 is major version
* | *------ 13 is minor version
* | | *--- 20 is patch version
* | | |*-- type 1 is alpha.
* | | ||*- flavor 0 (disabled)
* | | |||
* 101302010
*/
def (mainPart, typeName) = versionName.toLowerCase().tokenize('-')
if (typeName == null) {
type = "9"
} else if (typeName.equals("dev")) {
type = "0"
} else if (typeName.equals("alpha")) {
type = "1"
} else if (typeName.equals("beta")) {
type = "2"
} else {
println("Unknown build type = " + typeName + ". Please look into app/versioning.gradle.")
throw new RuntimeException("Unknown type = " + typeName)
}
/**
* At the project start time, there was flavor support.
* Now we should reserve last digit, because removing it will reduce version code 10 times.
* And next version will have versionCode less than previous.
*/
flavor = "0" // disabled
int flavorDigit = 1
int typeDigit = flavorDigit * 10
int patchDigit = typeDigit * 10
int minorDigit = patchDigit * 1000
int majorDigit = minorDigit * 1000
def (major, minor, patch) = mainPart.tokenize('.')
(major, minor, patch, type, flavor) = [major, minor, patch, type, flavor].collect {
it.toInteger()
}
(major * majorDigit) + (minor * minorDigit) + (patch * patchDigit) + (type * typeDigit) + (flavor * flavorDigit);
}
}
@fredgrott
Copy link

this is great as you covered all the use cases even the edge ones, good work

@andraantariksa
Copy link

For those who don't know, one of the examples of flavor are "free" and "paid" version of your app

https://www.journaldev.com/21533/android-build-types-product-flavors

If you just making a free apps, just disable it like this script mentioned. If not, just make a different suffix number for each of flavor

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