App DevelopmentStrategy and Innovation

How to Use Fastlane for Android (with examples)

Whether you are part of a large team or an independent developer, building, testing, and deploying your application is a core part of your routine. Often, we wind up bringing in various Gradle plugins to integrate with tools, or worse—take matters into our own hands by manually notifying our teammates of new builds or uploading APKs directly to Google Play. This can start out as a minor inconvenience but becomes a tedious and error prone process. In comes Fastlane, a tool to automate all of these repetitive tasks, from building and testing to notifying our teammates and deploying to Google Play.

Getting Started

We can install Fastlane with Homebrew or RubyGems.

gem install fastlane -NV or brew cask install fastlane

Then run fastlane init in the project directory. We will need to provide the package name, press enter when asked for the path to our JSON secret key, and answer n when asked about uploading to Google Play; we will set up our secret key and Google Play uploads later. We now have a Fastlane directory containing a Fastfile and Appfile. The Fastfile contains lanes, or tasks to execute, and the Appfile contains some app metadata.

Running Tests with Fastlane

First things first: let’s run our tests. Fastlane can execute Gradle tasks, so we can set up a lane in our Fastfile to run our test suite with Gradle. We can specify the task, flavor, and build type. With our lane set up below, running tests and analysis is as simple as fastlane test.

lane :test do
    # Run unit tests
      task: "test",
      flavor: "qaCore",
      build_type: "Debug"

    # Run FindBugs
      task: "findbugs",
      flavor: "qaCore",
      build_type: "Debug"

    # Run PMD
      task: "pmd",
      flavor: "qaCore",
      build_type: "Debug"

Deploying to Test Environments

Now that our unit tests are running, the next step is getting the app into our Test Engineer’s hands. We use Fabric’s Crashlytics Beta to deploy our test builds; Fastlane offers excellent support for this. There is a built-in action and no required external dependencies. Another great integration that Fastlane offers is Slack. When we submit a new build to Crashlytics, we can send out a Slack notification, closing the loop with the Test Engineers. This is great when you work on a cross-disciplinary team, as we do at WillowTree.

lane :deploy_crashlytics do |options|
     apk_path: "/tmp/workspace/apk/qaCore/release/app-qa-core-release.apk",
     api_token: ENV["FABRIC_API_KEY"],
     build_secret: ENV["FABRIC_BUILD_SECRET"],
     notes: ENV["NOTES"],
     groups: options[:deploy_group]
   message_slack(options[:build_number], "Crashlytics Beta")

 def message_slack(build_number, target)
     message: ":android: Successfully deployed new *Android* builds *#{build_number}* to *#{target}* :android:",
     fail_on_error: false,
     slack_url: ENV["SLACK_NG1_DEV_ACTIVITY_URL"],
     default_payloads: [:git_branch, :git_author, :last_git_commit_hash]

Publishing the App

When it’s time to deploy to Google Play, we use Fastlane’s Supply, a tool for uploading to Google Play. This requires a JSON key from the Google Play Developer Console. Detailed instructions for retrieving the key can be found here. Once we have it, it’s time to add its path to our Appfile and run fastlane supply init in the project root. Once the setup is complete, we’re ready to deploy at the click of a button.

  lane :release_alpha do |options|
      track: "alpha",
      apk: options[:apk],
      json_key: "/tmp/key.json",
      package_name: options[:package_name],
      skip_upload_metadata: true,
      skip_upload_images: true,
      skip_upload_screenshots: true
    message_slack(options[:build_number], "US Play Store")

Fastlane has over 200 integrations available, so this is just a small slice of what it has to offer on Android. It truly simplifies the often cumbersome and error-prone process of manually deploying builds. Next time you’re setting up a deployment pipeline, you should strongly consider Fastlane.

Join our team to work with Fortune 500 companies in solving real-world product strategy, design, and technical problems.

Find Your Role

Xcode Cloud: Choosing the Right Continuous Integration and Delivery Tool for Your Team

Apple recently announced the release of Xcode Cloud at the latest iteration of their...

Read the article