Automate Android development process with GitLab CI and Firebase
mobile Android

Automate Android development process with GitLab CI and Firebase

Bob Nguyễn

Manually testing mobile App and distribute APK files is not an interested process but you may have to do it almost every day. Usually you need to send an APK file to tester team after finished a feature or even after fixed a tiny label bug. Manually testing the app is also a pain point for developer in general. Continuous integration (CI) and continuous delivery (CD) is the solution for this.

Goals

In this article, we’ll use Gitlab CI and Firebase to automate following processes

  • Lint checks
  • Build debug APK file and androidTest APK file
  • Run local unit tests
  • Run instrumented tests
  • Distribute APK file to tester team

We assume that you know the process of creating an Android app, can write and run tests locally, and are familiar with the Firebase and GitLab CI.

Prerequisites


TodoApp

Let create a very simple android TodoApp like this. Full source code can be found here https://gitlab.com/quan_nguyen/todoapp.git

Assume that you're working on feature branch, the goals are making sure the source code is checked and tested before merging to develop branch. Now we start create a .gitlab-ci.yml file.

Create .gitlab-ci.yml file

Gitlab already provides many useful templates to get started with. Just copy the android template one.

Add stages

As mention above, we need to run through 4 stages, add stages to the .gitlab-ci.yml to make sure our jobs are run in correct order

We also need to update ANDROID_COMPILE_SDK, ANDROID_BUILD_TOOLS, ANDROID_SDK_TOOLS variables to match our android project.

Check linting

To run lint checks, build APK files and run local unit tests we need androidSDK tools. The sample before_script in the template Android.gitlab-ci.yml is great, it’s all we need to download cmdline-tools from official source. It also setups environment variables and accept all licenses.

Downloading cmdline-tools and gradle are time taking processes, to avoid wasting of time let cache the android-home and .gradle directory.

Now let move all the cmdline-tools setup commands to setup_cmdline_tools.sh file and add checking if cmdline-tools directory does not exists because we only need to download it in the first time then it will be cached for later pipelines.

Here we need to tweak a bit because we’ll need to run different set of commands in uiTests and distribute job. So move it to job level.

Build debug APK and androidTest APK

app-debug.apk and app-debug-androidTest.apk are output of the assembleDebug job. We’ll need these files in uiTests and distribute job. Let archive them.

Run instrumented tests with Firebase Test Lab

There are two options to run instrumented tests. Run on connected device/emulator or run with Firebase Test Lab. The first option is suitable for running in your local. In ci environment, we use Firebase Test Lab. If you haven't already add Firebase to your Android project, refer to this link to see how to do it https://firebase.google.com/docs/android/setup#console

Now it's time to use gcloud CLI, the official Google Cloud docker image in docker hub can work with gitlab-ci very nicely. We also need a google service account with Editor role, so it can run instrumented tests on behave of you. Refer to this link to see how to create a service account https://cloud.google.com/iam/docs/creating-managing-service-accounts

To read more about gcloud firebase test android run options refer to this link https://firebase.google.com/docs/test-lab/android/command-line#running_your_instrumentation_tests

After the job done, you can see test results on firebase Test Lab.

Distribute your APK to tester team

In the distribute job, we’ll use Firebase CLI to distribute your app to tester team. To save the firebase-tools installing time let use a docker image which has firebase-tools installed already. You can found a very simple but useful one here https://hub.docker.com/r/rambabusaravanan/firebase. The image is not an official one, but if you look at the Dockerfile file there’s not thing to worry about.

To read more about appdistribution:distribute options refer to this link https://firebase.google.com/docs/app-distribution/android/distribute-cli

Now when ever you create a merge request from a feature branch to develop branch, gitlab will automatically run lint checks, build debug APK and androidTest APK, run unit tests and UI tests and finally distribute the debug APK file to tester team.

Enjoy your CI/CD.

References