13

Currently every time I build my APK I manually need to update my build version in 2 places - which seems very unautomated and counter-intuitive (I am comparing this to the Jenkins BUILD_ID).

I understand that I must (and prefer it so) manually update the (semver) version - 0.2.0.

But surely there must be some kind of process/setting/plugin that can automatically update the +3 on every build? I have read a lot of posts and solutions, but none actually do what I would expect it to do (I don't really want pre-build hooks and perl regex scripts unless I have to).

Note: I am not yet pushing to PlayStore - just distributing APK to core team.

pubspec.yaml

version: 0.2.0+3

and

local.properties

flutter.versionName=0.2.0
flutter.versionCode=3

I am using :

  • Android Studio 4.0.2
  • Flutter 1.22
batman567
  • 826
  • 2
  • 12
  • 23

3 Answers3

1

I've been wondering the same thing since it causes issues when uploading to the app stores.

Looks like the solution is to automate this process with something like fastlane, which is what the Flutter docs seem to favor.

Fastlane: https://fastlane.tools/

Flutter docs on Continuous Delivery: https://flutter.dev/docs/deployment/cd

Fastlane increment_build_number action: https://docs.fastlane.tools/actions/increment_build_number/

Ted
  • 240
  • 2
  • 12
  • 2
    More useful discussion here: https://stackoverflow.com/questions/54357468/how-to-set-build-and-version-number-of-flutter-app – Ted Apr 03 '21 at 13:19
  • 2
    increment_build_number only works for iOS – Hashem Aboonajmi Jun 29 '22 at 09:32
  • Supported platforms are iOS and Mac. Here's a link to the source https://docs.fastlane.tools/actions/increment_build_number/#:~:text=increment_build_number-,Supported%20platforms,-ios%2C%20mac – chemturion Dec 14 '22 at 15:30
1

What I do is use a shell script that sets the build number in pubspec.yaml based on the git commit count and an offset. The offset is used when I need to increment a build without changing the commit, such as when publishing the same version to Beta and Prod.

Using VSCode on my Mac, I've also setup a Launch and Task to execute the script prior to each build, but it can also be run manually or converted to a Windows BAT file (with the appropriate Linux tools awk sed grep wc xargs installed.

Note that you need to stop and re-run (rebuild) the iOS and Android projects before they'll recognize the updated builder number.

I run this in the folder above the Flutter project:

#!/bin/bash

path_to_pubspec="flutter_project/pubspec.yaml"
current_version=$(awk '/^version:/ {print $2}' $path_to_pubspec)
current_version_without_build=$(echo "$current_version" | sed 's/\+.*//')
revisionoffset=0
gitcount=`git log | grep "^commit" | wc -l | xargs`
new_version="$current_version_without_build+$gitcount"
echo "Setting pubspec.yaml version $current_version to $new_version"
sed -i "" "s/version: $current_version/version: $new_version/g" $path_to_pubspec
Matt H
  • 6,422
  • 2
  • 28
  • 32
0

What works for me:

  1. Open your app/build.gradle file.

  2. Set versionCode to 1 (if it is flutterVersionCode.toInteger())

    android {
        ...
        defaultConfig {
          ...
          versionCode 1
          versionName flutterVersionName
        }
    }
    
  3. Run this command - fastlane add_plugin increment_version_code

  4. Open fastlane/Fastfile and add this lane

    lane :increment_build_num do
        previous_build_number = google_play_track_version_codes(
          package_name: 'Package name',
          track: "internal",
          json_key: "Json key path",
        )[0]
    
        current_build_number = previous_build_number + 1
    
        increment_version_code(
          gradle_file_path: "./app/build.gradle",
          version_code: current_build_number
        )
     end
    
  5. Run fastlane increment_build_num before making a new build

IvanPavliuk
  • 1,460
  • 1
  • 21
  • 16