-
-
Save yeho/dd52ead9d9bde7d560c496df2f912628 to your computer and use it in GitHub Desktop.
Versioning Android apps + Feature Branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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