Version number and code?

While building Android apps, there are 2 very important characteristics; the version number and version code. You define them in your app/build.gradle:

android {
defaultConfig {
versionName 1.2.3
versionCode 123

We typically want to change this version code so that it's clear that it's built by CI and by which pipeline. Also, the version code has to be higher then the previous release when uploading an app to the Play Store.

Via gradle.properties

We've been making Android apps for many years now, and typically defined the version code and name in the gradle.properties. This makes it easy to find and update manually or with a script.

projectVersionName=1.2.3
projectVersionCode=1230

In your app/build.gradle you can then use these properties:

android {
defaultConfig {
versionName projectVersionName
versionCode projectVersionCode

Gradle property

The next iteration, fueled by Fastlane documentation was to pass a gradle property when building your app.

So you build your app with:

./gradlew assembleRelease -PciVersionCode=1004 -PciVersionName="1.0.0.4"

Or if you use Fastlane:

gradle(
task: "assemble",
build_type: "release",
properties: {
"ciVersionCode" => 1001,
"ciVersionName" => "1.0.0.4",
}

In your app/build.gradle you then need to add some code to check if the property is present or not and use them accordingly:

def newVersionName
if (project.hasProperty('ciVersionName') && project.ciVersionName) {
newVersionName = project.ciVersionName
} else {
newVersionName = projectVersion
}
def actualVersionCode


android {
defaultConfig {
versionName newVersionName
versionCode newVersionCode

With a fastlane plugin

There is also a Fastlane plugin fastlane-plugin-versioning-android that actually rewrites your app/build.gradle file. I do think this is a cool approach, but I'm always careful not to use yet another dependency that isn't activily maintained.

Or, you can change it without writing code in your app/build.gradle!

While all of these previous solutions are nice, they require you to add code. And while I was reading through the Fastlane documentation I actually discovered that there is a baked-in gradle metbod that isn't very known at all.

After reading the fastlane gradle documentation, going through this never resolved issue, I then discovered this this stackoverflow thread. Somewhere at the bottom of it there is a small reply that you can actually use android.injected.version.code and android.injected.version.name to edit this without further configuration! All the constants that you can use this way look to be listed in this javadoc. Not the best reference, but after some testing I can conclude that this works wonderfully!

To summarize, if you want to change your android version code and name, you can now simply use these gradle properties and no longer have to write code in your app/build.gradle! I've also made a PR on the Fastlane documentation to make this clearer in their docs: https://github.com/fastlane/fastlane/pull/19370#issuecomment-923986665 (which has been merged :sparkles:).

Gradle property

./gradlew assembleRelease -Pandroid.injected.version.code=123145 -Pandroid.injected.version.name="1.2.3.145"

Fastlane

gradle(
task: "assemble",
build_type: "release",
properties: {
"android.injected.version.code" => "1.2.3.145",
"android.injected.version.name"=> 123456,
}
)