firebase / firebase-android-sdk

Firebase Android SDK
https://firebase.google.com
Apache License 2.0
2.25k stars 570 forks source link

App Distribution Plugin configuration does not respect build types #5694

Open safu9 opened 6 months ago

safu9 commented 6 months ago

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

My application uses different projects and service credential files for each build type, like this

// build.gradle.kts
buildTypes {
    getByName("release") {
        // ...
        firebaseAppDistribution {
            artifactType = "APK"
            groups = "testers"
            serviceCredentialsFile = "$rootDir/secrets/app-distribution-release-key.json"
        }
    }
    getByName("debug") {
        // ...
        firebaseAppDistribution {
            artifactType = "APK"
            groups = "testers"
            serviceCredentialsFile = "$rootDir/secrets/app-distribution-debug-key.json"
        }
    }
}

When I try to distribute with the relase config, the app distribution plugin uses the dubeg credential instead of the release one.

$ ./gradlew assembleRelease appDistributionUploadRelease

> Task :app:appDistributionUploadRelease
Using APK path in the outputs directory: ...\app\build\outputs\apk\release\app-release.apk.
Uploading APK to Firebase App Distribution...
Using service credentials file specified by the serviceCredentialsFile property in your app's build.gradle file: ...\secrets\app-distribution-debug-key.json
Uploading the APK.

> Task :app:appDistributionUploadRelease FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:appDistributionUploadRelease'.
> App Distribution halted because it had a problem uploading the APK: [403] The caller does not have permission

After I found this https://github.com/firebase/firebase-android-sdk/issues/5651#issuecomment-1908246234 , I tried both adding import com.google.firebase.appdistribution.gradle.firebaseAppDistribution and using configure<com.google.firebase.appdistribution.gradle.AppDistributionExtension> instead of firebaseAppDistribution. The latter worked, but the former did not.

When I went to the declaration of firebaseAppDistribution, Android Studio showed me that the extension function for org.gradle.api.Project in this code was used in my gradle script.

// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available

package com.google.firebase.appdistribution.gradle

public fun com.android.build.api.dsl.ProductFlavor.firebaseAppDistribution(action: com.google.firebase.appdistribution.gradle.AppDistributionExtension.() -> kotlin.Unit): kotlin.Unit { /* compiled code */ }

public fun org.gradle.api.Project.firebaseAppDistribution(action: com.google.firebase.appdistribution.gradle.AppDistributionExtension.() -> kotlin.Unit): kotlin.Unit { /* compiled code */ }

public fun org.gradle.nativeplatform.BuildType.firebaseAppDistribution(action: com.google.firebase.appdistribution.gradle.AppDistributionExtension.() -> kotlin.Unit): kotlin.Unit { /* compiled code */ }

I think we need an extension function for com.android.build.api.dsl.ApplicationBuildType, which is not a subclass of org.gradle.nativeplatform.BuildType.

lehcar09 commented 6 months ago

Hi @safu9, thank you for reaching out and reporting the issue. I tried reproducing the issue and I was able to replicate the issue in "firebaseAppdistribution" key when configured in "buildTypes". I’ll notify our engineers and see what we can do here.

rizwan123912 commented 6 months ago

@safu9

MaesterChestnut commented 6 months ago

Thanks for reporting this issue; we're looking into what's causing this ignoring of application variants. Can you try replacing firebaseAppDistribution with configure<AppDistributionExtension> in both your release and debug buildTypes?

safu9 commented 6 months ago

Yes, replacing firebaseAppDistribution with configure<AppDistributionExtension> in both buildTypes resolves this problem. Gradle uses the correct serviceCredentialsFile for each buildType when using configure<AppDistributionExtension>.

ajithsrepo commented 3 months ago

@MaesterChestnut @lehcar09 Using configure<AppDistributionExtension> doesn't resolve this issue when I use flavors. Uploads fail because the appDistributionUpload task will always use the last resolved json file. In my case that'd be firebase_app_distribution_key_qa.json. For example, if I run assembleTestUATRelease appDistributionUploadTestUATRelease, it'd throw a 403 Forbidden error because the task uses firebase_app_distribution_key_qa.json to upload the UAT app.

androidComponents {
    val firebaseAppDistributionConfig = mapOf(
        "TestPROD" to FirebaseConfig(
            serviceCredentialsFile = "./vault/firebase_app_distribution_key_prod.json",
            releaseNotes = "Release notes for PROD",
            groups = "dev"
        ),
        "TestUAT" to FirebaseConfig(
            serviceCredentialsFile = "./vault/firebase_app_distribution_key_uat.json",
            releaseNotes = "Release notes for UAT",
            groups = "dev"
        ),
        "TestQA" to FirebaseConfig(
            serviceCredentialsFile = "./vault/firebase_app_distribution_key_qa.json",
            releaseNotes = "Release notes for QA",
            groups = "dev"
        )
    )

    onVariants { variant ->
        val variantName = variant.flavorName
        val firebaseConfig = firebaseAppDistributionConfig[variantName]

        firebaseConfig?.let {
            configure<com.google.firebase.appdistribution.gradle.AppDistributionExtension> {
                serviceCredentialsFile = it.serviceCredentialsFile
                releaseNotes = it.releaseNotes
                groups = it.groups
            }
        }
    }
}