Complete guide to CI/CT/CD for Android and iOS(Fastlane) with CircleCi — Part-1 Android
Mobile application development is one of the core areas of the company’s portfolio. Most of the companies prefer to release apps manually to app store rather than automate the build and release process this takes additional resources and time to release the app.
In this article, I am going to give complete steps to enable the CI/CD process for mobile platforms Android and iOS with CircleCI.
CircleCI
Before proceeding further first let us try to understand what is CircleCI. It's a cloud-based CI/CD tool that handles all the environment setup needs for you on the cloud. So we don’t need to set up a server and install the required software/packages to run the CI/CD scripts. In simple words, it's just a cloud server that listens to your git and runs scripts i.e lint, test, build, etc.. every time when you push code to git. All you need to get familiar with the CircleCI configuration.
To build and test mobile applications we need a machine, instead of a physical machine CircleCI will create a new VM or Docker image to run you code on it.
let's see how this whole process works when you push changes to git.
It's really cool to know that every time it creates a fresh image of executors to run build our apps :).
Android
CircleCI provides us a docker image for android in which our code runs and builds the app. I suggest to read about docker if you are not familiar with it before reading further. ref http://docker.com/
Let's create .circleci/config.yml file in the root of the Android project and start configuring it.
orbs — are similar to GitHub Actions or helm charts, in simple words, it's just a set of instructions that are used most commonly. so for common operations like checkout code, we can us -checkout instead of git clone commands.
To do android common operation we will use android orbs circleci/android@0.2.1
To do slack common operations we will use slack orbs circleci/slack@3.4.2
jobs — are set of instructions to run our commands in the specified executor. we are going to create a job name called build-distribute, also working_directory where our code will be cloned and execute commands on it.
We have to specify executor for Android, CircleCI provides executor: android/android which is basically a docker image contain required java, Android SDK.
steps — contain individual commands or orbs commands
We are using android orbs commands accept-license and restore-build-cache to accept Android SDK license as its crated brand new SDK every time and restore gradle dependency files to make build faster.
caching — CircleCI provides a way to cache the dependencies/packages out of the box, for android android-orbs we installed and will take care of android related cache files.
commands — are shell commands that will run on working_directory. For android, we are using ./gradlew lint ./gradlew test and ./gradlew clean assembleDebug to run linting, testing, and building apk file.
Once the build process is completed we can distribute the apk file to QA/Testing teams with firebase CLI, as firebase CLI is not pre-installed on CircleCI android executor we have to install it manually with commands. Installing and using any CLI tool is pretty simple as we have commands to configure on yml file. Please do refer to firebase CLI documentation to know how to create firebase token and APP ID.
slack notification — will notify the status of build to specified channels on webhooks. For further read on slack webhooks head-over to incoming-webhooks. We are using slack orbs which help us to setup slack with minimal setup.
workflows — contain a list of jobs and on which branch these jobs has to run. We want to run build-distrubute job every time we push code to the master branch, so we have used the filter option to specify the branch names. For more info on how to set up branch name for specific jobs head-over to this tutorial.
As you noticed there were a few environment variables we have used in the config file, CircleCI gives us a way to protect tokens and secrets. DO NOT COMMIT token or any sensitive information into a git repo, instead create an EVN variable under project settings and use it in the config file.
As CircleCI creates a fresh image every time when it builds, If you have questions like How to see units-test reports? how to download apk file directly from circleCI ? then artifacts will rescue to persist the data you needed. we can set up unit tests output directory to artifacts by setting up the below command in steps.
#To persist build files
- store_artifacts:
path: /output/#To show test results in Test tab
- store_test_results:
path: output/scan
Similar way we can give apk file location path to store_artifacts to persist apk file for future downloads.
- store_artifacts:
path: /home/circleci/code/app/build/outputs/apk/debug/
Stay tuned for part 2 on Fastlane and iOS !!
Congratulations!! 😎 You made it to the end. And if you liked this 😍article, hit that clap button below 👏. It means a lot to me and it helps other people see the story.
I really appreciate any suggestions and questions.
Fellow me on twitter for updates !!!