Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment Android build number in Continuous Integration

Tags:

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:

  1. Increase the number during the Jenkins build and push the changed file back to the repo.
    • I don't like the idea of having my build server doing pushes/commits.
  2. Increasing the number with a git hook.
    • I like the idea even less to use git for modifying files. If I'm on vacation who (in the rest of the team) would ever find this and know what to do with it?
  3. Have a file on another server (which the build server can access directly) wherin the current version is saved. Gradle will access it, increase it & save it again.
    • This seems 'fragile' but at least I could have an easy 'increaseVersion' task that is only executed on the server. And if it breaks (if we move servers or something) it's fairly easy to repair.

Currently, I like 3 the best.

Does anybody have a definite way of doing it?

like image 323
fancy Avatar asked May 11 '15 15:05

fancy


People also ask

What is versionCode in Android?

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.

What is continuous integration in Android?

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.


1 Answers

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.

like image 131
Christopher Orr Avatar answered Oct 06 '22 02:10

Christopher Orr