Open In App

Automated Release for Android Using GitHub Actions

Last Updated : 11 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Automating the release process for Android applications can significantly enhance efficiency, reduce manual errors, and ensure consistent delivery. GitHub Actions provides a powerful platform to automate workflows directly from your GitHub repository. This article will guide you through setting up an automated release pipeline for Android using GitHub Actions.

Why Use GitHub Actions for Automated Releases?

GitHub Actions allows you to automate, customize, and execute software development workflows right in your repository. With GitHub Actions, you can:

  • Automate the build, test, and release process.
  • Integrate with various tools and services.
  • Trigger workflows based on events like pushes, pull requests, and releases.
  • Ensure consistent and repeatable build environments.

Setting Up Your Android Project

Before diving into GitHub Actions, ensure your Android project is set up correctly with the necessary build configurations.

  1. Gradle Configuration: Ensure your build.gradle file is properly configured with all dependencies and plugins required for building your project.
  2. Versioning: Use a versioning scheme in your build.gradle file to manage app versions.

Creating a GitHub Actions Workflow

  1. Create the Workflow File: Inside your GitHub repository, create a new directory called .github/workflows if it doesn't exist. Then, create a file named release.yml within this directory.
  2. Define the Workflow: Open the release.yml file and define your workflow. Below is an example of a workflow configuration:
name: Android Release

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

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

    - name: Set up JDK 11
      uses: actions/setup-java@v2
      with:
        distribution: 'zulu'
        java-version: 11

    - name: Grant execute permission for gradlew
      run: chmod +x gradlew

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

    - name: Sign APK
      env:
        KEYSTORE_PATH: ${{ secrets.KEYSTORE_PATH }}
        KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
        KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
        KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
      run: |
        ./gradlew assembleRelease
        jarsigner -verbose -sigalg SHA256withRSA -digestalg SHA-256 -keystore $KEYSTORE_PATH app/build/outputs/apk/release/app-release-unsigned.apk $KEY_ALIAS
        zipalign -v 4 app/build/outputs/apk/release/app-release-unsigned.apk app/build/outputs/apk/release/app-release.apk

    - name: Upload APK
      uses: actions/upload-artifact@v2
      with:
        name: app-release.apk
        path: app/build/outputs/apk/release/app-release.apk

Explanation of the Workflow

  • Trigger: The workflow triggers on a push to the main branch.
  • Job: The build job runs on an ubuntu-latest runner.
  • Steps:
    • Checkout: Uses the actions/checkout action to check out the repository.
    • Set up JDK: Uses the actions/setup-java action to set up JDK 11.
    • Grant Permission: Grants execute permission to the gradlew script.
    • Build: Runs the Gradle build command to build the project.
    • Sign APK: Uses environment variables stored in GitHub Secrets to sign the APK.
    • Upload APK: Uses the actions/upload-artifact action to upload the signed APK as a build artifact.

Configuring Secrets

To securely store sensitive information like keystore paths and passwords, use GitHub Secrets.

  1. Add Secrets:
    • Navigate to your repository on GitHub.
    • Go to Settings > Secrets > Actions.
    • Add the following secrets:
      • KEYSTORE_PATH
      • KEYSTORE_PASSWORD
      • KEY_ALIAS
      • KEY_PASSWORD

When done with writing the workflow, commit the changes and go to the Actions tab, you'll see a workflow running.

When both the jobs i.e. Generate APK and Release APK shows green check, go to Code and on the right side in the Releases section, you will see a release published with the name as the repository name happened with v1.

Best Practices

  • Modularize Workflows: Break down your workflows into reusable components and actions.
  • Secure Secrets: Always use GitHub Secrets for sensitive information.
  • Monitor Workflows: Regularly monitor and maintain your workflows to ensure they run efficiently.
  • Test Locally: Test your build and release processes locally before integrating them into GitHub Actions.

Conclusion

Automating the release process for your Android applications using GitHub Actions can significantly streamline your workflow and ensure consistent, reliable builds. By following the steps outlined in this article, you can set up a robust CI/CD pipeline that automates the build, sign, and release process, allowing you to focus more on developing new features and less on manual tasks. Embrace the power of automation and take your Android development workflow to the next level with GitHub Actions.


Article Tags :

Similar Reads