If you have overseen the development for even a relatively active Android app, you might already know how often an app needs to be updated. Google has terrific support for building apps integrated right into Android Studio, but it still requires several manual steps to build and submit the app for deployment. Fastlane is a command line tool to automate all the tasks for app deployment. Given the plethora of tasks that it handles, it’s amazing how easy it is to set it up.

I have overseen the development of several apps including several that were picked up from other developers and it still amazes me how many of them go without fastlane.

Install

fastlane has a very good onboarding page to allow you set up fastlane for your app. Make sure you check that page out if you are starting out. But if you are anything like me and you just want to dive into it, this post is for you.

Installation is really simple. Use any of the following two:

$ brew install fastlane

OR

$ sudo gem install fastlane -NV

Configure

First step is to run fastlane init from the project directory that will generate the initial fastlane configuration for your project. The most interesting files are Fastfile and Appfile, both in fastlane directory.

Appfile

Let’s start with the configuration of Appfile. It contains configuration to identify your app. It could be left empty, but more often than not you would like to have the following options:

json_key_file "fastlane/key.json" # Path to the json secret file - Follow https://github.com/fastlane/supply#setup to get one
package_name "in.sapandiwakar.app"

for_lane 'staging' do
  package_name "in.sapandiwakar.app.staging"
end

To set up multiple package names for different lanes, use for_lane with a block for configuration as above.

Fastfile

Fastfile is the one that is more involved. It contains instructions for fastlane to build and deploy your app (there are many more options, but let’s start with simple ones). Let’s add a beta lane to Fastfile that creates an apk and uploads it to play store.

desc 'Deploy a new beta version to the Google Play'
lane :beta do
  gradle(task: 'clean')

  gradle(task: 'assembleProd')

  sign_apk(
    keystore_path: '.signing/release.keystore',
    alias: 'alias',
    storepass: 'password',
    tsa: 'http://timestamp.comodoca.com/rfc316'
  )

  zipalign(apk_path: lane_context[SharedValues::SIGNED_APK_PATH].to_s)

  supply(track: 'beta',
         apk: lane_context[SharedValues::SIGNED_APK_PATH].to_s)
end

Let’s break this lane down. The gradle tasks are pretty self explanatory. You can change them with whatever tasks you use to build your app (assembleProd task assembles the app for flavor named prod).

The next step, sign_apk signs the apk using the supplied configuration. Finally, we need to run zipalign before being able to submit the app to play store. zipalign is an archive alignment tool that provides important optimization to Android application (APK) files. This is required before submitting any app to play store.

The actions sign_apk and zipalign are not integrated with fastlane and need to be added separately. Download the actions from here and add them to fastlane/actions directory. This will make both actions available for use in the Fastfile.

The last step, supple uploads the app to Play Store. You can specify different tracks to upload the app to either alpha or beta deployment on play store.

Deploy

Run fastlane beta and it should build your app and submit to the play store.

You can even post a message to slack after publishing the build. Just add slack(message: "Successfully distributed a new beta build") as the last line of your lane.

If you use Crashlytics instead of TestFlight for distributing the builds, you can use the following instead of supply in the above lane.

crashlytics(api_token: "[insert_key_here]",
            build_secret: "[insert_key_here]")

Check out fastlane docs for all the possible options for configuring fastlane.