Perfectus: Fastlane Integration

Victor Apoyan
6 min readAug 21, 2020

When you are working on the Android application, it's very important to deliver them to the users as fast as possible, so they can test and give you feedback. Google Play Console provides functionality to release an application for close/open beta channels, alpha channels, for internal testers and production. But the process of the release always takes some time and a few clicks, how we can improve this?

In the previous article, I showed how to integrate CircleCI

In this article, I will show how to integrate the Fastlane and completely automate the release process.

Installing Fastlane

The very first step would be the installation of Fastlane, below I will describe the steps of installation on Linux if you have a different operating system, please refer to the official documentation (but in general steps should be similar)

First, we need to create Gemfile file in the root directory of the project with the following content

Second, run the command below in the command line

sudo gem install fastlane -NV

Now when the Fastlane is installed, we need to set up it

Setting up Fastlane

Navigate to the source folder of your project, for which you want to set up Fastlane and run the command from the terminal

fastlane init

Fastlane will automatically detect your project, and ask for any missing information.

Package Name

You can find your application package name in the build.gradle file

Path to the json secret file

Just type `fastlane/api.json` and press Enter

Download existing metadata and setup metadata management

When you are asked this question simply type n and press Enter

After initializing Fastlane for your project, you now need to get a credentials file from your Google Developers Service Account. This is the file that will enable you to actually upload your binaries to the PlayStore.

Getting Google Credentials

Open Google Play Console in the browser and log in to your account. Then press on the “Settings” and “Developer Account” and here press on the “API Access”, you should see the screen below

Now we need to enable API access for the account, for that click on the “Create New Project”, after waiting a bit a screen will be changed to

Here click on the “Create Service Account”, which will pop up a dialog like this

Following instructions in the opened dialog, first, we need to click on the “Google API Console” link, so do it. A new tab will be opened with Google Cloud console

Here click on the “+ Create Service Account”.

In the opened page, fill in the required fields and press “Create”

Now Select a tole, press on the Drop Down and type in the filter filed “Service Account User” select it and then press “Continue”

Almost at the finish line, press “Done”, in the opened screen under the Actions, press on more and then press on the “Create key”

The key type should be selected JSON, press the “Create” button, after that the key file in the json format will be downloaded to your computer.

Go back to the initial dialog with the instructions and press on the “Done”. You will now see a new service account. Press on the “Grant Access” and then choose the role “Release Manager” and click on the “Add User”.

Now when you have a .json file with the key inside downloaded on your local machine, copy it and paste in your project fastlane folder.

PLEASE NOTE: Do not commit this file, as it contains secrets of your Playstore account, keep it ONLY locally.

Modify Fastfile

You need to modify Fastfile as follows

Generate an upload key and keystore

Open Android Studio then presses on the “Build” button in the top menu, from the opened menu select “Generate Signed Bundle/APK…”

Select “APK” and press “Next” button

Fill in all missing information and press the “Next” button, at this step you already should have your Keystore saved somewhere on your local machine.

Now in the project root folder create a file `keystore.properties` with the following content, replace myStorePassword, `mykeyPassword`, `myKeyAlias` with the real data which you provided in the previous step.

storePassword=myStorePassword
keyPassword=mykeyPassword
keyAlias=myKeyAlias
storeFile=myStoreFileLocation

add this file to the git ignore list, this file should not be committed as it contains your secret passwords.

Now open your application build.gradle and before android block add the following lines

And in the android block add the following

Generate build numbers for your app versions

Next, we need to create functions in the build.gradle file to generate build numbers for your app versions.

and don’t forget to set `versionCode` and `versionName` as follows

Configuring CircleCI

Now when keys are generated and Gradle files adjusted, it’s time to configure CircleCI, so it can build and deploy the application on the play store.

At this point, CircleCI config.yml file should look like this

We need to provide all missing data, as you remember, above I mentioned that keystore.propertie , kesyore.jsk and api.json files should not be committed in the repository, but Fastlane needs them, to satisfy Fastlane we need to add a few environment variables. About how to add environment variables read in my previous article, use Base64 online converter, to convert api.json , keystore.jsk files to environment variables.

Now everything should be ready. If you commit all your changes, this should trigger build on the CircleCI side after which application will be automatically published on the Play Store.

Bonus: Fastlane Configuration

In the early development stages, you probably would not want to release the application to a broad audience, just for a few internal testers, or alpha/beta channel. Fastlane is providing that possibility, all you need to do is to add the following code to your Fastlane file, by specifying track: 'internal' your application will be deployed to the internal test channel.

You can read more about different configurations in the article

Another thing which you most probably would like to do is to set up CircleCI in a way that, Fastlane automatic deployment tasks are started only when you push the changes to the certain branch (for example develop), to achieve that behavior just modify the config.yml file as follows

That’s it, I hope this article will help you to build great applications using CircleCI and Fastlane.

--

--