101

How do you set the version name and version code of a Flutter app without having to go into the Android and iOS settings?

In my pubspec.yaml I have

version: 2.0.0

but I don't see a place for the build number.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

8 Answers8

171

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

  • 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.

version: 1.0.0+1

Re-enabling auto versioning

If your Flutter versioning is not automatically updating anymore, see this answer for how to fix it.

See also:

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 3
    I have the same problem and no matter what I put in this version field or as --build-number or --build-name parameters for flutter build, the app-release.apk has always version code 1 (and so Google Play rejects it upon subsequent uploads). The versionCode 1 and versionName 1.0.0 are also visible in output.json file in release directory. – camcam Jan 25 '19 at 14:46
  • 1
    I solved the problem by fixing some "cannot load Android facet" plugin errors which I had in Android Studio and deleted the build directory. After that, versions from pubspec.yaml are respected by flutter build. – camcam Jan 25 '19 at 17:27
  • 6
    I have had annoying experiences where the build number specified in my pubspec.yaml has not updated in the iOS release, and I only notice when I have waited for a new archive build, which then displays the old build number. Now I always double-check the build ver in the info.plist before doing a new iOS release build. – CoastalB Sep 18 '19 at 09:15
  • "... _optional_ build number separated by a +." What happens if we omit it? Does Flutter then require `--build-number`, or default to 1, or does it come up with a build number of its own somehow? – Thomas May 27 '20 at 09:41
  • @Thomas, Good question. I don't know off hand. I always include the build number. One way to check would be to see how the Android or iOS build number changes if you remove it. The Android build number is generated from it in the `android/local.properties` file. – Suragch May 27 '20 at 14:20
  • Is it safe to use this? or better to modify in both Android and iOS separately? – Alan Steiman May 30 '20 at 12:48
  • 1
    @AlanSteiman, It is safe to use it. You can always double check the values when you are about to publish your app. – Suragch May 31 '20 at 02:53
  • 1
    `By default a Flutter project is set up to automatically update the Android and iOS settings` What if at some point in your project's development cycle that process got deleted/overwritten? How do you re-enable that behavior (without rebuilding the entire project)? – Abion47 Aug 28 '20 at 20:17
  • @Abion47, I'm not sure about re-enabling it, but in one of my projects Android and iOS version numbers got out of sync so I just updated the version numbers in iOS before publishing. – Suragch Aug 29 '20 at 07:35
  • I found a Medium article describing the process of re-enabling it on iOS and Android. Would you mind if I edited your answer to include that information? – Abion47 Aug 29 '20 at 18:17
  • @Abion47 That would be great. Thank you. No need to ask if you can edit my answers. I'm glad when people improve them. It's one of the ways I learn more. – Suragch Aug 30 '20 at 00:46
  • IMHO "Define flutter.versionCode in the local.properties file." should be changed to "Define it using pubspec.yaml"? – ch271828n Sep 15 '20 at 00:18
  • @ch271828n, That was added as an edit to this answer when auto enabling stops working, so I'm not too sure about it myself. For a normal project you would just add it in pubspec.yaml. – Suragch Sep 15 '20 at 05:25
  • @Suragch I also think should be in pubspec. I think who edits it just copy-paste and forgets to modify that sentence :) – ch271828n Sep 15 '20 at 09:56
  • @Abion47, I didn't know enough about the details of the edit you added to keep it up to date (see subsequent comments), so for now I've just linked to the article you mentioned. If you would still like the content on this page, could you add a new answer to the question? (You can go to the edit history to copy your original edit and paste it into a new answer.) – Suragch Sep 16 '20 at 01:26
  • @Suragch I could, but your answer already has so many votes that a new answer wouldn't really get the info out there. That person's point was toward the error message that gets shown when the version info can't be loaded, which shouldn't be a problem unless the person really screwed with their project hierarchy. It's an error message from early on in the Flutter days so it should be updated, but it's also a bit of a nitpicky complaint. (You could've just @-ed me so I could come back and answer the question instead of deleting all that info...) – Abion47 Sep 16 '20 at 05:12
  • @Abion47, I added your answer as a new answer so that the information isn't lost. I also linked to it here so that users who need that info can easily find it. Feel free to edit it to fix any errors. – Suragch Sep 16 '20 at 06:36
  • I can't add another extra part in version name like this 1.0.0.1+1 , getting invalid version number error , i edited the pubspec.yaml – Md. Nahiduzzaman Rose Sep 15 '22 at 14:10
85

Thanks to user abion47 for finding and condensing the following answer from the article Versioning with Flutter.

Re-enabling auto versioning

By default a Flutter project is set up to automatically update the Android and iOS settings based on the version setting in pubspec.yaml when you build the project. If, however, you have since overridden those settings, you can re-enable that behavior by doing the following:

iOS

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:

<?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>
...

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')
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
  }
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 1
    After updating **pubspec.yaml** file I have checked *local.properties* but it shows default flutter.versionName=1.0.0 & flutter.versionCode=1. And it is the default gradle structure that you have shown above. So what's new here? – Sumit Shukla Sep 17 '20 at 05:19
  • @SumitShukla, I'm not really sure. This was originally added to my main answer above (see the edit history and comments), but I wasn't able to maintain it when questions arose because I didn't have any experience with this myself. So I broke it off into a new answer. – Suragch Sep 17 '20 at 09:51
  • if it doesn't work in ios, see https://stackoverflow.com/a/67108518/10148104 – nashihu Jan 25 '23 at 05:55
