iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔥

Deploying Android apps to Firebase App Distribution with codemagic.yaml

に公開

Introduction

  • This is a memo documenting the process of publishing an Android app to Firebase App Distribution using a Codemagic yaml file.
  • General flow
    • Prerequisites
    • Build verification and trigger settings on Codemagic
    • Publishing based on service account authentication

Reference Articles

Continuous Integration & Delivery (CI/CD) for Android | Codemagic Blog

I will proceed by referring to the article above.
Also, I am proceeding on the assumption that the target Android project has already been added to a Codemagic App.

Environment Setup and Preparation

1. Prerequisites

1-1. Setting up Testers and Groups in Firebase App Distribution

image1.png

In this case, I will create a group called "Developer" under "Testers & Groups" so that notifications are sent when there is a new release.

*Note: If it's your first time, clicking "Get Started" should display the screen shown above.

1-2. buildTypes and productFlavors

I am assuming the following buildTypes and productFlavors configuration for this deployment.

// app/build.gradle.kts
android {
    // ...
    flavorDimensions += "default"
    productFlavors {
        create("dev") {
            signingConfig = signingConfigs.getByName("dev") // signingConfig already specified
            isDefault = true
            applicationIdSuffix = ".dev"
            dimension = "default"
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
}

CI Build

1. A Simple Workflow for Testing and Building

First, create a simple workflow. Create a codemagic.yaml file in the project root with the following content.

workflows:
  build-dev-android-workflow:
    name: Build Dev Android Workflow
    max_build_duration: 60
    instance_type: mac_mini_m1
    environment:
      groups:
        - setup # If necessary for changing JAVA_HOME (see troubleshooting for details)
    scripts:
      - name: Run tests
        script: ./gradlew test
      - name: Build devRelease
        script: ./gradlew assembleDevRelease
    artifacts:
      - app/build/outputs/apk/**/*.apk

Once you have committed it, push it immediately.

1-1. Manual Build

After pushing, check the Codemagic dashboard, and you should be able to "Start your first build" for the target project.

image2.png

Configure the red-framed settings as needed and click "Start new build" to begin the build.

image3.png

If the build is successful, you're good to go!

1-2. Enabling Builds on Push to a Specific Branch

https://docs.codemagic.io/yaml-running-builds/starting-builds-automatically/

Referring to the link above, add the following. In this case, the build will run when a push is made to the develop branch.


triggering:
  events:
    - push
  branch_patterns:
    - pattern: "develop"
      include: true
      source: true

Publishing to Firebase App Distribution

Authentication via Service Account

From the Firebase dashboard, click the gear icon > Service accounts > N service accounts.

image4.png

Once the GCP screen opens, select "Service accounts" from the left menu and click "Create service account" from the top menu. Enter the service account details and click "Create and continue".

image5.png

Next, for "Select a role", choose "Firebase App Distribution Admin SDK Service Agent".

image6.png

Finally, click "Done" without entering anything else.

image7.png

Next, click "Manage keys" from the menu of the created service account.

image8.png

Go to "Add key" > "Create new key", select "JSON" as the key type, and click "Create". The downloaded JSON file will be used later, so manage it in an appropriate location.

image9.png

Codemagic Environment Variable Settings

Copy the content of the JSON file you just downloaded to your clipboard.

cat xxxxxxxxx.json | pbcopy

In the "Environment variables" tab of the target app settings in Codemagic, add a new environment variable called FIREBASE_SERVICE_ACCOUNT.

image10.png

Paste the content you copied to the clipboard into the value field.

Finally, add the following to codemagic.yaml and push it.

publishing:
  firebase:
    firebase_service_account: $FIREBASE_SERVICE_ACCOUNT
    android:
      app_id: $ANDROID_APP_ID
      groups:
        - Developer

If the app is released in Firebase App Distribution as follows, it's a success!

image11.png

Troubleshooting

Error: Android Gradle plugin requires Java 17 to run. You are currently using Java 11. occurs!

Error details

* What went wrong:
An exception occurred applying plugin request [id: 'com.android.application', version: '8.1.1']
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
      Your current JDK is located in /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
      You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

Solution

The location of JAVA_HOME for Java 17 is listed here.

image12.png

Referring to the above, select "Environment variables" from the target app's settings screen in Codemagic and add the following:

Name: JAVA_HOME
Value: /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
Variable group: setup (change the group setting as necessary)

image13.png

The following error occurs during publishing

Authenticating with `--token` is deprecated and will be removed in a future major version of `firebase-tools`. Instead, use a service account key with `GOOGLE_APPLICATION_CREDENTIALS`:

Currently, firebase_token cannot be used, so you need to use firebase_service_account instead.

This article was written with reference to the following information

Discussion