Showing posts with label GitHub Actions. Show all posts
Showing posts with label GitHub Actions. Show all posts

Setting Up GitHub Actions for Efficient Android App Development

GitHub Actions is a powerful tool for automating workflows in software development. In the context of Android app development, you can use GitHub Actions for Continuous Integration (CI) and Continuous Deployment (CD) to streamline your build, test, and deployment processes. Here's an overview of how Android engineers can set up GitHub Actions in their Android app project:

1. Setting up GitHub Actions for Android CI/CD

Step 1: Create a .github folder in your repository

In your Android project, create a .github folder at the root of your repository.

Step 2: Create a workflow file

Inside the .github folder, create a workflows directory. Inside this directory, create a .yml file (e.g., android.yml). This file will define the CI/CD workflow for your Android app.

Here's an example of a basic GitHub Actions configuration file for an Android project:

name: Android CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        java-version: [11]
        android-ndk-version: [22.1.7171670] # Choose the NDK version you need

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

      - name: Set up JDK
        uses: actions/setup-java@v2
        with:
          java-version: ${{ matrix.java-version }}

      - name: Set up Android SDK
        uses: android-actions/setup-android@v2
        with:
          api-level: 30  # Set your target Android API level
          ndk-version: ${{ matrix.android-ndk-version }}

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

      - name: Run tests
        run: ./gradlew testDebugUnitTest

      - name: Upload build artifacts
        if: success()
        uses: actions/upload-artifact@v2
        with:
          name: build
          path: app/build/outputs

Key Steps in the Workflow

  1. actions/checkout: This action checks out the repository's code so that it can be used in subsequent steps.
  2. Set up Java: Android projects require a specific version of Java, and this action sets up Java 11 (as an example) for your project.
  3. Set up Android SDK & NDK: This step installs the Android SDK and NDK on the CI machine so it can build Android apps.
  4. Build the app with Gradle: This step builds your Android app using Gradle. The ./gradlew build command compiles the app.
  5. Run tests: The ./gradlew testDebugUnitTest command runs unit tests for your Android project.
  6. Upload artifacts: After a successful build, you can upload artifacts (such as APKs or logs) as build outputs for further use.

2. Additional Steps

You can extend the workflow to include more advanced steps, such as:

  • Run UI tests (Espresso): You can add a step to run UI tests using Espresso, similar to how you run unit tests.

    - name: Run UI tests
      run: ./gradlew connectedDebugAndroidTest
    
  • Deploy to Firebase or Play Store: After building the app and running tests, you can deploy it directly from GitHub Actions using Firebase App Distribution or Google Play's API.

  • Code Quality Checks: Before building or deploying, you can integrate tools like Lint, Checkstyle, or Detekt to perform code quality checks.

  • Publish Artifact: After a successful build, you can add steps to upload APKs to GitHub Releases or other distribution platforms.

3. Example: Publishing to Firebase App Distribution

To distribute your app to Firebase App Distribution automatically after a successful build, you can add the following steps to your workflow:

      - name: Upload APK to Firebase App Distribution
        uses: wzieba/Firebase-Distribution-Github-Action@v1
        with:
          appId: ${{secrets.FIREBASE_APP_ID}}  # Set this as a secret in GitHub
          token: ${{secrets.FIREBASE_AUTH_TOKEN}}  # Set this as a secret in GitHub
          groups: testers  # Specify the group of testers
          file: app/build/outputs/apk/release/app-release.apk  # Path to your APK

4. Setting Secrets in GitHub

For sensitive data like Firebase credentials, make sure to set them up as secrets in GitHub:

  • Navigate to your repository on GitHub.
  • Go to Settings > Secrets > New repository secret.
  • Add secrets like FIREBASE_APP_ID and FIREBASE_AUTH_TOKEN.

5. Run the Workflow

Once the GitHub Actions workflow file is added, it will run automatically when you push changes to the main branch or create a pull request targeting the main branch.

You can monitor the progress and see the results in the Actions tab of your GitHub repository.


This approach will automate your build, test, and deployment processes, making it easier to maintain high-quality Android applications. It also helps with faster feedback on code changes, especially in larger teams.

Reference: https://docs.github.com/en/actions, https://github.com/actionshttps://docs.github.com/en/actions/about-github-actions/understanding-github-actions,  https://github.com/marketplace/actions/automated-build-android-app-with-github-actionhttps://github.com/marketplace/actions/upload-android-release-to-play-store