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.
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.
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.
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.
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.