Pondikpa Tchabao
Pondikpa Tchabao

Reputation: 3665

Flutter: upgrade the version code for play store

I have published an application on the play store with flutter, now I want to upload a new version of the application. I am trying to change the version code with:

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

or changing the local.properties like this

 flutter.versionName=2.0.0
 flutter.versionCode=2
 flutter.buildMode=release

but every time I get an error on the 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.

Upvotes: 292

Views: 234016

Answers (27)

Vivek
Vivek

Reputation: 494

This question is answered here multiple times. But sometimes the fine details given in documentation are missed in hurry, so attempting to summarize it once again.

Google play store require every app to have new version number every time an update is uploaded. This is an INTEGER say 1 for your initial app (app bundle or APK) upload. Every time when you upload a new version, Google expect this version number to be bumped up. So, your next version should be greater than 1 meaning 2 or more. Do not get this version number confused with your flutter app version such as 1.0.3 or 1.1.1 or something like that. Every time when you upload a new version just increase play store specific version number (or rather upload count in simple). So, if you have just released an app, its initial version is 1, now when you release a new version with either a big feature update or a minor update, your next version should be 2 or more. So, if you kept it as 2, your next version should be 3 or 4. And if it was 4, then next should be 5 or more. Hope this gives an idea.

Now, the question here is, how you can simply manage this without the overhead of updating this in multiple places such as build.gradle file. As the official document says here, all you need to do is updating the version field in pubspec.yaml file.

version: 1.0.0+2

In the above version number, first three numbers separated by comma are app specific version. Your first version is 1.0.0, then next minor version is 1.0.1 and so on. The number comes after + is what does the trick here. When you build an android app using flutter run command, or by clicking the debug or run icon or run in profile mode/release mode etc. in VS Code or Android Studio, the local.properties file under android/app folder gets modified. For example, following is what created when I ran the android app in debug mode.

flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=2

If you compare this with the version code 1.0.0+2, you can see what happened here. Everything before + sign is updated as versionName and the number after + got updated as versionCode. As long as your android/app/build.gradle file has following lines, all you need to do is to increase the version code after + sign every time you build an APK or app bundle for Play Store. So, in this case, when I would upload my next file in Play Store, I would use 1.0.0+3.

defaultConfig {
    versionCode = flutter.versionCode
    versionName = flutter.versionName
}

Needless to say, run your android app at least once in Android Studio or VS Code so that local.properties file has this latest version before app bundle or APK is created.

Upvotes: 1

Arpit Verma
Arpit Verma

Reputation: 91

Just change the version code in pubspec.yaml and build the appbundle.

From :

version: 1.0.0+3  // If you have 1.0.0+1

To :

version: 1.0.0+4  // Then change to 1.0.0+2

and run :

flutter pub get  // Do not forget this!!

then run :

flutter build appbundle

You can find the version: x.y.z+c in pubspec.yaml as shown below.

Pubspec.yaml screenshot for reference

Upvotes: 1

Automaticly converting versionName (A.B.C) to versionCode (A * 1000 * 1000 + B * 1000 + C):

In build.gradle after:

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

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

Insert:

def tmpStr = flutterVersionName + '.';
def tmpInt = 0;
while (tmpStr != '') {
    tmpInt = tmpInt * 1000 + tmpStr.substring(0, tmpStr.indexOf('.')).toInteger();
    tmpStr = tmpStr.substring(tmpStr.indexOf('.') + 1);
}
flutterVersionCode = tmpInt.toString();
println('flutterVersionName: ' + flutterVersionName);
println('flutterVersionCode: ' + flutterVersionCode);

Result:

flutterVersionName: 1.1.18
flutterVersionCode: 1001018

Upvotes: 1

Ayoub BOUMZEBRA
Ayoub BOUMZEBRA

Reputation: 5023

In my case, i solved the same exact problem by changing two files:

1- in pubspec.yaml:

from:

version: 1.0.0+1

to:

version: 1.0.0+2

