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.
Join Stack Overflow to learn, share knowledge, and build your career.
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
2.0.0
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
If your Flutter versioning is not automatically updating anymore, see this answer for how to fix it.
--build-number
, or default to 1, or does it come up with a build number of its own somehow?
– Thomas
May 27 '20 at 9:41
Thanks to user abion47 for finding and condensing the following answer from the article Versioning with Flutter.
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:
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>
...
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
}
}
Updating the app’s version number
The default version number of the app is 1.0.0
. To update it, navigate to the pubspec.yaml
file and update the following line:
version: 1.0.0+1
The version number is three numbers separated by dots, such as 1.0.0
in the example above, followed by an optional build number such as 1 in the example above, separated by a +
.
Both the version and the build number may be overridden in Flutter’s build by specifying --build-name
and --build-number
, respectively.
In Android, build-name
is used as versionName
while build-number
used as versionCode
. For more information, see Version your app in the Android documentation.
After updating the version number in the pubspec file, run flutter pub get
from the top of the project, or use the Pub get button in your IDE. This updates the versionName
and versionCode
in the local.properties
file, which are later updated in the build.gradle
file when you rebuild the Flutter app.
Docs : https://flutter.dev/docs/deployment/android#updating-the-apps-version-number
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.
What worked for me:
versionname
and versioncode
.flutter pub get
.local.properties
for updated flutter.versionName
flutter.versionCode
file in gradle folder.