Codementor Events

Build Android CI with Bitbucket Pipeline and HockeyApp Part 1

Published Feb 14, 2018Last updated Feb 16, 2018
Build Android CI with Bitbucket Pipeline and HockeyApp Part 1

Continuous Integration (CI) is the process of automating the building and testing of code every time a team member commits changes to version control.

“Continuous Integration doesn’t get rid of bugs, but it does make them dramatically easier to find and remove.” — Martin Fowler, Chief Scientist, ThoughtWorks

Configuring a CI can be tricky for Android apps. But let’s see how quick it is to build an Android app with Bitbucket Pipeline and deliver it with Hockey app.

What is Bitbucket Pipeline?

Bitbucket Pipeline is a continuous integration service that Atlassian geniously integrated in their git solution Bitbucket last year. It’s based on Docker container technology and requires only a YAML file to start using it.

Create a Repo on Bitbucket

To begin this process, you should have a repository (hosting your Android project) on Bitbucket. If you have one already, skip this step.

For new users, you have to sign up here.

Click on the + button on the left Nav bar and select->Repository.

Follow here to complete the process and have your Android project uploaded to your just-created repo.

Yeah! Back to the business of the day.

Enable Bitbucket Pipeline

To start using Bitbucket Pipeline, you simply need to go to your repository, and under the Pipeline menu, choose a language template — in this case, Java(Gradle). Click on commit. This will automatically create a YAML fileto start with under your master branch.

Configure Docker

Here, you’ll need a Docker image to build your app. It has to include your Android build environment to be able to compile it. To simplify it, Uber dev team created one for this purpose that you can use here.

For those familiar with Docker, you can go ahead and create your own Docker light image if you don’t need all of the packages included in the Uber image.

For this post, we will be using the Uber image.

You can specify a custom docker image from Docker Hub as your build environment.

image: uber/android-build-environment:latest
pipelines:
  default:
    — step:
    script: # Modify the commands below to build your repository.
    — echo “Start default step”
    — ./gradlew assembleDebug

NOTE:
your build will fail if the YAML file is not right indented. To help handle this, use Bitbuck-pipeline validator.

First, I specify the image that is going to be used to build the app. Then, by default, for any branches, it will execute commands. Here, I build a debug APK using Gradle wrapper. Our continuous integration is ready!

Now we will create a build.sh file to house our reusable script. Make sure you give it the right permissions.

#!/bin/sh# Add Android SDK license in a default filemkdir "${ANDROID_HOME}/licenses" || trueecho "8933bad161af4178b1185d1a37fbf41ea5269c55" > $ANDROID_HOME/licenses/android-sdk-licenseecho "d56f5187479451eabf01fb78af6dfcb131a6481e" > $ANDROID_HOME/licenses/android-sdk-license# Build the app./gradlew assembleDebug

To give the file the permission to execute, run this on your terminal (make sure you're currently in the your project root folder).

git update-index --chmod=+x build.sh

Check to see if it has 0755.

git ls-files --stage
100755 58b61dd2872df1ba7a0ad96f81e801a788c46502 0 build.sh

Update the Bitbucket-pipeline.ymlfile to point to the build.sh file. This file contains your Android SDK license.

 # You can specify a custom Docker image from Docker Hub as your build environment.
 image: uber/android-build-environment:latest 
 pipelines: 
     default: 
     — step: script: # Modify the commands below to build your repository. 
     - unset ANDROID_NDK_HOME # To unset the NDK env if not needed 
     — echo “Start default step” 
     — ./build.sh 
     - echo "Amazing"

Coooool!!!

Our Continuous Integration build and test is successful!

Once built and tested for a specific branch, you can actually deploy it somewhere. That’s what we’re going to do with HockeyApp in Part 2 of this post.

Thank you for reading. Please comment in case you run into any difficulty.

I will be glad to help.

Discover and read more posts from Nsikak Thompson
get started
post commentsBe the first to share your opinion
Show more replies