2- in android/locale.properties

from:

flutter.versionName=1.0.0
flutter.versionCode=1

to:

flutter.versionName=1.0.0
flutter.versionCode=2

3- Last action:

flutter clean
flutter packages get

Upvotes: 19

Hammad Nasir
Hammad Nasir

Reputation: 2959

Updating it in project/android/app/build.gradle worked for me.

defaultConfig {
    versionCode 2 // this needs to be updated
    versionName "1.0.5"
}

Hope this helps!

Upvotes: 3

klinsman Agam
klinsman Agam

Reputation: 90

For example if you to make android version 3 ,

For Android go to pubspec.yaml and edit here

version: 3.0.0

and go to build.gradle and edit here

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '3'
}

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

Upvotes: 0

Rahul Raj
Rahul Raj

Reputation: 1493

Follow these steps for above flutter 2.10.2 version

Step 1: Change following changes in pubspec.yaml

    //change version 1.0.0+1 to 1.0.0+2
version: 1.0.0+2
    
    environment:
      sdk: ">=2.16.1 <3.0.0"

Step 2: Change following change in android\local.properties

flutter.sdk=C:\\flutter
flutter.buildMode=release

// Change here flutter.versionName=1.0.0 to flutter.versionName 1.0.1
flutter.versionName=1.0.1

 //Change here flutter.versionCode=1 to flutter.versionCode=2
flutter.versionCode=2

flutter.minSdkVersion=21
flutter.targetSdkVersion=31
flutter.compileSdkVersion=31

Upvotes: 7

Nitin_Narale
Nitin_Narale

Reputation: 1046

still someone looking for a Good answer

in pubsec.yaml file

change version: 1.0.0+1 to version: 1.0.0+2

then open your code in android by selecting

File -> Open -> your Flutter Code workspace -> Android icon of project

Now go to build.gradel

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

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

to

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '2'
}

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

Now last one local.property file

sdk.dir=C:/Users/Admin/AppData/Local/Android/Sdk
flutter.sdk=D:\\flutter_windows\\flutter
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1

to

sdk.dir=C:/Users/Admin/AppData/Local/Android/Sdk
flutter.sdk=D:\\flutter_windows\\flutter
flutter.buildMode=debug
flutter.versionName=1.0.2
flutter.versionCode=2

Upvotes: 9

Amina Bekir
Amina Bekir

Reputation: 270

before uploading the app bundle, first write the Release name. I faced the same issue and That's worked for me.

enter image description here

Upvotes: 0

K Pradeep Kumar Reddy
K Pradeep Kumar Reddy

Reputation: 650

I have been doing it like this in the 'app/build.gradle' file

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}else {
    flutterVersionCode = '4'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}else {
    flutterVersionName = '1.3'
}

Upvotes: -2

jwehrle
jwehrle

Reputation: 4774

Something that might be helpful to others that land here, the Play Store only looks at the versionCode in isolation. So, if you've updated your versionNumber from, for example, 1.0.0+1 to 1.1.0+1 Play Store will throw an error that the versionCode has not changed. So, regardless of what your versionNumber is, you must also change your versionCode - as in, changing from 1.0.0+1 to 1.1.0+2

Upvotes: 3

Lee Probert
Lee Probert

Reputation: 10859

All of these answers mirror the official documentation, and it is how I am setting my versionName and versionCode. But when I upload my build I get the same error as reported by the post author.

My previous version code on the play store shows as 4 (0.0.2) ... I am used to how iOS works so this looked odd to me. The number in the brackets should be the build/code number and the main number is the actual version number. Incrementing the build number when necessary without having to bump the version (because there are no significant changes).

So when I attempted to upload 0.0.3+1 with a new build number to increment for this new version, it complained that the 1 had already been used.

So how does this work on the Play store? I'm confused too.

Upvotes: 0

Rohan Taneja
Rohan Taneja

Reputation: 10727

version in pubspec.yaml file

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

Upvotes: 676

