Adding Jenkins build number into your Android application

Today I was trying to get the Jenkins build number into my Android app for display, however, I couldn’t find any tutorials on how to do this. I have pieced together a few things and put them together below.

Getting the Jenkins build number into Gradle build system

So, Android uses two fields for versioning, these appear in the app’s build.gradle file.

versionCode – The unique version that android uses to distinguish between versions of an app.

versionName – This is more of a human readable and flexible build number that is what I’d like to display. By default this is hard coded to “1.0” and I’d like to make it “1.0.” + JENKINS_BUILD_NUMBER.

Jenkins exposes the current build number as a system environment variable so we need to access this in the build script using the following code. Put this in the defaultConfig section for now (I’m sure there might be a better place but this works for me):

ext.buildNumber = System.getenv("BUILD_NUMBER") ?: "dev"

This code above sets the buildNumber variable to the jenkins build number if it is not null, otherwise it sets it to “dev”. I did this so that we can distinguish between development builds and continuous integration builds.

I then updated versionNumber with the following code:

versionName "0.1.$buildNumber"

This now makes the Jenkins build number and our application version accessible to our application.

Getting the version name from the build system into my app
If you are using the Gradle build system there is an addition class available to your application that allows you to get the configured version number.

Add BuildConfig.VERSION_NAME to where ever you need to retrieve the current version name from. For example, I have a TextView in the settings screen of my application and I set the version name with the code below.

final TextView versionName = (TextView) findViewById(;

That’s it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s