O U I C O D I N G

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

But first, we need to know what is “continuous integration”, “continuous deployment”, and “GitHub Actions”.

Certainly, you’ve heard about ongoing integration and deployment. These are two “functionalities” that relate to a broader subject which is DevOps.

According to Wikipedia, Continuous integration, also known as CI, is “a set of practices used in software engineering that consists of checking every time the source code is modified that the result of the modifications does not produce a regression in the developed application. The concept was first mentioned by Grady Booch1 and generally refers to the practice of extreme programming. The main goal of this practice is to detect integration problems as early as possible in the development process. In addition, it allows to automate the execution of the test suites and to see the evolution of the software development”.

The following figure summarizes the functionality of continuous integration.

Image for post

Image for post

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

Image for post

Image for post

Therefore, CI/CDs essentially help to realize the project faster by automating the “build”, the test and the “release”. This is shown in the figure below.

Image for post

Image for post

And in order for developers to automate all this, they often use tools such as Jenkins, Travis CI, CircleCI… And lately, the newest addition to GitHub is called GitHub Actions.

GitHub Actions

It can be presented as the Oreo production plant, where there is an assembly line that goes through several machines creating the biscuits, adding chocolate, cream… in order to have the packet well wrapped with Oreo pieces.
In our case, and for an Android project, chocolate and cream are the test and build. And the packaged cookie is the deliverable which is the APK file.

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

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

Image for post

Image for post

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

Image for post

Image for post

As mentioned in the android.yml file, at each “push”, GitHub Actions automatically starts building your project using the latest version of ubuntu.
We’re going to modify this file to automate the unit tests and the generation of an APK for each push on the repo.

Unit tests

The following piece of code 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/[email protected]
- name: set up JDK 1.8
uses: actions/[email protected]
with:
java-version: 1.8
- name: Unit tests
run: bash ./gradlew test --stacktrace

The APK generation

We have finished with the unit tests, and we are now approaching the generation of the deliverable for an Android project, the APK.
We add in the jobs section the following code, which generates the APK file “app-debug.apk” and places it in “app/build/outputs/apk/debug”. And just like the unit tests, this processing is done on the latest version of ubuntu and using Java 8.


apk:
name: Generate APK
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- name: set up JDK 1.8
uses: actions/[email protected]
with:
java-version: 1.8
- name: Build debug APK
run: bash ./gradlew assembleDebug --stacktrace
- name: Upload APK
uses: actions/[email protected]
with:
name: app
path: app/build/outputs/apk/debug/app-debug.apk

And at the end, our “android.yml” will be as follow:


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


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

Image for post

Image for post

Image for post

Image for post

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

Image for post

Image for post

Image for post

Image for post

Image for post

Image for post

The build of the apk file:

Image for post

Image for post

Image for post

Image for post

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

Image for post

Image for post

And we can find and download our generated apk file from “Artifacts” on the top right.

Image for post

Image for post


PS: If you are faced with an error that looks like this one:

Image for post

Image for post

You just have to modify, in the file Android.yml, the lines “runs-on: ubuntu-latest” by “ runs-on: windows-latest”


That’s it, we finish with this first part of the tutorial.

Any questions? Remarks ?… To your keyborads then 😀

Par Jean Marc Lahaye le 03 juin 2019

Articles Similaires

OUIcoding facilite, permet, promeut et célèbre l’innovation !

Informations

Nous suivre

Email: [email protected]
Tél: +33 1 84 19 03 47 
Tél: +33 6 58 26 23 59