Here are two quick tips that let you include your current Jenkins CI build number in both your .apk’s file name and/or the app label.

When Jenkins executes a job, it sets some environment variables that you can refer to in your build script. Here’s a full list of all the variables that are available: Jenkins Set Environment Variables

The one we’re interested in is called BUILD_NUMBER. And here’s how you can refer to it in your build script using the Groovy DSL:

Setting the APK name:

The default APK base name can be changed with the project’s archivesBaseName property:

defaultConfig { versionCode System.getenv("BUILD_NUMBER") as Integer ?: 0 versionName 1.0 project.ext.set("archivesBaseName", "MyApp-" + versionName + "-" + versionCode); }

This code snippet would produce the following APK names:

MyApp-1.0-55.apk for build no. 55 from Jenkins CI and

MyApp-1.0-0.apk for local builds from your IDE.

It’s as simple as that. You can use System.getenv("foobar") to grab any of the environment variables that are available.

Ok, now let’s change the app label. That’s the actual name of your app, the way you see it in the app launcher.

Setting the app label:

Let’s assume in your AndroidManifest.xml you are referring to the app label like this: android:label="@string/app_name" The Android Gradle plugin let’s you override resource values using the resValue command. So in order to include your current Jenkins build number in the app label you can use the following snippet:

resValue "string", "app_name", "MyApp " + (System.getenv("BUILD_NUMBER") ?: "IDE")

This will override the app_name resource with whatever you pass as the 3rd parameter. In our case that’s again the Jenkins build number (if available) or “IDE” for local builds. Note that you can use that command in both the defaultConfig, the buildTypes and the productFlavors closure. Pretty cool, eh?

Btw I’ve been using the Elvis operator to keep the commands nice and concise. Have you ever wondered why it’s called like that? Just turn your head to the left.

?: