Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I auto-increment build number on Flutter App?

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
like image 611
batman567 Avatar asked Dec 18 '25 06:12

batman567


1 Answers

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"
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS sed (requires a space after -i)
    sed -i '' -e "s/version: $current_version/version: $new_version/g" $path_to_pubspec
else
    # GNU sed (requires no space after -i)
    sed -i'' -e "s/version: $current_version/version: $new_version/g" $path_to_pubspec
fi
like image 127
Matt H Avatar answered Dec 20 '25 21:12

Matt H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!