Setting Up CI/CD pipeline for Android Apps with GitHub Actions

harshil rastogi
5 min readSep 30, 2023

--

GitHub Actions is a powerful tool provided by GitHub that allows you to automate various tasks for your repositories. For Android developers, setting up GitHub Actions can significantly enhance the development workflow by automating tasks like building and testing the Android app. In this article, we will explore how to set up GitHub Actions for Android projects.

This is not what you are thinking we are going to build :)

Prerequisites

  1. GitHub Repository: First, make sure your Android project is hosted on GitHub.
  2. Gradle Wrapper: Ensure that your Android project uses the Gradle Wrapper (gradlew) for building. This is the standard setup for Android projects.

Creating the GitHub Actions Workflow

A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked in to your repository and will run when triggered by an event in your repository, or they can be triggered manually, or at a defined schedule.

Workflows are defined in the .github/workflows directory in a repository, and a repository can have multiple workflows, each of which can perform a different set of tasks.

Step 1: Create a Workflow File

Create a .github/workflows directory in the root of your repository if it doesn't exist. Inside this directory, create a new YAML file, for example, android-build.yml. This file will define your GitHub Actions workflow.

By Clicking on Configure you can create your very first workflow, github provides a marketplace of its own where you can configured predefined workflow in your YAML files as well.

Step 2: Define the Workflow

In the YAML file, define your workflow. Here’s a basic example that builds and tests an Android project:

We can control when a workflow should run:

Lets dig deeper and add some jobs to your workflow, here I am adding two jobs one is to build the project and second one is to check the lint in my push or pull request.

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, Github Actions!

# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.


android-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 1
- name: set up JDK
uses: actions/setup-java@v3
with:
distribution: zulu
java-version: 11
cache: gradle
- run: ./gradlew lint
- uses: harshil119/action-android-lint@v3
with:
report-path: build/reports/*.xml
continue-on-error: false

In above code snippet job 1 is quite simple its just printing the text.

Job 2 is to check lint warnings if any in your code,

runs-on: ubuntu-latest

Configures the job to run on the latest version of an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by GitHub.

    - uses: actions/checkout@v4

The Uses: keyword, this is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code.

- name: set up JDK
uses: actions/setup-java@v3
with:
distribution: zulu
java-version: 11
cache: gradle

This step uses the actions/setup-java@v3 action to install the specified version of the Java.

- run: ./gradlew lint
- uses: harshil119/action-android-lint@v3
with:
report-path: build/reports/*.xml
continue-on-error: false

This step uses the typical gradle command to check lint in your android project, which we use to do in terminal of android studio. Once it runs successfully it will create a report at above given path and this job will not continue if any error shows up.

Viewing the activity of a workflow

When ever a user push the code onto the main branch this workflow will trigger. When your workflow is triggered, a workflow run is created that executes the workflow. After a workflow run has started, you can see a visualization graph of the run’s progress and view each step’s activity on GitHub.

  1. On GitHub.com, navigate to the main page of the repository.
  2. Under your repository name, click Actions.

A Successful pipeline will look like this

Conclusion

Setting up GitHub Actions for your Android projects automates the build and test process, saving time and ensuring the quality of your codebase. As your project evolves, you can extend the workflow to include additional steps like deploying to beta testers or publishing the app to app stores. GitHub Actions provides a flexible and powerful automation platform that can streamline your Android development workflow.

Happy coding!

--

--