Auto-versioning Flutter apps

By default a Flutter project is set up to automatically update the Android and iOS version on the version setting in pubspec.yaml when you build the project.

This configuration is useless since is already done. But if you need to re-enable this setting follow these instructions.

Both iOS / Android version can be set from pubspec.yaml

version: 1.2.0+1

1.2.0 is Version Name and +1 is Version code. Changes will reflect for both native Android and iOS version name and version code.

iOS

  • Open the ios/Runner/Info.plist file.
  • Set the value for CFBundleVersion to $(FLUTTER_BUILD_NUMBER)
  • Set the value for CFBundleShortVersionString to $(FLUTTER_BUILD_NAME)

The XML for the file should look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  ...
  <key>CFBundleVersion</key>
  <string>$(FLUTTER_BUILD_NUMBER)</string>
  <key>CFBundleShortVersionString</key>
  <string>$(FLUTTER_BUILD_NAME)</string>
  ...
</dict>
...

Flutter will generate a ios/Flutter/Generated.xcconfig file that declares these variables.

  • Open the project in Xcode, select the Build Settings tab of the Runner target and search for versioning. Change the value of Current Project Version to $(FLUTTER_BUILD_NUMBER). image.png

Android

  • 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')

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 both versionName and versionCode values.
android {
  ...
  defaultConfig {
    ...
        // it uses 1 as default value
    versionCode flutterVersionCode?.toInteger() ?: 1
    versionName flutterVersionName
  }
}

Flutter will generate a android/local.properties file that declares these variables.

After these changes you can change pubspec version attribute and build your app with updated version name and build number.

Bonus

Do you want to show this Version Name somewhere in your app?

Try this package https://pub.dev/packages/package_info_plus