Ali_R
Ali_R

Reputation: 121

I don't think anyone has actually answered the question. A lot of suggestions are updating the version in pubspec. But depending on your deployment you might not use those values.

flutter build --build-number=X --build-name=Y

X gets used as your version code Y gets used as your version name

To test just run build and check local.properties

Upvotes: 6

Soon Santos
Soon Santos

Reputation: 2239

In case you already changed the versionCode, it may be because Play Console already accepted your build.

Instead of clicking on upload, click in Choose from library and choose the build that was already sent.

enter image description here

Upvotes: 12

mhmd82
mhmd82

Reputation: 11

this works for me! I recognised that first app as Default Version Name 1.0.0 Version Number 1 so this means 1.0.0+1

I updated my app after I wrote as 1.0.0+2 in pubspec.yaml.

Upvotes: 1

Juancki
Juancki

Reputation: 1882

Any of the solution did not work for me with App Bundle, I changed to APK and no issues with the version.

Not clear why though.

Upvotes: 0

0llie
0llie

Reputation: 8885

You can still do completely your own thing by overwriting in android/app/build.gradle:

  • def flutterVersionCode
  • def flutterVersionName

to your own values.

Upvotes: 2

Akhil Shukla
Akhil Shukla

Reputation: 501

For Android

"X.Y.Z+n" here "x.y.z" represents the VERSION NAME and "n" represents the VERSION NUMBER. The following changes to be made-

  1. In pubspec.yaml change your version number.
  2. Update your local.properties by running flutter pub get command.
  3. Now build your apk or app bundle by running flutter build apk or flutter build appbundle command.

Upvotes: 13

sultanmyrza
sultanmyrza

Reputation: 5474

  • in pubspec.yml version: 1.0.0+1
  • change to version: 1.0.0+2
  • flutter build ios --release-name --release-number will update version in ios
  • flutter pub get && flutter run will update version for android (android/local.properties)

Upvotes: 1

Malik Elbadri
Malik Elbadri

Reputation: 95

I had the same problem, I solve it by restarting Android Studio.

Upvotes: 0

Abhinand V B
Abhinand V B

Reputation: 51

Check

android{
//....
  defaultConfig {
  //....
  version code:2
  }
}

on android>app>Build.gradle from your project's root folder

Upvotes: 5

Emmanuel Ametepee
Emmanuel Ametepee

Reputation: 515

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.

Upvotes: 8

Evandro Pomatti
Evandro Pomatti

Reputation: 15144

Docs says the build args should override pubspec.yml:

Both the version and the build number may be overridden in Flutter’s build by specifying --build-name and --build-number, respectively.

https://flutter.dev/docs/deployment/android#updating-the-apps-version-number

Upvotes: 3

Dave
Dave

Reputation: 2748

Figured this one out. Documentation is not straight forward

in your pubspec.yaml change the version like this

version: 1.0.2+2

where the stuff is VER_NAME+VER_CODE

Upvotes: 262

Adam
Adam

Reputation: 444

First one change flutter version in pubspec.yaml example `version 1.0.3+4

In case of android go to local.properties than change version name and code same like flutter version code and name.

In case of Ios go to generated.xcconfig than chnage FLUTTER_BUILD_NAME=1.0.3 FLUTTER_BUILD_NUMBER=4`

Upvotes: 1

Pushan Gupta
Pushan Gupta

Reputation: 3825

Solution:

Inside pubspec.yaml add this (probably after description, same indentation as of description, name etc...):

version: 2.0.0+2

Then do packages get inside flutter local directory (Do not forget to do this)

Explanation:

Everything before plus is version name and after is version code. So here the version code is 2 and name is 2.0.0. Whenever you give an update to the flutter app make sure to change the version code compulsorily!

Addtional Info:

Whenever android app is built, build.gradle inside android/app/ looks for version code and name. This usually lies in local.properties which is changed every time you change flutter pubspec.yaml

Upvotes: 32

Related Questions