“Continuous Integration/Delivery” for Android with GitHub Actions — Part 1

Tarek Ben Driss
ProAndroidDev
Published in
6 min readApr 11, 2021

--

For French-speaking developers, you can find this article in French here.

Dear developers, In this article I will present you how to automate the test execution and build of your Android application while using the github feature: “GitHubAction”

But first, we need to know what ‘continuous integration’, ‘continuous deployment’, and ‘GitHub Actions’ are

Certainly, you have heard about ongoing integration and deployment. These are two “features” that concern a broader topic that is DevOps.

According to Wikipedia, continuous integration, also known as CI, is “a set of practices used in software engineering consisting in verifying each modification of source code that the result of the modifications does not produce regression in the developed application. The concept was first mentioned by Grady Booch1 and generally refers to the practice of extreme programming. The main purpose of this practice is to detect integration problems as soon as possible during development. In addition, it allows to automate the execution of test suites and to see the evolution of the software development.”

The following figure summarizes the features of continuous integration.

On the other hand, continuous deployment, or CD, is “a software development strategy where any code validation that passes the automated test cycle is automatically transferred into the production environment, thus propelling the changes to the software users”. According to LeMagIT.

Thus, the CI/CD essentially help to carry out the project faster by automating the “build”, the test and the “release”. As shown in the figure below.

And in order for developers to automate all of this, they often use tools like Jenkins, Travis CI, CircleCI … And lately, the newborn GitHub called GitHub Actions.

GitHub Actions

We can present it as the Oreo production factory, where we find an assembly line that passes through several machines creating the cookies, adding chocolate, cream … in order to have the package well packed with Oreo pieces.

In our case, and for an Android project, the chocolate and the cream are the test and the build. And the wrapped cookie is the deliverable which is the APK file.

So let’s get to work now. And of course, you need to have an account on GitHub.

After choosing your GitHub repository, click the Actions menu at the top.

GitHub Actions gives you the possibility to easily install CI/CD on your project, so it creates the file “.github/workflows/android.yml”

As mentioned in the android.yml file, with each push, GitHub Actions automatically launches the build of your project using the latest version of ubuntu.

We will modify this file to automate unit tests and the generation of an APK for each push on the repo.

Unit Tests

The following code snippet will launch at each push on the master branch, the unit tests with the command “bash ./gradlew test — stacktrace”

name: Android CI/CDon:
push:
branches:
- 'master'jobs:
test:
name: Run Unit Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Unit tests
run: bash ./gradlew test --stacktrace

Building the APK

We’ve finished with unit testing, and now we’re talking about generating the deliverable for an Android project, the APK.
The following code is added in the jobs section, which generates the APK file “app-debug.apk” and places it in “app/build/outputs/apk/debug”. And just like unit tests, this treatment is done on the latest version of ubuntu and using Java 8.

apk:
name: Generate APK
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build debug APK
run: bash ./gradlew assembleDebug --stacktrace
- name: Upload APK
uses: actions/upload-artifact@v1
with:
name: app
path: app/build/outputs/apk/debug/app-debug.apk

And finally our “android.yml” will be as follows:

And now it’s time to test what we did before.

Once we push a new commit, either through the command prompt or through the tools of Android Studio, our Oreo cookie production chain begins its work:

Just go to “Actions” in your repository to view the test and build steps.

The build of the apk file:

And at the end, the GitHub Actions tells us that the execution of unit tests as well as the generation of the apk are done successfully.

And we can find and download our generated apk file, it’s from “Artifacts” in the upper right.

PS: If you encounter an error that looks like this:

So it is enough to modify, in the file Android.yml, the lines “runs-on: ubuntu-latest” by “runs-on: windows-latest

And lo, we finish with this first part of the tutorial. You can find the second part here.

Any questions? Notes?… To your keyboards then :D

Sharing (knowledge) is caring 😊 Thanks for reading this article. Be sure to clap or recommend this article if you found it helpful. It means a lot to me.

If you need any help, join me on LinkedIn and GitHub.

--

--