Muyiwa Femi-Ige Femi-Ige Muyiwa Oladele is a statistics major from the Federal University of Technology, Minna. He is an enthusiastic programmer versed in programming languages like Python and JavaScript.

Using Fastlane for Flutter: A complete guide

5 min read 1419

Using Fastlane For Flutter: A Complete Guide

Fastlane is an open source tool suite used to automate releases and deployments for Android and iOS apps, favored by many developers because it can significantly cut down on deployment time. Working with Android and iOS apps can be quite tedious, especially when handling screenshots, beta deployments, App Store deployments, and code signing, among others.

This article will serve as a guide on how to use Fastlane with your Flutter applications. Let’s get started!

Prerequisites

To proceed, I recommend you have:

  • An active Flutter application
  • Familiarity with Flutter and Flutter SDK
  • Familiarity with Firebase and have Firebase CLI installed on their device
  • Installed Homebrew. If you haven’t, here is a link on how to do so on Mac

Setting up

Installing Fastlane

To make use of Fastlane in your Flutter application, you will need to install it correctly (note that we are using macOS to run our application).

You can check out the documentation on installing Fastlane on any device of your choice. Use the command below to install Fastlane:

brew install fastlane

To confirm Fastlane has been installed successfully on your device, check its version by running the following command:

fastlane -v

If the version of Fastlane and the path to where we installed it is returned, we have installed Fastlane successfully.

Setting up your package name

The package name of your application must be distinct.

If you created your Flutter application using Android Studio, you will already have a default package name assigned to your application. The package name is unique to your local device but may not be on Google Play or the App Store.

If you want to change your application package name, you can do so in Android Studio. At the project pane in Android Studio, click on the settings icon at the top:



Android Studio Project Pane

This will bring up a dropdown; ensure that the Compact Directories option is unchecked:

Android Studio Dropdown Compact Categories

Next, you can refactor your package name. To do this, right-click on your application’s package name and select Refactor, then Rename. A window will pop up; click Rename Package in the window, rename, and then save the update.

Setting up Supply and getting our JSON file

Supply is a tool in Fastlane that enables you to upload app metadata, binaries, and screenshots to Google Play.

To initialize Supply, you need to have successfully uploaded an APK to your app in the Google Play Console at least once. Setting it up requires downloading a credentials file from your Google Developers service account.

Now, we need to obtain our JSON secret file. This file will be required when we set up our Fastlane deployment flow. To get this file, follow these steps:

  1. Open your Google Play Console account, click on the Account Details section, and take note of the Developer Account ID listed
  2. Next, click on Setup, then on API access
  3. Click on the Create new service account button
  4. Follow the Google Cloud Platform link in the dialog; this will open a new window, then do the following:
    1. Click on the Create a service account button located at the top of the Google Cloud Platform Console
    2. Ensure that you are on the correct Google Cloud Platform project
    3. Input a service account name and click Create
    4. Next, select a role; find and select Service Account User, and click Continue
    5. Click Done
    6. Next, select the Actions vertical three-dot icon of the service account you just created
    7. Select Manage Keys on the menu.
    8. Click Add Key, then Create New Key
    9. Make sure JSON is selected as the key type, and click Create
    10. Once this is created, you have your JSON file. Note the path of the file
  5. Head back to the Google Play Console module, and click Done to close the dialog
  6. Next, select Grant Access for the newly added service account at the bottom of the screen. Try refreshing the page to reflect the latest updates if it does not show
  7. Select the permissions of your choice
  8. Invite a user to wrap up the process

Initializing Fastlane

In a standard manual application deploration instance, once you have a unique package name, the next step is to package your application for distribution, then to create a new Keystore, and so on. We will use Fastlane to handle the whole process.

To proceed, head on to the root directory of your Flutter application and initialize Fastlane for Android deployment by navigating to your Android folder directory and running the following command:

fastlane init

