Hello there! Android devs👋, This article how to develop and maintain easy building, flexible maintenance, automated deployment and other DevOps operations for android applications using GitHub Actions and deployment in Google Play Store! Lets go 🚀
Article on Hashnode: https://tharunbalaji2004.hashnode.dev/android-ci-cd
What is meant by CI ?
CI stands for Continuous Integration, which is a development practice that delivers software to the end user with production reliability. The Continuous Integration (CI) is an automated integration process which generates a build and runs automates tests against it. Usually, a CI is attached with a Repository or Codebase and all the changes are merged before starting it.
What is meant by CD ?
CD stands for Continuous Delivery, which is an automated process of deploying and making the application available successfully to use. The CD process is started only when the application has passed through the integration process and tested with no critical issues.
CI/CD for Android App
(Figure: CI/CD Pipeline Architecture for Android)
CI pipeline
Now let us design our CI pipeline flow so that we are clear what we want to achieve. For any Android project I would recommend the following steps:
- Android Lint Check
- Unit Tests
- Instrumentation Tests
- Static Code Analysis
- Build Debug apk (Packaging)
1. Setup GitHub Actions for repository
To add GitHub Actions workflow file to your repository you need to create a yaml file .github/workflows/ci.yaml
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
start:
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v2
- name: Run sample script
uses: echo Hello, world
- name - refers the action name
- on push, pull_request - It states the branch to be used for CI process when push or pull_request to the specified branch happens.
- jobs - used for specifying the jobs to be performed
- sample - the name of job to be performed
- runs-on - it specifies on which serves should the process be performed say ubuntu
- steps (name, uses) - Each step has its own name and uses, and the first step should be to checkout the code
2. Perform Android Lint check
🤔 What is meant by Lint ?
😎 The lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. Basically it's an basic code correction and suggestion tool
Now that our basic configuration is in place, we will add Lint check as our first job. Let us understand what the following configuration does.
Step 1: runs-on: ubuntu-latest tells to run the job on latest ubuntu machine
Step 2: actions/checkout@v2 action checks out the codebase on the machine
Step 3: Once we have the codebase on the machine, run ./gradlew lintDebug
Step 4: Publish the lint report as a github artifact
lint:
name: Perform lint check
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v2
- name: Run lint
uses: ./gradlew lintDebug
- name: Upload html test report
uses: actions/upload-artifact@v2
with:
name: lint.html
path: app/build/reports/lint-results-debug.html
- with - it uploads the artifact as the specified name to the path
3. Perform Android Unit Tests
🤔 What is meant by Unit Testing ?
😎 Unit tests in Android are used to test individual units or components of an application in isolation. These tests focus on verifying the functionality of a specific class, method, or module without external dependencies.
Unit Tests reference: https://developer.android.com/training/testing/local-tests
Our second job would be to run the unit tests. This job will run after the lint job and that is why you see needs: [lint] in the below config.
Step 1: runs-on: ubuntu-latest tells to run the job on latest ubuntu machine
Step 2: actions/checkout@v2 action checks out the codebase on the machine
Step 3: Run ./gradlew test will run the unit tests
Step 4: Publish the test report folder as a github artifact
unit-test:
name: Perform Unit Testing
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v2
- name: Run tests
uses: ./gradlew test
- name: Upload test report
uses: actions.upload-artifact@v2
with:
name: