How to Automate CI/CD for Android App with Boilerplate
Tags: android, webdev, mobile, opensource
Published: 2022-11-18 08:59:51

Stop publishing your Android apps manually and start doing this fully automated at any stage.

Mobile development, as well as any other software development, requires writing code that has to go through all possible tests. To exclude the possibility of errors, which are likely to be caused by human factors, we have made a mobile CI/CD pipeline to automate the processes of verification and delivery of the application to the test and production environment.

This article overviews [the boilerplate for building a CI/CD pipeline for Android via Fastlane and GitHub Actions](https://github.com/maddevsio/android-ci-cd).

## Why do we need a CI/CD pipeline?
Above all, CI/CD allows you to increase productivity with automation.

With the help of a CI/CD, you can automate the various tasks within building an app, create artifacts (.apk, .aab), and deploy them into production. This eliminates manual chores and makes the developer’s life easier.

## What is Fastlane?
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.

It allows you to customize your app's build types and flavors.

Besides building apps, Fastlane handles various tedious tasks such as code signing, generating screenshots, releasing apps, and much more.

Its built-in plugin system makes it easy to extend its capabilities. Various tools/plugins allow you to create apps that can be published to the Google Play Store, Firebase App Distribution, or Apple TestFlight.

Now it is time to look closer at our boilerplate. Why do you need it?

## How does our boilerplate work?
Components scheme:

![Components scheme](https://images.prismic.io/superpupertest/80086cca-1731-442b-bbf7-010f4d72aa77_Android_2.png?auto=compress,format)

And there are the following advantages:

* **Quick start CI/CD.** With this boilerplate, you can easily build the CI/CD for your android app based on Fastlane.
* **Easy adaptation to external CI/CD tools.** We use GitLab-ci or GitHub actions as the executor of Fastlane commands and the construction of the workflow.
* **Notification.** It has notifications to Slack about successful operations or errors in the pipeline process.
* **No special build machine setup is required.** We build the application inside a docker container with all the dependencies installed; this provides portability and the ability to use standard GitHub agents or GitLab runners.

To understand the next steps in a little more detail, you can first check out our video overview:

[![Boilerplate to Prepare a CI/CD for Android Application Automatically
](https://images.prismic.io/superpupertest/3968d536-0440-404c-9bf5-05628065e7aa_android_ci_cd.png?auto=compress,format)](https://www.youtube.com/watch?v=poSugKUtBPU)

Let’s get started with the Android CI/CD tutorial!

## Prerequisites
First of all, you need to:

1. Create an account in the [Google cloud platform](https://cloud.google.com/);

2. Create a [Google cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects);

3.  Create a [Firebase project and activate app distribution](https://cloud.google.com/firestore/docs/client/get-firebase);

4. Create a [Google Play developer account](https://play.google.com/console/about/).

Only after that can we move to keys for building and releasing Android applications.  

###Prepare keys for build and releasing the application
* Json-file with configuration for Firebase
Prepare the Json-file with the Firebase project settings encoded to base64
https://firebase.google.com/docs/android/setup - Step 3
`base64 google-services.json > firebase_setting`
* Service account with access to Firebase
Create Service Account for the release application to Firebase
Choose your Firebase account --> Project Overview --> Project setting --> Service Account --> create service account
Prepare the Service Account key encoded to base64.
`base64 sa.json > key_firebase`
* Service account with access to Google Play
Create Service Account for release application to Google Play
Prepare Service Account key encoded to base64.
`base64 google_play.json > google_play`
* Keystore for signing an application
To sign an application, you need a key, which can be generated with the command.
`keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000`
* Prepare signing keystore encoded to base64.
`base64 my-release-key.keystore > keystore`

Pay attention please note the parameters that you specify when creating the key. These parameters will need to be added to the environment variables.

## Let's prepare the CI/CD to run
If you need to prepare the CI/CD in GitLab, please see the [README](https://github.com/maddevsio/android-ci-cd).

### GitHub Actions
* We can use environments in GitHub Actions, but the environments are available only in public repositories or corporate subscriptions.
* In this boilerplate, we don't use environments in GitHub Actions.

###Prepare environment variables
GitHub --> Settings --> Secrets --> Actions --> New repository secret

* `GOOGLE_SERVICES_JSON` - In the value field, paste your google-services.json encoded to base64.
* `SA_JSON_KEY` - In the value field, paste your sa.key encoded to base64.
* `SA_JSON_GP_KEY` - In the value field, paste your google_play.json encoded to base64.
* `KEYSTORE` - In the value field, paste your my-release-key.keystore encoded to base64.
* `ALIAS` - Alias name
* `ALIAS_PW` - Alias password
* `KEYSTORE_PW` - Keystore password
* `APPROVERS` - List of approvers for Google Play release.
* `APP_PACKAGE_NAME` - The default android package name, for example, we use com.boiler.android.hello
* `APP_PACKAGE_NAME_STAGING` - The package name for the staging environment, for example, we use com.boiler.android.hello.staging
* `APP_VERSION_NAME` - Application version
* `FIREBASE_APP_ID_PROD` - Application ID for production Firebase
* `FIREBASE_APP_ID_STG` - Application ID in for staging Firebase
* `SLACK_WEBHOOK_URL` - Slack webhook URL

You can find more information about environment variables on Github.

When you complete all this preparation, you can start to build and release the application to Firebase. That’s all!

We hope you will find this boilerplate helpful, and you will find it among mobile CI/CD pipeline best practices for Android.

We look forward to your feedback on our boilerplate.