You will receive a prompt to input your application package name; in our case, our application name is votersapp, and the package name is com.votersapp.votersapp.

N.B., you can find your package name under the bundle.gradle file in the defaultConfig option.

Click Enter after supplying your package name.

Bundle Gradle Package Name

Next, you will receive a prompt to enter the path to your JSON secret file. Input the path to the JSON file we downloaded above. Alternatively, if you are yet to obtain your JSON file, click Enter to skip this step.

Path JSON Secret File

Next, you will receive a notification or pop-up asking if you intend to upload any metadata or screenshots; for this demonstration, we will not be uploading those, so input “n”.

Uploading Metadata

Hit Enter to set up the rest of the initialization from here on.


More great articles from LogRocket:


Once complete, we will see a Fastlane folder in the Android directory. Within this is an Appfile and a fastfile. Follow the process to initialize Fastlane for iOS. The Fastlane file contains all Fastlane configuration tools.

To fetch all the Google Play store metadata, run:

fastlane supply init

Now, to deploy our application to the Play Store, navigate to the project’s directory and execute the following command in the terminal:

flutter clean && flutter build apk

Deploying the application for testing

To demonstrate the capabilities of Fastlane, we will deploy our APK to Firebase App Distribution. We can perform testing and CI/CD using the Fastlane application.

Now, we will set up our Firebase project. To do this, follow these steps:

  1. Head on to Google Firebase Console and create an account if you do not already have one
  2. Click Create a project
  3. Specify your project name and click Continue
  4. On the menu sidebar to the left, select App Distribution and the Android icon
  5. Click Register application
  6. Specify the same package name as specified in your application for the package name. If you are not using any Firebase services, skip downloading the google-services.json file
  7. Click Continue to register App Distribution for your project
  8. Your setup should look like this:
    Firebase App Distribution Dashboard
  9. Finally, head to the tester and group section and create a new group. Then, add a new tester to the group

To assign Flutter app distribution to Fastlane, execute the following command in your terminal:

fastlane app_plugin firebase_app_distribution

In your Fastlane file, replace the existing code with the code below. This code will ensure you map Fastlane distribution to deploy to Firebase.

default_platform(:android)

platform :android do
 desc "Deploy to Firebase app distribution"
 lane :deploy do
  begin
  firebase_app_distribution(
   groups: "testers",
   release_notes: "Fixing bug in features",
   apk_path: "../build/app/outputs/flutter-apk/app-release.apk",
   firebase_cli_path: "/usr/local/bin/firebase",
   firebase_cli_token: <FIREBASE_CLI_TOKEN>
   app: <YOUR_APP_ID>
  )
  end
 end
end

Run the command below in your terminal and grant Firebase access to get your Firebase CLI token in the above code.

firebase login:ci

Copy and replace it as the value for firebase_cli_token in the code above.

N.B., this is secret and should be kept safe.

Get your App ID from the Firebase console.

Next, we will deploy our application by specifying the lane we wish to deploy.

In the code we placed in our Fastlane file, we deploy the value for the “Lane” field. So, to execute a deployment in other lanes, we will tell Fastlane to run that particular lane. To deploy our application using Fastlane, we implement the command here:

fastlane deploy.

The command above will upload the APK specified in that particular lane. Once completed, you can head to your Firebase console and view your application deployed using Fastlane.

Firebase Console Deployed Application

Conclusion

Fastlane is simple and easy to implement — as I noted in the introduction, it saves hours of deployment time and is extremely useful.

In this article we have demonstrated the process of installing and initializing Fastlane and deploying our application for testing. Check on the official documentation to learn more about Fastlane. Happy coding!

: Full visibility into your web and mobile apps

LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page and mobile apps.

.
Muyiwa Femi-Ige Femi-Ige Muyiwa Oladele is a statistics major from the Federal University of Technology, Minna. He is an enthusiastic programmer versed in programming languages like Python and JavaScript.

Leave a Reply