🚀 How to Automate Android Builds with GitHub CI/CD and Gradle

🚀 How to Automate Android Builds with GitHub CI/CD and Gradle

🔧💡 Automating Android builds using GitHub CI/CD (Continuous Integration and Continuous Delivery) is a powerful way to ensure consistency, speed, and quality in your development process. With just a few configuration files, you can trigger builds, run validations, and publish artifacts automatically on every code change.

To achieve this, GitHub provides a system based on three core components:

  • Workflows – the orchestration logic for automation
  • Actions – reusable building blocks that execute tasks
  • Runners – the machines where the jobs actually run

In this article, you'll learn how to configure a CI pipeline using these tools to build your Android app using Gradle — automatically and reliably.


🧩 Understanding Workflows, Actions, and Runners

✅ Workflow

A workflow is a YAML file that defines a series of automated steps that run when specific events happen in your GitHub repository — such as a code push, a pull request, or even a scheduled timer.

Workflows are saved inside your project under:

.github/workflows/
        

Each workflow can contain multiple jobs, and each job is made of steps that define what to do.

Example trigger:

on:
  push:
    branches: [ "main" ]
        

This tells GitHub to run the workflow whenever someone pushes to the main branch.


⚙️ Action

An action is an individual task that can be reused across multiple workflows. Actions are the core building blocks of automation. They can do things like:

  • Check out your code
  • Set up a Java environment
  • Cache dependencies
  • Build and package your app
  • Upload or deploy artifacts

You can use:

  • ✅ Official GitHub-maintained actions (e.g., actions/checkout)
  • 🌍 Community actions from the GitHub Marketplace
  • 🧱 Your own custom actions

Example of using an action:

- name: Checkout repository
  uses: actions/checkout@v4
        

🏃 Runner

A runner is the server where your workflows actually execute. GitHub provides hosted runners with popular environments like Ubuntu, Windows, and macOS, or you can set up self-hosted runners for more control.

Specify the runner using runs-on:

runs-on: ubuntu-latest
        

Each job in a workflow runs in a clean environment on its own runner, which ensures isolated, repeatable builds.


📦 Prerequisites

Before getting started, make sure you have:

  • 📁 An Android project hosted on GitHub
  • ⚙️ Gradle properly configured in your project
  • 🧠 Basic knowledge of YAML and CI/CD concepts


🛠️ Creating the Build Workflow

Now let’s create a workflow that builds your Android app on every push or pull request.

Create a file named .github/workflows/android-build.yml and paste the following configuration:

name: Android Build

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    name: Build Debug APK
    runs-on: ubuntu-latest

    steps:
      - name: 📥 Checkout repository
        uses: actions/checkout@v4

      - name: ☕ Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: 17

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

      - name: 🔓 Make Gradle executable
        run: chmod +x gradlew

      - name: 🛠️ Build Debug APK
        run: ./gradlew assembleDebug

      - name: 📦 Upload APK
        uses: actions/upload-artifact@v4
        with:
          name: debug-apk
          path: app/build/outputs/apk/debug/app-debug.apk
        

💡 Useful Tips

  • Use Gradle cache: As shown above, caching Gradle speeds up subsequent builds and reduces bandwidth usage by reusing downloaded dependencies.
  • 🔐 Automate release builds with signing: Store signing keys in GitHub Secrets and configure your build.gradle for secure automation.
  • 📱 Continuous distribution: Connect with Firebase App Distribution or Play Store APIs to send builds directly to testers or publish them.
  • 🧼 Validate code quality: Add steps for linting, static analysis, or formatting checks in the same workflow.


✅ Conclusion

Automating Android builds with GitHub CI/CD and Gradle helps make your development process faster, safer, and more consistent. You gain the ability to detect issues earlier, save time on manual tasks, and ensure that your builds are always reproducible.

#Android #AndroidDev #Github

Flávio Meira

FrontEnd Software Engineer | JavaScript, TypeScript, ReactJS & Next.js Specialist | Accessibility a11y | MBA in FrontEnd Development & Software Architecture | iFood

1w

Awesome guide! Automating Android builds with GitHub Actions and Gradle is a game-changer—clean, repeatable, and perfect for speeding up delivery.

Like
Reply
Leo E.

DevOps Engineer | DevSecOps | GitOps | Cloud | Terraform | Ansible | Puppet | CI/CD | AWS | Kubernetes | Docker | Shell

1w

Cool breakdown! Leveraging Gradle wrapper for the builds is a smart move, making the pipelines able to fit any project. One tip I can give, since I wrote tons of workflows: you can replace the "actions/cache" usage, opting for "gradle/actions/setup-gradle" instead. The latter gives you lots of benefits in using the wrapper, including Gradle and dependency caching, detailed reporting cache usage and configuration, dependency graph reports for Dependabot, and automatic capture of build scan links, all with minimal configuration!

Like
Reply
João Paulo Ferreira Santos

Data Engineer | AWS | Azure | Databricks | Data Lake | Spark | SQL | Python | Qlik Sense | Power BI

1w

Very informative!

Like
Reply
Otávio Prado

Senior Business Analyst | Agile & Waterfall | Data Analysis & Visualization | BPM | Requirements | ITIL | Jira | Communication | Problem Solving

1w

Very informative! Thanks for sharing Pedro Borba ! 🚀💯

Like
Reply

To view or add a comment, sign in

More articles by Pedro Borba

Insights from the community

Others also viewed

Explore topics