2025-01-05
When publishing Android apps to the Google Play Store, each release must have a higher version code than the previous one. Fastlane, the popular automation tool for app deployment, can help us manage this automatically.
Here's how to set up automatic version code incrementing using Fastlane:
google_play_track_version_codes
API to fetch the latest version code from Google Play Storeapp/build.gradle
fileAdd this lane to your fastlane/Fastfile
:
1desc "Increment version code from current Google Play Store number"
2lane :increment_version_code do
3 current_version_code = google_play_track_version_codes(
4 track: "internal"
5 )[0]
6 new_version_code = current_version_code + 1
7 file_name = "../app/build.gradle"
8 current_content = File.read(file_name)
9 new_content = current_content.gsub(/(versionCode\s[0-9]+)/, "versionCode #{new_version_code}")
10 File.write(file_name, new_content)
11end
Note that in line 4, I pass internal
as track parameter to get my latest version code. You can change it to any supported track, which is production
, beta
, alpha
, or internal
.
Now, you can increment your version code with fastlane by issuing command below
1echo "increment_version_code: detected versionCode $(cat app/build.gradle | grep -E "versionCode\s[0-9]+")"
2bundle exec --gemfile ../Gemfile fastlane increment_version_code
3echo "increment_version_code: app versionCode changed to $(cat app/build.gradle | grep -E "versionCode\s[0-9]+")"
android, programming, fastlane, automation