0.0.1, 0.0.2, 0.1.0, 0.1.1, 0.1.3,…. I’m super lazy, and I get bored really quickly with repeating tasks. So, whenever
I can offload something to a script, I just do it.
One such offloading I’ve done lately, is versioning the APK.
So, I’ve created a very simple Groovy plugin for Gradle, which will calculate the version-code and version-name based on the count of commits in the git repo. It’s is not the brightest method, but it is so damn simple, it has to work.. Right?
Plugin
Gradle can be extended using Groovy plugins (under the buildSrc
folder). We’ll use that to provide two static methods to our build.gradle
file:
This file should be saved as [project root]/buildSrc/src/main/groovy/versionbuilder/VersionBuilder.groovy
API
This is will give you two public static methods which you can use:
buildGitVersionNumber
- Returns an integer which can be used as your project’s versionCode.buildGitVersionName
- Returns a major.minor.build string which can be used as project’s versionName. Change the plugin’s code, if you prefer a different template for versioning.
Example
To use those methods, in your build.gradle
file:
Configuring
You probably noticed two constants in plugin’s code:
GIT_COMMIT_COUNT_NORMALIZE
- Use this to subtract a constant from the commits count. Use this to set the initial versionCode as close as possible to your current versionCode.GIT_COMMIT_COUNT_MINOR_NORMALIZE
- you’ll probably want to reset your build versioning every time you bump minor or major.
Things will just work from now on! No need to figure versions anymore, what a profit, ah?