Android Project Automated Build and Test using Circle CI

Raka Westu Mogandhi
3 min readAug 27, 2016

--

Deploying Android project is a task that every Android programmers hate because it’s a long recurring task before they can test the program they build. Of course, anything recurring or repeatable can be automated using Continuous Integration tools.

Here I want to share some of my experience using Circle CI as Continuous Integration tools that can help me automating the build and instrumentation test.

Circle CI is free Continuous Integration tools that works with multi platforms (frontend, backend, mobile and docker) and it’s fully integrated with Github. It has easy configuration using single`yml` script and simple dashboard to manage environment variables, project settings, etc.

Circle CI Dashboard

Screenshot above was one of my project dashboard. In this page we can see all build or test that already done or still running from our project. The build list is divided by branch name too so we can filter it based on the branch name.

Circle CI Dashboard Build Detail

We can see build detail in dashboard build detail screen like above. It was running instrumentation test using configuration below.

So let me tell you what is happening with configuration above.

  • General → general build-related configurations that are not related to a specific phase, and an experimental section for early access previews of configuration changes under consideration.
    In this section I set the build artifact directory which is the APK build directory in the Android project.
  • Machine → adjusting the VM to project preferences and requirements. Here I set that the environment is Android-specific.
  • Dependencies → setting up project’s language-specific dependencies.
    In pre-build phase, I set that latest Gradle must be downloaded and installed. Then I need to run some shell script that is copying the environment variables into `gradle.properties` because the project contains some build configuration that was saved in `gradle.properties` instead of environment variables.
    In override section, all Android dependencies (SDK, platform tools, build tools, support package, etc.) need to be downloaded and resolved by using `gradle dependencies` command.
  • Test → running tests. In this section I need to run instrumentation test which is using Espresso so I am sure that my application running properly using some scenario defined beforehand.

Running test in Circle CI is easy as long as it can be ran using command line. For Espresso instrumentation test, it’s divided into some steps before we can run the test.

1.
- (./gradlew assemble):
timeout: 360
This is to ensure the application was build successfully before the test process.2.
# start the emulator
— emulator -avd circleci-android22 -no-audio -no-window: background: true parallel: true
Start the emulator without window and audio service. This configuration will make the emulator running with minimum resources.3.
# wait for it to have booted
- circle-android wait-for-boot
The emulator needs to be waited until it finished the boot.4.
# unlock the emulator screen
- sleep 30
- adb shell input keyevent 82
This is to ensure the emulator screen wasn't on sleep mode.5.
# run tests against the emulator.
- ./gradlew connectedDevelopmentDebugAndroidTest -PdisablePreDex
Running the test on emulator. Added `-PdisablePreDex` option is to improve build performance by reducing the resources used by the build process.

Thanks for reading this article. Hope it can inspire you to automate the long and recurring process in your Android project so you can focus on another task and be more productive :)

--

--