How to Set Build and Version Number of Flutter App

In this blog post, we will walk you through the process of setting the build and version number of your Flutter app. We will cover how to update both the version name and the version code number in the same place in pubspec.yaml, as well as re-enabling auto-versioning if it’s not automatically updating anymore.

Setting the Version and Build Number

You can update both the version name and the version code number in the same place in pubspec.yaml. Just separate them with a + sign. For example:

version: 2.0.0+8

This means that:

  • The version name is 2.0.0
  • The version code is 8

This is described in the documentation of a new project (but you might have deleted that if you are working on an old project):

The following defines the version and build number for your application.
A version number is three numbers separated by dots, like 1.2.43
followed by an optional build number separated by a +.
Both the version and the builder number may be overridden in flutter
build by specifying –build-name and –build-number, respectively.
Read more about versioning at semver.org.

Example Version Number

An example of how to set the version number would be:

version: 1.0.0+1

Re-enabling Auto Versioning

If your Flutter versioning is not automatically updating anymore, you can re-enable it by following these steps:

iOS Re-Enable Auto Versioning

Open the ios/Runner/Info.plist file. Set the value for CFBundleVersion to $(FLUTTER_BUILD_NUMBER) and set the value for CFBundleShortVersionString to $(FLUTTER_BUILD_NAME). The XML for the file should look something like this:





    ...
    CFBundleVersion
    $(FLUTTER_BUILD_NUMBER)
    CFBundleShortVersionString
    $(FLUTTER_BUILD_NAME)
    ...

...

Android Re-Enable Auto Versioning

Open the android/app/build.gradle file. Ensure you are properly loading the Flutter properties at the top of the file:

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    throw new GradleException("versionCode not found. Define flutter.versionCode in the local.properties file.")
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    throw new GradleException("versionName not found. Define flutter.versionName in the local.properties file.")
}

Then set the android.defaultConfig section so that versionName is flutterVersionName and versionCode is flutterVersionCode.toInteger():

android {
    ...
    defaultConfig {
        ...
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }
}

Conclusion

In this blog post, we covered how to set the build and version number of your Flutter app by updating both the version name and the version code number in the same place in pubspec.yaml. We also walked you through re-enabling auto-versioning if it’s not automatically updating anymore.