Increment Android build number in Continuous Integration Increment Android build number in Continuous Integration jenkins jenkins

Increment Android build number in Continuous Integration


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 codeext.versionMajor = 1ext.versionMinor = 2android {  defaultConfig {    versionName computeVersionName()    versionCode computeVersionCode()  }}// Will return "1.2" in this exampledef computeVersionName() {    // Basic <major>.<minor> version name    return String.format('%d.%d', versionMajor, versionMinor)}// Will return 120042 for Jenkins build #42def 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.