Fastfile lane to build and install all flavors for given build type to a device

Add this lane to your Fastfile and run fastlane installAll type:{BUILD_TYPE} in command line. Replace BUILD_TYPE with the build type you want to build.

For example: fastlane installAll type:Debug

This command will build all flavors of given type and install it to your device. Currently, it doesn't work if you have more than one device attached. Make sure you have only one. In the future I'm planning to add option to select target device.

Fastfile to build and upload multiple flavors to Beta by Crashlytics

This is a sample Fastfile setup for a multi-flavor app. It gives you an option to build and deploy all flavors or a single flavor. After the deployment, it reports to Slack the status of the deployment, and sends a notification to testers in Beta by Crashlytics testers group.

To build and deploy all flavors use:

fastlane android beta

To build a single APK and deploy use:

fastlane android beta app:flavorName

Using a single Fastlane file, you can manage iOS, Android, and Mac apps. If you are using this file just for one app platform is not required.

How It Works

android argument tells fastlane that we will use :android platform.

Inside :android platform you can have multiple lanes. Currently, I have only :beta lane. The second argument from the command above specifies the lane we want to use.

options[:app]

There are two Gradle tasks. First, it runs gradle clean. If you provided a flavor with app key, fastfile runs gradle assembleReleaseFlavor. Otherwise, it runs gradle assembleRelease to build all build flavors.

If we are building for all flavors, an array of generated APK file names is stored inside SharedValues::GRADLE_ALL_APK_OUTPUT_PATHS. We use this to loop through generated files and deploy them to Beta by Crashlytics. notifications and groups fields are optional. They are used to notify testers registered for the app on Beta by Crashlytics.

If you are familiar with Crashlytics, you might know that to activate an app in the portal, you have to run it on a device and use it first. Otherwise, Crashlytics will assume the app inactive and throw an error. In this scenario, I capture it and report to Slack as a failure, so you will know which app is inactive.

If deployment is successful, fastlane will send a success message to Slack.

#{/([^\/]*)$/.match(apk)} this regex is used to get flavor name from APK path. You can remove it if it does not work for you.

get_version_name and get_version_code are two Fastlane plugins to retrieve app version name and code. You have to install these gems if you want to use, or you can remove them. Read more about Plugins here.

The else statement will be executed if you are building and deploying a single APK. We don't have to provide apk_path to Crashlytics since we have only one app.

error do block at the end is used to get notified if anything else goes wrong during execution.

Note

Don't forget to replace SLACK_URL, API_TOKEN, GROUP_NAME and BUILD_SECRET with your own credentials.