15

For me my version was

version: 1.0.0+1

and i changed it to

version: 1.2.0+1

then in terminal

flutter build appbundle --build-name=1.2.0+1 --build-number=2
Yusuf Amr
  • 487
  • 6
  • 5
  • 8
    The `+1` at the end of your version is the build number while the name is generally in the form `x.y.z`. So I would use `version: 1.2.0+2` and `flutter build appbundle --build-name=1.2.0 --build-number=2` – Suragch Oct 28 '21 at 06:31
9

For me, what worked was to set the correct version in the pubspec.yaml file and then run:

flutter clean

and then:

flutter pub get

flutter clean really does the trick.

You should also run:

pod install

at your /ios directory

MendelG
  • 14,885
  • 4
  • 25
  • 52
  • 1
    `flutter clean` definitely does the job. Been trying everything but it wasn't until I used this command that it actually worked out – ArtemNovikov May 12 '23 at 14:12
6

What worked for me (Android) :

  • Update pubspec.yaml inside flutter project file for versionname and versioncode.
  • Run flutter pub get on terminal.
  • Run your project in a device.
  • Open android module in Andriod Studio and check local.properties for updated flutter.versionName flutter.versionCode file in gradle folder.
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
4

In Android, build-name is used as versionName while build-number used as versionCode. Read more about Android versioning at https://developer.android.com/studio/publish/versioning In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. Read more about iOS versioning at https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html

This above information you can get in pubsec.yaml

You will find versionCode or buildnumber (for android) in your android/app/build.gradle.

defaultConfig {
        applicationId "com.fabio.resturanto"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true

    }

You have flutterVersionCode and flutterVersionName in your local.properties file. This is specific to Android build

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

version: 1.2.0+1

Here 1.2.0 is Version Name and +1 is Version code. changes here (pubspec.ymal) will reflect for both Android and iOS version name and version code.

Mohd Danish Khan
  • 1,044
  • 11
  • 12
3

if you are using android studio most easiest way to change it from local.properties

enter image description here

Md Tariqul Islam
  • 2,736
  • 1
  • 20
  • 35
2

On my local builds I use this solution to get ride of the warning while building.

Action Required: You must set a build name and number in the pubspec.yaml file version field before submitting to the App Store.

build.sh

#!/bin/bash
#my vars
BUILD_NAME="Local build 3.2.3"
BUILD_NO=10

#my vars injection 
perl -i -pe 's/^(version:\s+\d+\.\d+\.\d+\+)(\d+)$/$1.('$BUILD_NO')/e' pubspec.yaml

#build ios
flutter build ipa --target=lib/main.dart \
  --build-name=$BUILD_NAME \
  --build-number=$BUILD_NO \
  --export-options-plist=./ExportOptions.plist \
  --release

#build android
flutter build apk --target=lib/main.dart \
  --build-name=$BUILD_NAME \
  --build-number=$BUILD_NO \
  --release

pubspec.yaml

name: radioavisenapp
description: Tidligere Radioavisen App
publish_to: 'none'
version: 3.2.5+10
MKLarsen
  • 51
  • 5