Android Continuous Integration — CircleCI

Chan Bo
Open Knowledge
Published in
5 min readMar 15, 2019

In this article I’m going to implement CircleCI Continuous Integration for any android projects. So now let’s care of some questions:

  • What is Continuous Integration?
  • Why use Continuous Integration?

What is Continuous Integration?

Continuous Integration or CI is the process of automating the build and testing of code every time a team member commit changes to version control. CI encourage developers to share their code and unit tests by merge their changes into a shared version control repository after every small task completion. Committing code triggers an automated build system to grab the latest code from the shared repository and to build, test, and validate the full master branch.

Why using Continuous Integration?

One of the important points of using CI is about have less conflicts when integrating code. Once the code is frequently merged, it has less chances of breaking what already exists. And even if it breaks what was already working, it is easier to solve. Another very important thing when talking about CI is that it needs to be supported by a suite of automated tests (unit tests and integration tests).

Setting up CircleCI

To run you first CircleCI build on CircleCI version 2.x platform, you must complete the following steps:

  1. Go to CircleCI Sign Up page.
  2. Click one of the Start buttons on the page to begin logging in or to start the process of allowing CircleCI to access your code on GitHub or Bitbucket. To limit Circle from the accessing your private GitHub repositories, select Public Repos Only from the Start with GitHub menu when you sign up and on every subsequent login.
  3. Type your GitHub or Bitbucket username, password, and two factory authorization if applicable, then click Sign In/Login.
  4. Click the Authorize Application or equivalent button. The CircleCI Builds Dashboard appears.
  5. Use the Project Setup pages of the CircleCI app to start building your project code.

CircleCI Configuration

  • Create your Bitbucket or GitHub repository for your projects.
  • Click on your root project and create .circleci folder inside that folder create config.yml file.
  • Now let’s write some config inside config.yml file

Config Walkthrough

version 2

We always start with the version to clarify which version of CircleCI that we are using. Currently it has two version only.

We have a jobs key. Each job represents a phase in your Build-Test-Deploy process. So everything else is going to live under that key.

We use the CircleCI provided Android image with the api-28-alpha tag. You can see Docker Images for more information about what images are available.

Now we’ll add several steps within the build job. We start with checkout so we can operate on the codebase.

Next we pull down the cache, if present. If this is your first run, or if you’ve changed either of your build.gradle file, this won’t do anything. We run ./gradlew androidDependencies next to pull down the project’s dependencies. Normally you never call this task directly since it’s done automatically when it’s need, but calling it directly allows us to insert a save_cache step that will store the dependencies in order to speed things up for next time.

If you haven’t undergo with ./gradlew , you can look at Build your app from the command line.

Then ./gradlew lint test run the unit tests, and runs the built in linting tools to check your code for style issues.

After we get result from ./gradlew lint test , we upload the build reports as job artifacts, and we upload the test metadata (XML) for CircleCI to process.

Docker Images

For convenience, CircleCI provides a set of Docker images for building Android apps. These pre-build images are available in the CircleCI org on Docker Hub. The source code and Dockerfiles for these images are available in GitHub repository.

The CircleCI Android Image is based on the openjdk:8-jdk official Docker image, which is based on buildpack-deps. The base OS is Debian Jessie, and builds run as the circleci user, which has full access to passwordless sudo.

API Levels

We have a different Docker image for each Android API level. To use API level 28 (Android 9) in a job you should select circle/android:api-28-alpha .

Alpha Tag

CircleCI Android Docker images are currently tagged with the suffix -alpha . This is to indicate the images are currently under development and might change in backwards incompatible ways from week to week.

*** Now let’s start our first build in CircleCI ***

  1. Please push your source code to your Bitbucket or GitHub repository that you’ve created. (write you .circleci/config.yml file before push to your repository)
  2. Go to CircleCI Website > Go to app > On the left side choose ADD PROJECTS > Browse your project > Set Up Project

3. After Set Up Project Go to > JOBS or WORKFLOWS you will see your project was running build

4. You can click on your build and see more detail about it.

5. After finish your build, you can see the result like Test Summary, Artifacts, and your Configuration as below:

Finally we have run our first build in our CircleCI.

That’s all about CircleCI Configuration with Android. In the next blog I’ll show you how to use CircleCI with Fastlane for Android App Development and we will go more advance about both CircleCI and Fastlane. So if you haven’t read about Fastlane, you can go to read Part 1, Part 2, and Part 3. If you have any problem, please let me know. 👂Thanks. 👋.

--

--