Versioning and release management
Why it matters
- Google Play refuses an upload whose
versionCodewas already used for the app, and Android refuses to install a lowerversionCodeover a higher one. A versioning mistake stops a release at the last step. - Libraries need semantic versioning so consumers know when an update can break them. A breaking change shipped as a minor version shows up as
NoSuchMethodErrororNoClassDefFoundErrorat runtime in someone else's app. - Interviewers ask how you keep
versionCodeincreasing across release branches and hotfixes, and how you map a version name to a version code without collisions. - Versioning ties the Delivery lessons together: CI stamps the version code, the same build is promoted across Play tracks, and an in-app update is offered only when the new
versionCodeis higher than the installed one.
The simple explanation
versionCodeis a ticket number at a deli counter: every new ticket is higher, and a number is never handed out twice.versionNameis the name on the ticket: people read it, the machine ignores it.- A library's semantic version is a promise to its users: a new major version means "this may not fit your code any more".
- Promoting a release through tracks is moving the same ticket to a different queue; it keeps its number.
What are versionCode and versionName in Android? Both are set in the app module's defaultConfig in build.gradle.kts and written into the manifest. versionCode is a positive integer used to decide which build is newer: Android uses it to block downgrades, Google Play will not accept an upload with a versionCode already used for the app, and the highest value Play allows is 2100000000. versionName is a free-form string shown to users, such as "2.4.1"; the system never compares it.
Version codes are unique per app, not per track. When you promote a release from internal testing to production, it is the same build with the same versionCode. Different tracks can hold different builds at the same time (for example 310 on internal and 300 on production); a user eligible for several tracks gets the highest versionCode available to them, which is why a testing build should have a higher code than production.
Semantic versioning (MAJOR.MINOR.PATCH) is for libraries, including your own modules published to an internal Maven repository: MAJOR for breaking API changes, MINOR for backward-compatible features, PATCH for backward-compatible fixes. Apps often use the same shape for versionName because it is familiar, but Play does not enforce any format.
| Field | Type | Who reads it | Rule |
|---|---|---|---|
versionCode | Int | Android, Google Play | positive, never reused, higher for each release |
versionName | String | users, Play listing | free text |
| Library version | String | Gradle dependency resolution | semantic versioning by convention |
Release management. In trunk-based development everyone merges small changes to main, CI stamps the versionCode from one counter, and a release is a tag on main (or a short-lived branch cut from it for stabilisation). With long-lived release branches, each branch needs its own non-overlapping versionCode range, and hotfixes must be merged back to main so the fix is not lost in the next release. Either way, write release notes for the Play listing's "What's new" text and tag the commit each versionCode was built from.
Notes
versionCodemust be a positive integer, never reused for the app, and higher than the version users have installed for them to get the update. Play's maximum is 2100000000.versionNameis a free-form string shown to users. Android and Play never compare it.- Promotion keeps the
versionCode: the release on production is the same build that was on internal or closed testing. - Keep testing tracks on a higher
versionCodethan production, otherwise testers who are also eligible for production get the production build. - CI should stamp
versionCode(from a build counter or a git tag), so developers never hand-edit it on several branches. - A derived scheme like
major * 10000 + minor * 100 + patchis readable but breaks ifminororpatchreaches 100. A plain counter has no such limit. - Semantic versioning: a
MAJORbump signals a breaking change. For Kotlin libraries, the binary-compatibility-validator plugin (or the Kotlin Gradle plugin's ABI validation) can fail the build when the public API changes unexpectedly. - Gradle resolves a dependency conflict to the highest requested version by default. If a library's major version removed an API another dependency uses, the app can compile and still crash at runtime.
- Keep dependency versions in one version catalog (
gradle/libs.versions.toml) so modules do not drift. - An in-app update via
AppUpdateManageris offered only when Play has a version with a higherversionCodethat this user can get.
Code example
This script bumps versionName by a semantic version part in gradle.properties. versionCode comes from CI instead, so it never depends on a file someone has to remember to edit.
#!/usr/bin/env bash
set -euo pipefail
# Usage: ./bump.sh [major|minor|patch]
PROPS="gradle.properties"
BUMP="${1:-patch}"
CUR_NAME=$(grep '^versionName=' "$PROPS" | cut -d= -f2)
IFS='.' read -r MAJ MIN PAT <<< "$CUR_NAME"
case "$BUMP" in
major) MAJ=$((MAJ + 1)); MIN=0; PAT=0 ;;
minor) MIN=$((MIN + 1)); PAT=0 ;;
patch) PAT=$((PAT + 1)) ;;
*) echo "Unknown bump type: $BUMP" >&2; exit 1 ;;
esac
NEW_NAME="${MAJ}.${MIN}.${PAT}"
# Portable in-place edit (works with GNU and BSD tools)
perl -pi -e "s/^versionName=.*/versionName=${NEW_NAME}/" "$PROPS"
git commit -am "Release ${NEW_NAME}"
git tag "v${NEW_NAME}"
echo "Tagged v${NEW_NAME}; CI builds the release from this tag"// app/build.gradle.kts
android {
defaultConfig {
// CI sets VERSION_CODE (for example from its build number); local builds use 1.
versionCode = providers.environmentVariable("VERSION_CODE").orNull?.toInt() ?: 1
versionName = providers.gradleProperty("versionName").get()
}
}versionNamelives ingradle.propertiesand is read withproviders.gradleProperty, so there is one place to change it.versionCodecomes from the CI environment, which increases on every run, so two branches cannot produce the same code from a stale file.- The tag records which commit each release came from, which is what you need when a crash report names a version.
perl -pi -ebehaves the same on Linux and macOS, unlikesed -i.
Run the idea in plain Kotlin
data class SemVer(val major: Int, val minor: Int, val patch: Int) : Comparable<SemVer> {
override fun compareTo(other: SemVer): Int =
compareValuesBy(this, other, { it.major }, { it.minor }, { it.patch })
override fun toString(): String = "$major.$minor.$patch"
}
fun parseVersion(s: String): SemVer {
val parts = s.split(".").map { it.toInt() }
return SemVer(parts[0], parts[1], parts[2])
}
fun bump(v: SemVer, type: String): SemVer = when (type) {
"major" -> SemVer(v.major + 1, 0, 0)
"minor" -> SemVer(v.major, v.minor + 1, 0)
"patch" -> SemVer(v.major, v.minor, v.patch + 1)
else -> v
}
fun derivedCode(v: SemVer): Int = v.major * 10000 + v.minor * 100 + v.patch
fun main() {
val current = parseVersion("2.4.1")
println("Current " + current + ", derived code " + derivedCode(current))
for (type in listOf("patch", "minor", "major")) {
val next = bump(current, type)
println(type + " -> " + next + ", derived code " + derivedCode(next))
}
println("As strings, \"1.10.0\" > \"1.9.0\": " + ("1.10.0" > "1.9.0"))
println("As SemVer, 1.10.0 > 1.9.0: " + (parseVersion("1.10.0") > parseVersion("1.9.0")))
val a = derivedCode(SemVer(1, 0, 100))
val b = derivedCode(SemVer(1, 1, 0))
println("1.0.100 and 1.1.0 both map to " + a + ": " + (a == b))
}Expected output:
Current 2.4.1, derived code 20401
patch -> 2.4.2, derived code 20402
minor -> 2.5.0, derived code 20500
major -> 3.0.0, derived code 30000
As strings, "1.10.0" > "1.9.0": false
As SemVer, 1.10.0 > 1.9.0: true
1.0.100 and 1.1.0 both map to 10100: true
In production
- A release branch was cut two weeks before release with
versionCode312 hardcoded inbuild.gradle.kts. - Meanwhile a hotfix built from
mainshipped to production as 314. - The release build carried 312, lower than the 314 users already had, so Android would never install it over their version. The problem surfaced only at upload time, and the release waited while the team rebuilt with a higher code.
versionCodeis now stamped by CI from a single increasing counter, so every build is higher than anything built before it, whichever branch it came from.- The release job compares the new code with the highest code on production (from Play Console or the Play Developer API
edits.tracksresource) and fails if it is not higher. - Hotfixes merge to
mainfirst and are cherry-picked to the release branch, so both lines carry the fix.
- An internal
:analyticslibrary removed a deprecated method and was published as1.5.0instead of2.0.0. - The app depended on
1.5.0directly, and on another internal library compiled against1.4.2that still called the removed method. Gradle resolved both to1.5.0. - The app compiled, and crashed at runtime with
NoSuchMethodErroron the screen that used the other library.
- The change was republished as
2.0.0, and the removal now follows a deprecation cycle: deprecate in a minor version, remove in the next major. - The library build runs the Kotlin binary-compatibility-validator, so an unplanned public API change fails CI.
- Dependency versions moved into
gradle/libs.versions.toml, and the team reviews./gradlew :app:dependenciesoutput when a library's major version changes.
Common mistakes
- Hand-editing versionCode on several branches. Two builds get the same code (rejected) or a lower one (never installed). Let CI stamp it from one counter.
- Using versionName for comparisons. It is free text, and as strings
"2.10.0"sorts before"2.9.0". CompareversionCodein code. - Promoting by rebuilding. Rebuilding for production creates a different build with a new code than the one testers checked. Promote the same release between tracks.
- A derived versionCode scheme without room.
major * 100 + minor * 10 + patchbreaks at2.10.0. Leave enough digits per part, or use a plain counter. - Breaking API changes in a minor library version. Consumers update expecting compatibility and get runtime crashes. Bump
MAJORfor breaking changes. - Long-lived release branches without merge-back. A hotfix on the branch that never reaches
maincomes back as a regression in the next release.
Interview questions
Answer out loud first. Then reveal the answer and compare it at each depth.
“What is the difference between versionCode and versionName, and what happens if you get versionCode wrong?”
“How do you manage versions across a multi-module Android project?”
“Release branches versus trunk-based development: which do you choose and why?”
“How does versionCode interact with Play tracks, staged rollouts and in-app updates?”
Quiz
You upload an .aab whose versionCode was already used for an earlier release of the app. What happens?
In semantic versioning, which part do you increment for a backward-compatible bug fix?
You promote a release from internal testing to production. What is its versionCode on production?
Production users are on versionCode 314. You ship a build with versionCode 312. What happens for them?
0 of 4 answered
Mini task
Let CI own versionCode (15 min)
-
In
gradle.properties, addversionName=1.0.0. Inapp/build.gradle.kts, setversionName = providers.gradleProperty("versionName").get(). -
Set
versionCode = providers.environmentVariable("VERSION_CODE").orNull?.toInt() ?: 1. Remove any otherversionCodeorversionNameassignment (search withgrep -rn 'versionCode\|versionName' --include='*.kts'). -
Run
VERSION_CODE=42 ./gradlew bundleRelease, then check the bundle's manifest:bundletool dump manifest --bundle=app/build/outputs/bundle/release/app-release.aab --xpath=/manifest/@android:versionCodeshould print42. -
In your CI workflow, pass the build counter (for example
github.run_numberplus an offset above your current highest code) asVERSION_CODE. -
Add the bump script from the example and run
./bump.sh minor. Confirmgradle.propertiesnow says1.1.0and av1.1.0tag exists.
Done when: no file hardcodes versionCode, a local build defaults to 1, a CI build gets its code from the build counter, and versionName changes in exactly one place.