Skip to content

Instantly share code, notes, and snippets.

@maxirosson
Created June 27, 2017 15:29
Show Gist options
  • Save maxirosson/f5045734cf7359cb5dce3190ba55de4e to your computer and use it in GitHub Desktop.
Save maxirosson/f5045734cf7359cb5dce3190ba55de4e to your computer and use it in GitHub Desktop.
Versioning Android apps + Feature Branches
apply plugin: 'com.android.application'
ext.versionMajor = 1
ext.versionMinor = 2
ext.versionPatch = 3
ext.versionClassifier = null
ext.isSnapshot = true
ext.minimumSdkVersion = 15
ext.featureBranchPrefix = "feature/"
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.sample"
targetSdkVersion 26
minSdkVersion project.ext.minimumSdkVersion
versionCode generateVersionCode() // 150010203
versionName generateVersionName() // 1.2.3-SNAPSHOT
}
}
private Integer generateVersionCode() {
return ext.minimumSdkVersion * 10000000 + ext.versionMajor * 10000 + ext.versionMinor * 100 + ext.versionPatch
}
private String generateVersionName() {
String versionName = "${ext.versionMajor}.${ext.versionMinor}.${ext.versionPatch}"
if (ext.versionClassifier == null) {
String gitBranch = getGitBranch()
Boolean isFeatureBranch = gitBranch != null && gitBranch.startsWith(featureBranchPrefix)
if (isFeatureBranch) {
ext.versionClassifier = gitBranch.replace(ext.featureBranchPrefix, "")
}
if (ext.isSnapshot) {
if (ext.versionClassifier == null) {
ext.versionClassifier = ""
} else {
ext.versionClassifier += "-"
}
ext.versionClassifier += "SNAPSHOT"
}
}
if (ext.versionClassifier != null) {
versionName += "-" + ext.versionClassifier
}
return versionName;
}
private String getGitBranch() {
String gitBranch
if (ext.has('GIT_BRANCH')) {
gitBranch = ext.get('GIT_BRANCH')
} else if (System.getenv().containsKey('GIT_BRANCH')) {
gitBranch = System.getenv('GIT_BRANCH')
} else {
gitBranch = 'git symbolic-ref HEAD'.execute().text
}
return gitBranch.trim().replace("origin/", "").replace("refs/heads/", "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment