DEV Community

Cover image for The Ultimate Guide to Automating Android APK Builds and Firebase Distribution with Fastlane 🚀
Amit Kumar
Amit Kumar

Posted on • Edited on

The Ultimate Guide to Automating Android APK Builds and Firebase Distribution with Fastlane 🚀

React Native is an excellent framework for building cross-platform mobile apps, but automating the process of building and distributing APKs can save a lot of time, especially as your app evolves. In this blog post, we'll walk through how to use Fastlane to automate the versioning, building, and distribution of your Android APK to Firebase App Distribution in a React Native project.


Image description

Prerequisites

Before we dive into the configuration, make sure you have the following ready:

1. Fastlane Installed
You need Fastlane installed on your system. If you haven’t already, install it by running:

sudo gem install fastlane -NV

Enter fullscreen mode Exit fullscreen mode

2. Firebase Project & App Distribution
Ensure you have your Firebase project set up and Firebase App Distribution integrated into your React Native project. You’ll also need your Firebase App ID, which you can find in the Firebase Console.

3. React Native Project Setup

Ensure that you're working in the android directory of your React Native project. You should be familiar with the Android-specific setup for React Native projects.


Image description

Step 1: Initialize Fastlane in Your React Native Project

Fastlane requires a configuration file to operate. Open your terminal and navigate to the android directory of your React Native project:

cd android

Enter fullscreen mode Exit fullscreen mode

Next, initialize Fastlane:

fastlane init

Enter fullscreen mode Exit fullscreen mode

During initialization, Fastlane will ask you to provide a package name (e.g., com.example.app). Once done, Fastlane will create a fastlane directory in your project.


Image description

Step 2: Install Fastlane Plugins

Fastlane is highly extensible through plugins. To interact with Firebase App Distribution and handle versioning, you'll need two Fastlane plugins:

  • Firebase App Distribution: This plugin will help you upload APKs to Firebase.
  • Increment Version Code: This plugin automatically increments the versionCode (used by Android) for each build.

Run these commands to install the plugins:

fastlane add_plugin firebase_app_distribution
fastlane add_plugin increment_version_code

Enter fullscreen mode Exit fullscreen mode

Image description

Step 3: Configure Your Fastfile

The core configuration for Fastlane is done in the Fastfile, which is located inside the fastlane directory. Open your Fastfile and add the following lanes:

default_platform(:android)

platform :android do

  # Lane to increment versionCode
  lane :increment_version do
    # Fetch the latest release from Firebase App Distribution
    latest_release = firebase_app_distribution_get_latest_release(
      app: ""1:0000000000:xxxx:xxxxxxx"" # Replace with your Firebase App ID
    )

    # Increment the versionCode by 1 from the latest release
    increment_version_code({ version_code: latest_release[:buildVersion].to_i + 1 })
  end

  desc "Build and upload the APK (Release) to Firebase App Distribution"
  lane :upload_to_firebase do

    # Increment versionCode before building
    increment_version

    # Clean Gradle build
    gradle(task: "clean")

    # Build the APK
    gradle(
      task: "assemble",
      build_type: "Release"
    )

    # Upload the APK to Firebase App Distribution
    firebase_app_distribution(
      app: "1:0000000000:xxxx:xxxxxxx", # Replace with your Firebase App ID
      testers: "amitkumar@domain.com",
      release_notes: File.read("../../release_notes.txt") # Ensure correct path to your changelog file
    )
  end

  desc "Build and upload the APK (Debug) to Firebase App Distribution"
  lane :upload_to_firebase_debug do

    # Increment versionCode before building
    increment_version

    # Clean Gradle build
    gradle(task: "clean")

    # Build the Debug APK
    gradle(
      task: "assemble",
      build_type: "Debug"
    )

    # Optional: Upload the APK to Firebase App Distribution
    firebase_app_distribution(
      app: "1:0000000000:xxxx:xxxxxxx", # Replace with your Firebase App ID
      testers: "amitkumar@domain.com",
      groups: "your-group-team-id",
      release_notes: File.read("../../release_notes.txt") # Ensure correct path to the changelog file
    )
  end

