versionCode & versionName Strategy
Android apps carry two versions, and they do different jobs:
versionCode |
versionName |
|
|---|---|---|
| Type | positive integer, at most 2100000000 | any string |
| Who reads it | the system and Google Play | humans (store listing, About screen, crash reports) |
| Rule | every upload to Play must be higher than all previous ones for that app; a device refuses to "update" to a lower one | none; 1.4.0, 2026.10, 1.4.0-beta.2 all work |
iOS has the same pair under different names: CFBundleShortVersionString (marketing version, MARKETING_VERSION in Xcode) and CFBundleVersion (build number, CURRENT_PROJECT_VERSION), and App Store Connect rejects a build number it has already seen for that version.
The hard part is the integer. Typing it by hand fails the first time two branches release on the same day, so release pipelines derive it.
Strategies
| Strategy | Example | Pros | Cons |
|---|---|---|---|
| CI build number | versionCode = GITHUB_RUN_NUMBER |
trivial, always increases per workflow | resets if the workflow is renamed or moved; unrelated to the version |
| Offset + build number | 10_000_000 + run |
survives migrating CI systems (bump the offset) | still opaque |
| From the semantic version | 1.4.2 → 1_04_02_00 (+ build in the last digits) |
readable; hotfix 1.4.3 beats 1.4.2 |
needs care with limits and with hotfixes on older lines |
| Commit count | git rev-list --count HEAD |
reproducible from the repo alone | breaks with squashed history, shallow clones, or release branches |
| Time-based | minutes since 2020-01-01 | monotonic without coordination | two builds in one minute collide; meaningless to humans |
Whatever you choose, the build must be reproducible: given the same commit and the same inputs (tag, CI build number), CI must produce the same versionCode. Never compute it from the wall clock inside Gradle.
A runnable model
This model encodes a semantic version and a build number into one integer, the way many teams do it, and checks the two rules that matter: it fits under Play's limit and it grows with every release in order.
PLAY_MAX = 2_100_000_000
def version_code(version, build, flavor_digit=0):
major, minor, patch = (int(x) for x in version.split("."))
assert minor < 100 and patch < 100 and build < 100 and flavor_digit < 10
# M.. MM PP BB F e.g. 1.4.2 build 7 -> 1 04 02 07 0
return (((major * 100 + minor) * 100 + patch) * 100 + build) * 10 + flavor_digit
releases = [("1.4.2", 7), ("1.4.3", 1), ("1.5.0", 3), ("2.0.0", 12)]
previous = 0
for version, build in releases:
code = version_code(version, build)
print(f"{version:<6} build {build:>2} -> {code:>10} {'OK' if previous < code <= PLAY_MAX else 'REJECTED'}")
previous = code
print("largest possible:", version_code("209.99.99", 99, 9))
print("fits:", version_code("209.99.99", 99, 9) <= PLAY_MAX)1.4.2 build 7 -> 10402070 OK
1.4.3 build 1 -> 10403010 OK
1.5.0 build 3 -> 10500030 OK
2.0.0 build 12 -> 20000120 OK
largest possible: 2099999999
fits: True
The trailing digit leaves room for per-flavor or per-ABI codes when one release ships several APKs (each must have a distinct code). The budget is finite: majors can go up to 209 before the code passes Play's limit. That's plenty, but every extra digit you add (a second flavor digit, a three-digit build number) divides it by ten, so check the arithmetic before changing the scheme.
Wiring it into Gradle
Pass the inputs in from CI as Gradle properties, with safe defaults for local builds. Here the version name comes from the latest Git tag and the code from a buildNumber property:
mkdir -p hello/app/src/main && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
// "v1.4.2" -> "1.4.2"; untagged checkouts get "0.0.0"
val gitVersion = providers.exec {
commandLine("git", "describe", "--tags", "--abbrev=0", "--match", "v*")
isIgnoreExitValue = true
}.standardOutput.asText.map { it.trim().removePrefix("v").ifEmpty { "0.0.0" } }
val buildNumber = providers.gradleProperty("buildNumber").map { it.toInt() }.orElse(0)
fun versionCodeOf(version: String, build: Int): Int {
val (major, minor, patch) = version.split(".").map { it.toInt() }
require(minor < 100 && patch < 100 && build < 100) { "version $version build $build out of range" }
return (((major * 100 + minor) * 100 + patch) * 100 + build) * 10
}
android {
namespace = "com.example.hello"
compileSdk = 36
defaultConfig {
applicationId = "com.example.hello"
minSdk = 24
targetSdk = 36
versionName = gitVersion.get()
versionCode = versionCodeOf(gitVersion.get(), buildNumber.get()).coerceAtLeast(1) // AGP rejects 0
}
}
KTS
gradle wrapper > /dev/null
git init -q && git add -A && git commit -q -m "first"
badging() { aapt2 dump badging app/build/outputs/apk/debug/app-debug.apk | grep -oE "versionCode='[0-9]*' versionName='[^']*'"; }
./gradlew -q assembleDebug && echo "untagged: $(badging)"
git tag v1.4.2
./gradlew -q assembleDebug -PbuildNumber=7 && echo "v1.4.2, build 7: $(badging)"
git commit -q --allow-empty -m "hotfix" && git tag v1.4.3
ORG_GRADLE_PROJECT_buildNumber=1 ./gradlew -q assembleDebug && echo "v1.4.3, build 1: $(badging)"
./gradlew -q assembleDebug -PbuildNumber=100 2>&1 | grep -A1 'What went wrong'untagged: versionCode='1' versionName='0.0.0'
v1.4.2, build 7: versionCode='10402070' versionName='1.4.2'
v1.4.3, build 1: versionCode='10403010' versionName='1.4.3'
* What went wrong:
version 1.4.3 build 100 out of range
Three details make this CI-friendly:
providers.execrunsgitin a way the configuration cache understands (it re-runs when its output could change). A plainProcessBuilderin a build script breaks the configuration cache.- The property, not the environment, is the interface.
-PbuildNumber=7andORG_GRADLE_PROJECT_buildNumber=1are equivalent, so CI can use whichever is easier; local builds default to 0. - Out-of-range inputs fail the build instead of silently producing a smaller code than the previous release.
An untagged checkout would compute 0, which AGP refuses (versionCode is set to 0, but it should be a positive integer), hence the coerceAtLeast(1): fine for a developer build, useless for a release. A shallow clone in CI also has no tags, which is a classic way to ship 0.0.0: fetch tags (fetch-depth: 0 or git fetch --tags in GitHub Actions) in release jobs.
Test yourself
Which rule does Google Play enforce for versionCode?
- It must equal the versionName
- Each upload must be higher than every previous one for that app, and at most 2100000000
- It must be a date
- It must be even
Why compute versionCode from inputs like a tag and a CI build number rather than from the current time inside Gradle?
- Gradle has no clock
- The same commit and inputs should always produce the same artifact (reproducibility)
- Time-based codes are too small
- Play rejects time-based codes
A release job prints versionName 0.0.0. What's the most likely cause?
- The keystore is wrong
- A shallow CI checkout without tags, so git describe found nothing
- R8 removed the version
- The configuration cache