Automating Flutter with Fastlane
Fastlane is a tool to automate deployments and releases of mobile apps. It is an open-source project and supports both Android and iOS platforms. You can use it for your cross-platform apps too, like React Native or Flutter.
The installation of Fastlane is quite easy. Besides RubyGems and HomeBrew, Gemfile is a highly recommended way to set up the Fastlane. I created a Gemfile under the Android folder of my Flutter app containing the following lines:
source "https://rubygems.org"
gem "fastlane"
Then, running the [sudo] bundle update
command will install Fastlane as mentioned in the official cookbook. But if you’re unlucky like me, you’ll also face some issues at this point.
Trying to run the given command does not help, and throws a new error:
There are lots of advice in here. I hope, one of them will work for you. I successfully installed it with brew install fastlane
.
Before initializing the Fastlane for my project, I created a service account from Google Play Console, granted as admin for my app, and downloaded the JSON file. This JSON file contains confidential data like a private key.
Then, in the android folder of my Flutter app, I run the command fastlane init
. It will ask for the package name and JSON key file. And it will create a Fastlane folder that contains Appfile and Fastfile. Appfile contains the definition of package name and path of JSON file. You can cross-check before running the fastlane supply init
command.
The next step is completed with the above command. With this, all metadata collected from Google Play Store, including images, screenshots, changelogs, descriptions, etc.
Now I can start to write scripts into Fastfile. The base script has a simple structure: Defining the platform, adding description, giving lane name, and writing the script what you want to automate:
default_platform(:android)platform :android do
desc "Runs all the tests"
lane :test do
gradle(task: "test")
end
end
In the terminal, running thefastlane test
command will give the following result:
To get a new build of your app and to upload it to Google Play Store, you can customize the steps:
desc "upload Playground APK to alpha track"
lane :upload_APK_Playground do
Dir.chdir "../.." do
sh("flutter", "build", "appbundle", "--release")
end upload_to_play_store(
package_name: 'org.example.test',
track: 'alpha', #beta, production
json_key_data: ENV['googlePlayJsonKey'],
apk_paths: './path/to/app-release.aab',
skip_upload_apk: 'false',
skip_upload_metadata: 'true',
skip_upload_images: 'true',
skip_upload_screenshots: 'true',
validate_only: 'false',
check_superseded_tracks: 'false',
)
end
**You need to know that the track parameter is set to production as default.
There is a Fastlane plugin to get the current version of your Flutter app. You can install it, too. Thanks to all contributors to the Fastlane project. It really saves hours.