Automating Android Builds with GitHub Actions: From Debug to Release

--

GitHub Actions provides a powerful platform for automating workflows, including building and distributing Android applications. In this article, we’ll walk through the process of setting up a workflow to build a debug version of an Android app and then extend it to create a release build. Let’s dive in!

Prerequisites:

  • A GitHub repository with an Android project.
  • Basic knowledge of GitHub Actions.

Step 1: Setting up a Debug Build

1.1. Create a Workflow File

In your GitHub repository, create a new file under .github/workflows named android_build.yml or AndroidBuild.yml

1.2. Define the Workflow

name: AndroidBuild

on:
pull_request :
branches : [ master ]
push :
branches : [ master ]

jobs:
build:
runs-on : macOS-latest //or ubunto-latest or windows-latest

steps:
- name: Checkout code
uses: actions/checkout@v4.1.0

- name: Set up JDK 11
uses: actions/setup-java@v3.13.0
with:
distribution: 'adopt'
java-version: '17'

- name: Grant execute permissions for gradlew //this step is only for macOS
run: chmod +x ./gradlew

- name: Build with Gradle
run : ./gradlew build

- name: Upload a Build Artifact
uses: actions/upload-artifact@v3.1.3
with:
name: dsTemplate.apk
path: app/build/outputs/apk/debug/app-debug.apk

1.3. Explanation:

  • on: push: This specifies that the workflow should be triggered on every push to the main branch.
  • runs-on: ubuntu-latest: This defines the type of runner (or virtual machine) the job will run on. We’re using the latest Ubuntu version.
  • actions/checkout@v2: This action checks out your repository so the workflow can access its content.
  • actions/setup-java@v2: This sets up the specified version of JDK, which is essential for Android development.
  • chmod +x ./gradlew: This grants execute permissions to the Gradle wrapper script, ensuring it can run.
  • ./gradlew assembleDebug: This command builds the debug version of the Android app.

Step 2: Extending to Release Build

2.1. Store Your Keystore

For a release build, you need a signing keystore. Store it as a GitHub secret:

  • Go to your repository on GitHub.
  • Click on Settings > Secrets > New repository secret.
  • Name it KEYSTORE and paste the Base64 encoded version of your keystore.

2.2. Modify the Workflow

Add the following steps to decode the keystore and build the release version:

    - name: Decode Keystore
run: echo "${{ secrets.KEYSTORE }}" | base64 --decode > /tmp/keystore.jks

- name: Build Release
run: ./gradlew assembleRelease -Pandroid.injected.signing.store.file=/tmp/keystore.jks -Pandroid.injected.signing.store.password=${{ secrets.KEYSTORE_PASSWORD }} -Pandroid.injected.signing.key.alias=${{ secrets.KEY_ALIAS }} -Pandroid.injected.signing.key.password=${{ secrets.KEY_PASSWORD }}

2.3. Explanation:

  • Decode Keystore: This step decodes the Base64 keystore from GitHub secrets and saves it to a temporary location.
  • Build Release: This builds the release version of the app using the decoded keystore and other signing information stored in GitHub secrets.

Conclusion

With GitHub Actions, automating the build process for Android apps, from debug to release, becomes a seamless experience. It ensures consistent builds, reduces manual errors, and accelerates the development cycle. By integrating CI/CD practices into your development workflow, you’re taking a significant step towards improving productivity and code quality.

Remember always to keep your secrets safe and never expose sensitive information in logs or public files. Happy coding!

--

--

Kashif Ahmad Sr.Android Developer @techeasesol.com

Sr. Android Developer(Kotlin || Java || Flutter)| Co-Founder techeasesol.com || Semantic Digitisation Expert