Monday, March 6, 2023

How to Change Version Code and Version Name in Flutter

We released a flutter application on the Play Store, and now I want to submit a new version of the application. We are attempting to alter the version number using the below command in Visual Studio Code:

flutter build apk --build-name=1.3.0 --build-number=3

or alterring the local.properties like this (through Android Studio or Visual Studio Code)

 

 flutter.versionName=1.3.0
 flutter.versionCode=3
 flutter.buildMode=release

 

With these changes when we build the apk bundle and try to release it in Google play store, We get the below error from play store:

You must use a different version code for your APK or your Android App Bundle because code 1 is already assigned to another APK or Android App Bundle

The Solution for this is as below: 

1. Go to build.gradle and the change the flutterVersionCode and flutterVersionName  as below
        
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1.3.0'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '3.0'
}

2. Go to pubspec.yaml file and add the versionCode and versionName as below:

version: 1.4.0+4

environment:
sdk: ">=2.12.0 <3.0.0"

Update version:A.B.C+X in pubspec.yaml.

For Android:

A.B.C represents the versionName such as 1.0.0.

X (the number after the +) represents the versionCode such as 1, 2, 3, etc.

Do not forget to execute flutter build apk or flutter run after this step, because: When you run flutter build apk or flutter run after updating this version in the pubspec file, the versionName and versionCode in local.properties are updated which are later picked up in the build.gradle (app) when you build your flutter project using flutter build apk or flutter run which is ultimately responsible for setting the versionName and versionCode for the apk.

For iOS:

A.B.C represents the CFBundleShortVersionString such as 1.0.0.
X (the number after the +) represents the CFBundleVersion such as 1, 2, 3, etc.
Do not forget to execute flutter build ipa or flutter run after this step

Happy Coding 


No comments: