Build Android Apps with GitLab CI/CD

Automate your Android testing and deployment with GitLab CI/CD and save much of your time.

Build Android Apps with GitLab CI/CD

Automate your Android testing and deployment with GitLab CI/CD and save much of your time.

Basic Introduction

Continuous Integration and Continuous Delivery/Deployment has become one of the most important parts of software development these days. It solved quite a lot of problems that we had run into in the past like having to run tests manually in our local environments, and it a pain deploy your application by build your APKs manually and then send them across. I have been using CI/CD for quite a while now, and I found it an important and interesting thing to work with. I am using it to automate my tests and deployments in my company and my own side projects.

CI/CD not only changed the way we do the development and deployment but it also improved our productivity by focusing on coding only and forget about deployment and running tests.

There are plenty of tools and providers that we can use with our projects. Some of them are free and some are not.

The free one that I usually use in my personal projects is GitLab CI/CD, it gives us a 1000 build minutes per month which is enough for my personal use.

There are also some of the best our there such as

  • GitHub Actions
  • CircleCI
  • TravisCI
  • Bitbucket Pipelines
  • Azure Pipelines

I will cover only Gitlab CI/CD in this blog post. I will talk about some of those as well in the far future. Hopefully!

Get Started

Of course, you need to have an Android project repository in GitLab to get started. I assume that you are good at that.

All CI/CD instructions and commands are in a file called .gitlab-ci.yml We use it to tell GitLab what to do with our source code.

I already made a docker container for building an Android project so that you wouldn’t need to do it by yourself.

After adding this file into your root directory in your project, and after pushing it into GitLab, you will see your app is being built.

GitLab CI Pipelines — Running

After the build is successful, the status will turn into apassed green color.

GitLab CI Pipelines — Success
This what all developers want to see!

✅ As you can see in the screenshot, the first build took 4 minutes and 14 seconds, but the second is much faster it took only 42 seconds. This is because we cached .gradle/wrapper and .gradle/caches in the process.

It’s important to keep your CI fast since it may help us reduce costs as well as time.

By click on each pipeline, you will be redirected to a jobs page which shows us about all the jobs in the pipeline.

GitLab Pipelines jobs

In this example, we have only one job assembleDebug however, you can have as many jobs as you want. For instance, you can have runTestsassembleReleaseinternalPublishReleaseplayStorePublishRelease and so on and so on. It gives you a lot of flexibility. Or maybe you want to build different types of APKs at the same time, GitLab CI also could do that for you.

By clicking on assembleDebug , you will be redirected to the detail page where it shows you all the output logs.

GitLab CI Job logs

⚠️ However, sometimes, we made mistakes and we committed it without knowing, GitLab Pipelines will build it and then if it is a failed build, it will immediately send you an email telling you what went wrong, which is quite handy in my opinion.

Oops! It’s a failed build

Also, you can find out what went wrong on the detail page by reading the error logs.

Build Artifacts

It is one of my favorite features of almost all CI/CDs these days. It lets you save the output files after your build is completed. In this example, we want to have an APK file saved after the build is finished.

Job Artifacts

You can down job artifacts that contain the APK file from the job detail page or you can do it on the Pipelines page as well.

Jobs Artifacts Download

Resources

GitLab project: https://gitlab.com/seanghay.dev/myfeed

Android CI: https://github.com/seanghay/android-ci

GitLab CI: https://docs.gitlab.com/ee/ci/


In conclusion, thanks for reading and stay safe. I will cover more about some advanced features on the next blog posts. Stay tuned!