fastlane — the magic tool behind Loco’s continuous release process

rajan ks
Loco
Published in
6 min readFeb 6, 2019

When you are seamlessly working to make your mobile application better, it can get very hard to manage releases with a lot of manual steps involved. This is when fastlane comes to your rescue to help save a ton of time by automating tedious development and release process.

What is fastlane ?

Fastlane is a magic tool which helps expedite Android and iOS release process with minimal effort. To be precise, using a single line command, you can get fastlane to do a lot of things ranging from generating and signing release build to uploading it to a store. Trust me when I say “magic tool”: it made our release process so smooth and saved us hours of time which can be spent on more productive activities.

fastlane is an open source platform aimed at simplifying Android and iOS deployment. fastlane lets you automate every aspect of your development and release workflow. Apart from managing releases, fastlane also helps in various other aspects such as distributing builds internally via Slack,Telegram and other communication channels. It can be used for verifying checklists before release and also capturing screenshots and uploading to store alongside release, code signing etc.

Why did we start using fastlane in first place?

Before jumping into the wonders of fastlane, here are some of its benefits that we considered:

  • As Loco is one of India’s fastest growing apps, we wanted to make sure everything was in place prior to release. Initially we’d do manual releases until a certain point where we had to distribute multiple APK’s based on phone architecture to maintain app size as minimal as possible. Then we added fastlane to our arsenal and from that point on, we started using it to do the tedious work for us using simple ruby scripts.
  • fastlane can also be used to automate distributing the app within team members through various communication channels like Slack, Telegram and Drive etc.
  • When your app is ready to be published, a major bottleneck is to generate and sign apk, upload it to the store with new screenshots, changelogs, etc. Wouldn’t it be amazing to have a single command that can do all this work instantly? And that’s exactly what fastlane does - the release process is now just that easy.
  • Made tons of changes to your code? Need to manually write up each and everything you implemented while sharing the build with your team? With fastlane, you can fetch git commit messages and form a proper readable release note which will be posted onto channels alongside the build.
  • If you frequently forget to verify your checklist before uploading, you can simply provide the checklist to fastlane and it won’t allow you to release the build unless you actively tick off the checklist.

How to setup fastlane?

fastlane is ruby-based and can be installed as gem in OS with ruby built-in.
You can install and setup fastlane for your Android project using HomeBrew (get HomeBrew here). Once HomeBrew is installed, you can easily install any ruby gem using the install command.

To install fastlane type below command in your terminal.

brew cask install fastlane

Once you execute above command, it will install fastlane into your system. For accessing fastlane from any directory, add it to shell profile. Open bash_profile using any editor like vi/nano like so:

vi ~/.bash_profile

Then paste the code below in your editor and save, terminate and open new session of terminal to start using fastlane:

export PATH="$HOME/.fastlane/bin:$PATH"
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

Now fastlane is ready to use, and the final step is to link fastlane with your Android project. Navigate to your working directory/ simply open terminal within Android project and type the command below to setup fastlane for your code:

fastlane init

By executing the above command, it will create a folder named Fastlane in your working directory, which will have two main files included:

  1. Fastfile is where all your automation related scripts go in.
  2. AppFile holds all the configuration properties.

If you want to connect your Play Store account with fastlane, then you have to get the Google Play Store service account api key file.

  • Open Google Play Console, navigate to settings > API access.
  • Click on CREATE SERVICE ACCOUNT button
  • Follow the Google Developers Console link in the dialog
  • Click the CREATE SERVICE ACCOUNT button at the top of the Google Developers Console
  • Provide a <service account name> and click on select a role to choose Service Accounts > Service Account User
  • Make sure Json is selected as the Key type, download the Json file to your computer and save it inside fastlane folder under api subfolder.
  • Now open AppFile inside the fastlane folder in your project directory and mention path to your Json file as:
json_key_file("./fastlane/key/<your_json_file_name>.json")
  • Navigate back to google play console and click on Grant Access for the newly added service account by choosing Release Manager from the Role dropdown finally click on Add User to save.

Smoot sailing so far? Awesome, you have now successfully linked your Google Play Store account with fastlane, and you are ready to fetch your app metadata and upload apk to store using scripts.

fastlane has two powerful tools called Supply and Screengrab. With Supply, you can get all the metadata of your app from the Play Store and every time you release the build using fastlane, it will post the metadata with new changelogs and images based on the version. Screengrab is used to generate localized screenshots of your app which can be uploaded using Supply. Run the following command to grab all the metadata:

fastlane supply init

Once you have executed the above command, you’ll see a folder structure similar to the one below in your project root directory:

fastlane inside working directory of Android App

How to publish the app to Google Play Store using one magic command:

So far so good! Next, let’s get started with some fancy ruby scripts in fastfile to do the magic work.

Add below lane to your fastfile to perform phased rollout of your app to Play Store based on percentage:

desc "Generate and publish APK to rollout"
lane :rollout do |options|

gradle(task: "clean assembleRelease")
version_code = getPropertyFromGradle("versionCodeBase") versionCode =
if version_code == nil
prompt(
text:"Enter Version Code :"
)
else
version_code
end

changelog = prompt(
text: "Changelog: ",
multi_line_end_keyword: "END"
)
languageCodes = ["en-US","hi-IN"]

languageCodes.each{ |code|
changeLogFile = File.new("./metadata/android/"+code+"/changelogs/"+versionCode +".txt","w")
changeLogFile.puts(changelog)
changeLogFile.close
}
percent = options[:percent] ? options[:percent].to_f/100 : 0.05
upload_to_play_store(
track: 'rollout',
rollout: percent.to_s
)
end

You can use the below ruby function to get version property from gradle file:

def self.getPropertyFromGradle(property_name)
version_code = "0"
begin
file = File.new("../app/build.gradle","r")
while (line = file.gets)
if line.include? property_name
versionComponents = line.strip.split(' ')
version_code = versionComponents[versionComponents.length - 1].tr("\"","")
break
end
end
file.close
rescue => err
puts "An exception occured while reading gradle file: #{err}"
end
return version_code
end

Then you need to the add version code of your app in your app gradle file in below format to auto pull version code for creating changelog file

ext {
versionCodeBase = 29
versionName = "29.10.1993"
releaseName = "release_name"
}
android {
...
defaultConfig {
applicationId "appId"
minSdkVersion 16
targetSdkVersion 28
versionCode project.ext.versionCodeBase
versionName project.ext.versionName
}
...
}

Once it is added, just run the below command to upload your app to the Store:

fastlane rollout percent:5

It will prompt for changelog, paste your changelog followed by END to auto update your changelogs along with your release.

For full rollout, you can either use above command with percent value 100 or create your own lane which does it for you like:

desc "Generate and publish APK to Play Store"
lane :deploy do |options|
rollout percent:100
end

And execute using command as simple as:

fastlane deploy

That’s how we automated and enhanced our release process at Loco.

Next time, I’ll talk more about how to share build via communication channels like Slack, Telegram and also how to use git commit messages as changelogs in up coming articles.

Till then, adiós !

To win cash daily, play Loco! Your favourite casual games, live quizzes, sports leagues — Loco has it all. Play games, win money. All day, every day. Download Loco now.

--

--

rajan ks
Loco
Writer for

Software Engineer at Facebook. Android Enthusiast, Online gamer.