end

Enter fullscreen mode Exit fullscreen mode

Key Features of This Script:

1. Automatic Version Code Increment:
The increment_version lane fetches the latest release from Firebase and automatically increments the versionCode by 1. This ensures you don’t have to manually keep track of version codes for each build.

2. APK Building:
The script builds both Release and Debug APKs using Gradle. It starts by cleaning the Gradle build to ensure there are no outdated files or caches.

3. Changelog Upload:
The release notes are read from a release_notes.txt file (make sure to provide the correct path). This file can be created anywhere in your project directory, but ensure that you provide the correct relative path in the release_notes.txt reference. The changelog is then uploaded to Firebase App Distribution, providing your testers with context on the new build.

4. Firebase App Distribution:
After the APK is built, it is automatically uploaded to Firebase App Distribution. You do not need to specify the APK path manually—Fastlane will pick the correct APK file.

  • Single Tester: If you want to distribute the APK to a single tester, simply provide their email in the testers field:
testers: "amitkumar@domain.com"
Enter fullscreen mode Exit fullscreen mode
  • Multiple Testers:

If you want to distribute the APK to multiple testers, add their email addresses, separated by commas:

testers: "amitkumar@domain.com", "second@domain.com", "third@domain.com"

Enter fullscreen mode Exit fullscreen mode
  • Using Firebase Groups:

Alternatively, if you have a group of testers in Firebase, you can use the group’s name (the group ID from Firebase Console). This is helpful for larger teams or structured tester groups:

groups: "your-team-name"  # Replace with the Firebase group ID

Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Step 4: Configure the Appfile

The Appfile contains configuration for Fastlane, such as your package name and Firebase credentials. Open the Appfile inside the fastlane directory and configure it with your Firebase credentials and package name.

json_key_file("")
package_name("com.xxxxxx") # Replace with your app's package name

Enter fullscreen mode Exit fullscreen mode

Step 5: Running the Script

Image description

With everything set up, you can now use Fastlane to build and distribute your APKs to Firebase. Run the following commands in your terminal in android directory:

  • For Release APK:
fastlane android upload_to_firebase
Enter fullscreen mode Exit fullscreen mode
  • For Debug APK:
fastlane android upload_to_firebase_debug
Enter fullscreen mode Exit fullscreen mode

This will trigger Fastlane to increment the version code, clean the build, create the APK, and upload it to Firebase with the appropriate changelog.


Troubleshooting Firebase App Distribution Upload Issues

Image description

When you attempt to upload your first build to Firebase App Distribution, you may encounter an error related to authentication or permission issues. This typically happens when Fastlane is not logged into Firebase. To resolve this, you can log in to Firebase using the following command:

1. Login to Firebase:
To authenticate Fastlane with Firebase, you need to use the Firebase CLI. Run the following command in your terminal:

firebase login --reauth

Enter fullscreen mode Exit fullscreen mode

This command will prompt you to log in to Firebase through your browser. After logging in, Firebase will authenticate your session and give you access to the Firebase services, including Firebase App Distribution.

2. Once Logged In:

After you successfully log in, you should be able to upload your APK using Fastlane without encountering any authentication errors.

Note:
If you have already logged in and still face issues, make sure the Firebase project associated with the app is correctly set in your Appfile. You should have the right project and app ID specified. Additionally, ensure that the service account you are using has the appropriate permissions to upload builds to Firebase App Distribution.


Conclusion

Fastlane is a powerful tool for automating repetitive tasks like building APKs and distributing them to Firebase App Distribution. By using Fastlane in a React Native project, you can ensure that the process is fast, consistent, and error-free. This setup handles everything from versioning to distribution, allowing you to focus on what matters most—developing your app.

Automating these steps reduces manual errors, saves you time, and provides a seamless experience for your testers. With just a few simple commands, your APK is built, versioned, and delivered to Firebase.

Image description

Top comments (0)