Mehedi Hassan Piash | Senior Software Engineer | Android | iOS | KMP | Ktor | Jetpack Compose | React-Native.

November 12, 2024

CI/CD for Android: A Comprehensive Guide

November 12, 2024 Posted by Piash , , , , No comments

Continuous Integration and Continuous Delivery (CI/CD) has become essential in Android development, helping developers automate building, testing, and deploying applications. In this article, we’ll create a straightforward CI/CD pipeline for Android using GitHub Actions, focusing on building, testing, and artifact management.

Step 1: Initialize a Workflow File
GitHub Actions workflows are configured in YAML files stored in .github/workflows. In your project, create a file named android-ci.yml with the following content.

name: Android CI/CD

on:
push:
branches:
- master # Run the workflow on pushes to the main branch
- main # Run the workflow on pushes to the main branch
pull_request:
branches:
- master # Run the workflow on pushes to the master branch
- main. # Run the workflow on pushes to the main branch

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up JDK
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '17' # Change to the version you need for your project

- name: Cache Gradle files
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
gradle-${{ runner.os }}

- name: Build and test
run: ./gradlew build --no-daemon

- name: Run unit tests
run: ./gradlew test --no-daemon

# Upload debug APK as an artifact
- name: Build debug APK
run: ./gradlew assembleDebug --no-daemon

- name: Upload APK
uses: actions/upload-artifact@v3
with:
name: movie-world-apk
path: app/build/outputs/apk/develop/debug/app-develop-debug.apk # Path to your APK

Workflow Explanation

1. Triggers: This workflow runs on pushes and pull requests to main and master branches.

2. Checkout Code: Uses actions/checkout@v2 to retrieve your code.

3. Set Up JDK: Installs the required Java Development Kit (JDK) with actions/setup-java@v2.

4. Cache Gradle Dependencies: Speeds up builds by caching Gradle files, saving dependencies between builds.

5. Build and Test: Runs ./gradlew build to build the project and ./gradlew test to execute unit tests.

6. Assemble Debug APK: Builds a debug APK with assembleDebug.

7. Upload Artifact: Stores the generated APK as an artifact for download and further review.

GitHub link: https://github.com/piashcse/Hilt-MVVM-Compose-Movie/tree/master/.github/workflows

Medium: https://piashcse.medium.com/cd-cd-for-android-a-comprehensive-guide-9c8129387ca8

0 comments:

Post a Comment