Continuous Integration with Github, Android and CircleCI
Continuous Integration (CI) plays an important role in the development process for almost every development team, or at least every team I’ve come across anyway. Knowing the effects of your changes is extremely useful to see how the tasks are coming together with other members. It’s also just a good approach to take in general if you’re developing something personally and want to track the progress and stability of your project. There’s a lot of CI tools available to anyone who needs it, with Jenkins or TravisCI being the most recognized tools out there. CircleCI is a fresh take on CI that provides a very clean UI and powerful flexibility to users. Let’s take a look at setting up a basic CircleCI build task for an Android application.
- A Github account (CircleCI also has BitBucket login option)
- Android Studio – I’m using the 3.0 Canary version with a Material Design theme so it might look a little different
Dipping your toes
I’m going to assume you already have an Android app up and running in Android Studio so I’ll skip that part. If you don’t have your application code in Github or another type of Version Control system, I would recommend you do that now. It can be a life-saver if things go bad and you can work on your code from anywhere.
The first thing we need is to create your CircleCI account. CircleCI is free to sign up to and use up to a certain limit (approx 1500 build minutes p/month) so it’s great for individuals. We’ll also be using a technique to speed up build times on CircleCI to reduce build times. When you sign in with your Github account, you’ll be able to choose which projects you’ll want to track / build. Select the project you want to follow and an initial build should trigger. CircleCI’s default configuration triggers a build to occur on any commit to any branch on the repository, but this can be customised later on. CircleCI fires up a container to host your app while it’s building.
CircleCI will attempt to build your Android app, but will more than likely ultimately fail with an error message similar to the one below. This is ok! We were not expecting a successful build anyway, but let’s fix it!
The issue here is that we have not granted the correct permissions to the ./gradlew command on the container that CircleCI has created. To fix this, we first need to create our circle.yml file in our repository (oh dear, we actually have to write config code? sigh )
CircleCI YAML File
Create a file in your app root directory called circle.yml . It’s probably best to switch to Project perspective in Android Studio for this part, as the file is hidden in the Android perspective. This file is YAML, so it might look a bit strange if you’re unfamiliar with it (at least, it did to me!) What we need to do is modify the permissions for the ./gradlew command. You can see how this is done in the override section in dependencies at line 25.
# # Build configuration for Circle CI # general: artifacts: - /home/ubuntu/Calculator/app/build/outputs/apk/ machine: environment: ANDROID_HOME: /usr/local/android-sdk-linux dependencies: pre: - if [ ! -e /usr/local/android-sdk-linux/build-tools/25.0.3 ]; then echo y | android update sdk --all --no-ui --filter "build-tools-25.0.3"; fi; - if [ ! -e /usr/local/android-sdk-linux/platforms/android-25 ]; then echo y | android update sdk --all --no-ui --filter "android-25"; fi; - if ! $(grep -q "Revision=43.0.0" /usr/local/android-sdk-linux/extras/android/m2repository/source.properties); then echo y | android update sdk --all --no-ui --filter "extra-android-m2repository"; fi; cache_directories: - /usr/local/android-sdk-linux/build-tools/25.0.3 - /usr/local/android-sdk-linux/platforms/android-25 - /usr/local/android-sdk-linux/extras/android/m2repository override: - chmod +x gradlew - ANDROID_HOME=/usr/local/android-sdk-linux ./gradlew dependencies test: override: - (./gradlew assemble): timeout: 360
This configuration file informs CircleCI that when the dependencies section is reached, gradlew must be given execution permissions (chmod +x) This will allow ./gradlew to run, but it will fail for a different reason. To use the Android SDK and build tools, you must accept the licence agreements. You can see that in the pre section of dependencies, we’re updating our sdk versions to the one specificied: In this case I’m using build tools 25.0.3 and the API 25 platform. By using if statements and also the cache directories, we can speed up the build preparation process each time. We could’ve specified to update everything regardless of what we already have, but this would not be very efficient.
Passing the Test
The most important part of the above config file is ./gradlew assemble. This is what actually builds your project, and we can see that a timeout of 6 minutes was set on this task. Once you have this file in your project, your CircleCI build should be ready. My builds usually take about 4 minutes to complete, but it is somewhat dependent on how much you pay! Settings on CircleCI can be accessed which change when a build is triggered, such as only on pull requests. CircleCI also has email notifications, along with integrations for Slack channels so it’s a very modern and useful tool for Android development.