Fastlane Series: Let Google Play Pick Your Version Code

Fastlane is an open-source tool that automates building and releasing mobile apps. If you're new to it, start with the official Android setup guide.

This is part of a series about how we automated the distribution of our Android app with Fastlane. In this one: how we stopped bumping the version code by hand.

The problem

Every upload to Google Play needs a version code that is higher than all the previous ones. On our team, bumping it was a manual step, and manual steps go wrong:

  • Someone forgets to bump it, and Play Console rejects the upload.
  • Two developers bump it on different branches to the same number.
  • Someone releases from a laptop where they forgot to pull, so their "next" number is already taken.

Each of these costs a failed upload and another 10 minutes of building.

The idea

Google Play already knows every version code you've ever uploaded. So instead of tracking the number ourselves, we ask Google Play for the highest one and add 1. It doesn't matter whose laptop the release runs on, or which branch they pulled. The number is always correct.

Fastlane has a built-in action for exactly this: `google_play_track_version_codes`.

Step 1: keep the version in one file

Fastlane needs to read and write the version, so it can't be hardcoded in build.gradle.kts. We keep it in a plain version.properties file at the project root:

APP_VERSION_CODE=161
APP_VERSION_NAME=1.2.32

Gradle reads it from there:

// app/build.gradle.kts
val versionProps = java.util.Properties().apply {
    rootProject.file("version.properties").inputStream().use { load(it) }
}

android {
    defaultConfig {
        versionCode = versionProps.getProperty("APP_VERSION_CODE").toInt()
        versionName = versionProps.getProperty("APP_VERSION_NAME")
    }
}

Now both Gradle and Fastlane use the same file, so the version is always the same everywhere.

Step 2: ask Google Play for the next number

Put the logic in a small helper, fastlane/helpers/versions_helper.rb:

module VersionsHelper
  VERSION_FILE = "../version.properties"

  def self.latest_version_code
    Fastlane::Actions::GooglePlayTrackVersionCodesAction.run(
      track: "internal",
      package_name: "com.example.bank",
      json_key: "../Private/fastlane-service-account.json"
    ).max.to_i
  end

  def self.update_version_code
    props = read_properties(VERSION_FILE)
    props["APP_VERSION_CODE"] = latest_version_code + 1

    File.open(VERSION_FILE, "w") do |f|
      props.each { |key, value| f.puts "#{key}=#{value}" }
    end
  end

  def self.read_properties(path)
    File.readlines(path).each_with_object({}) do |line, props|
      next if line.strip.empty? || line.start_with?("#")
      key, value = line.split("=", 2)
      props[key.strip] = value.strip
    end
  end
end

.max.to_i also handles a brand-new app: if the track is empty, max returns nil, nil.to_i is 0, and the first version code becomes 1.

Load the helpers at the top of your Fastfile:

Dir[File.join(__dir__, "helpers", "*.rb")].sort.each { |file| load file }

Step 3: use it before every release build

lane :build do
  VersionsHelper.update_version_code
  gradle(task: "app:bundleRelease")
end

That's it. Every release build now gets the right version code without anyone thinking about it.

Why the version name stays manual

We only automate the version code. The version name (1.2.32) we still change by hand in version.properties. The code is just a counter, so a machine should own it. The name is a team decision — is this a patch, or a new feature release? — so a person should own it.

Takeaway

Don't keep a counter that someone else already keeps for you. Google Play knows which version codes are taken — ask it, add 1, and a whole category of failed uploads disappears.