Sub topic 4
Versioning with Version Name and Build Number
Every Flutter release carries two version values. The version name is what users see. The build number is what the stores use to tell uploads apart. This chapter covers where each value lives, how Flutter passes it to Android and iOS, and the mistakes that cause rejected uploads.
It assumes you can edit pubspec.yaml, run flutter build from a terminal, and have already set the application ID and bundle identifier from the previous chapter.
The single source of truth: pubspec.yaml
A Flutter project keeps its version in one line of pubspec.yaml:
# pubspec.yaml
name: my_app
description: Example app
version: 1.2.0+7The value has two parts, separated by a plus sign:
1.2.0is the version name. Users see it in the store listing and in the system settings. It follows the patternmajor.minor.patch.7is the build number. The stores use it to order uploads. Users rarely see it.
Flutter writes these values into the native projects every time you build. You do not edit the version in Android Studio or Xcode by hand.
| Part | Android field | iOS field |
|---|---|---|
| Version name (`1.2.0`) | `versionName` | `CFBundleShortVersionString` |
| Build number (`7`) | `versionCode` | `CFBundleVersion` |
The Android values are read from android/local.properties through the Gradle properties flutter.versionName and flutter.versionCode. The iOS values flow through the build settings FLUTTER_BUILD_NAME and FLUTTER_BUILD_NUMBER, which Xcode substitutes into Info.plist. A new project template already contains this wiring.
What each store requires
The two stores enforce different rules, and both reject an upload that breaks them.
Google Play
versionCodemust be an integer.- Each upload to a given app must have a
versionCodehigher than every earlier upload. Play checks this across all tracks, including internal testing. - The maximum value is 2,100,000,000.
versionNameis free text for display. Play does not use it to order releases.
App Store Connect
CFBundleShortVersionStringmust be one to three period-separated integers, such as1.2.0. Suffixes like-betaare not allowed.CFBundleVersionmust increase for each build uploaded under the same version name. You may reuse a build number under a different version name, but a monotonically increasing number is simpler to reason about.- Once a version name is approved and released, you cannot submit new builds under it. The next release needs a higher version name.
The safest policy for both platforms is to treat the build number as a counter that only goes up and never resets.
Overriding the version from the command line
You can override the pubspec.yaml value for a single build with two flags:
flutter build appbundle --release \
--build-name=1.2.1 \
--build-number=8The flags take precedence over the file for that build only. They do not edit pubspec.yaml. This is the usual way for a CI system to inject a build number without committing a change (see the CI chapter later in this tutorial).
If you pass only --build-number, Flutter takes the version name from pubspec.yaml. If you omit the +N part in pubspec.yaml and pass no flag, Flutter uses the default build number, which is 1. That default causes "version code already used" errors on the second upload, so always set a number.
Choosing a versioning scheme
Pick a scheme before your first release. Changing it later is awkward, because the build number cannot go down.
| Scheme | Example | Strengths | Weaknesses |
|---|---|---|---|
| Manual counter | `1.2.0+7` | Simple, visible in the repository | Easy to forget to bump |
| CI run number | `--build-number=$RUN_NUMBER` | Always increases, no commits needed | Tied to one CI system; resets if you migrate |
| Timestamp | `--build-number=2611282130` | Unique and ordered | Must stay under the 2.1 billion Android limit |
| Encoded version | `10200007` for 1.2.0 build 7 | Build number reflects the version | Needs a script; limits each field's range |
A common choice is the CI run number for pre-release builds and a tagged version name for releases. Whatever you choose, document it in the repository so every contributor bumps values the same way.
Semantic versioning for the version name
The version name works well with semantic versioning:
- Increase patch (
1.2.0to1.2.1) for bug fixes. - Increase minor (
1.2.0to1.3.0) for new features that do not change existing behavior. - Increase major (
1.2.0to2.0.0) for large changes, such as a redesign.
Because the stores only look at the build number for ordering, the version name is a communication tool for users and your team.
Reading the version inside the app
Sometimes the app needs to show its own version, for example on an About screen or in a crash report. The package_info_plus package reads the values from the installed app:
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
void main() => runApp(const MaterialApp(home: AboutScreen()));
class AboutScreen extends StatelessWidget {
const AboutScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('About')),
body: Center(
child: FutureBuilder<PackageInfo>(
future: PackageInfo.fromPlatform(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
final info = snapshot.data!;
return Text('Version ${info.version} (${info.buildNumber})');
},
),
),
);
}
}Add the package with flutter pub add package_info_plus. Here info.version is the version name and info.buildNumber is the build number, as a string. The value reflects the build, not the source file, so it is correct even when you used --build-number.
Common errors and fixes
- "Version code 7 has already been used." Google Play already holds an upload with that
versionCode. Increase the build number and rebuild. Deleting a draft release does not free the number. - The build number did not change after editing
pubspec.yaml. Runflutter clean, then build again. Stale values inandroid/local.propertiesor the iOSGenerated.xcconfigfile can persist until Flutter regenerates them. - App Store Connect says the bundle version must be higher. The
CFBundleVersionis not greater than a previous build for the same version name. Bump the number. - Archiving from Xcode shows an old version. Xcode reads
Generated.xcconfig. Runflutter build ios --config-onlyor any Flutter build first so the file is regenerated, then archive. - Hard-coded values in native files. If someone typed a number into
build.gradle.ktsor the Xcode General tab, Flutter's value is ignored. Restore the references toflutter.versionCode,flutter.versionName,$(FLUTTER_BUILD_NUMBER)and$(FLUTTER_BUILD_NAME).
Checking the version of a built artifact
Confirm what you are about to upload instead of trusting the source file. For an Android APK, the Android SDK's aapt2 tool prints the values:
aapt2 dump badging build/app/outputs/flutter-apk/app-release.apk | grep versionCodeThe output includes versionCode='7' and versionName='1.2.0'. For an iOS build, open the generated Info.plist inside the archive, or check the version in Xcode's Organizer after archiving.
Exercise
Your pubspec.yaml says version: 2.0.0+15. Google Play rejects your next upload with "Version code 15 has already been used." You fix a bug and want to ship it as a patch. What do you change, and which value does Play compare?
Show answer
Change the line to version: 2.0.1+16 (or build with --build-number=16). Play compares only the build number, which becomes versionCode, and it must be higher than every earlier upload. The version name 2.0.1 is for users and for your own records.
Exercise
A teammate sets --build-number=2611282130 (a timestamp with minute precision) and the Play upload fails on the next day's build, which uses --build-number=2611290900. Why does the first one work and the second one fail, or does the failure have another cause?
Show answer
Both numbers are below the 2,100,000,000 limit only if they stay under it, and 2,611,282,130 is already above it. The first value exceeds the maximum versionCode, so it is the one that fails. A shorter timestamp, such as year, month, day and hour as 26112821, stays within range and still increases.