I'm using Android Studio, Gradle, git.
On every push to the master branch I want to build a new App version that's uploaded to the Alpha channel of the Google Play Store. For this I need to increase the versionCode of the App for every build.
For this I seem to have several choices:
Currently, I like 3 the best.
Does anybody have a definite way of doing it?
versionCode — A positive integer used as an internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions. This is not the version number shown to users; that number is set by the versionName setting, below.
Continuous integration systems let you automatically build and test your app every time you check in updates to your source control system. You can use any continuous integration tool that can initiate a Gradle build to build your Android Studio projects.
I use none of the above — like you, I don't want to alter the repo for each build, nor any files.
Jenkins has an always-increasing value for each build, exposed via the BUILD_NUMBER
environment variable.
In Gradle, I generate the versionCode
value programmatically at build time, using the BUILD_NUMBER
value to ensure that the versionCode
is always higher than the previous build.
A snippet of my build.gradle
:
// Used to set the package version name and version code
ext.versionMajor = 1
ext.versionMinor = 2
android {
defaultConfig {
versionName computeVersionName()
versionCode computeVersionCode()
}
}
// Will return "1.2" in this example
def computeVersionName() {
// Basic <major>.<minor> version name
return String.format('%d.%d', versionMajor, versionMinor)
}
// Will return 120042 for Jenkins build #42
def computeVersionCode() {
// Major + minor + Jenkins build number (where available)
return (versionMajor * 100000)
+ (versionMinor * 10000)
+ Integer.valueOf(System.env.BUILD_NUMBER ?: 0)
}
So I only need to update the two values at the top when making a release build. For all other build types, I can let Gradle/Jenkins automatically set the versionCode
and then upload to Google Play.
This also means, for any alpha version listed on the Play Store, or by inspecting an APK, I can see straight away which Jenkins build it came from, and from there the git commit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With