在 FCM 中为不同的风格配置多个 google-services.Json

configure mulitple google-services.Json for different flavours in FCM

我正在尝试根据 android 项目中的不同风格实施多个 google-service.json 文件。 我曾尝试使用以下代码:https://medium.com/google-cloud/automatic-per-variant-google-services-json-configurations-with-gradle-d3d3e40abc0e

下面是我同时拥有 google-service.json 文件-

的路径
app/flavourA/google-services.json
app/flavourB/google-services.json

在 app/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

    android {
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    flavorDimensions"default"

        productFlavors {
            flavourA {
                buildConfigField("String", "flavored_app_name", "\"flavourA\"")

                buildConfigField("String", "ApplicationId", "\"com.companyname.flavourA\"")
                applicationId 'com.companyname.flavourA'
                dimension "default"
                testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
                versionCode 68
                versionName '3.1.7'
            }
            flavourB{
                versionCode 14
                buildConfigField("String", "flavored_app_name", "\"flavourB\"")

                buildConfigField("String", "ApplicationId", "\"com.companyname.flavourB\"")
                applicationId 'com.companyname.flavourB'
                dimension "default"

                testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
                versionName '1.7.0'
            }
        }

    }


    def appModuleRootFolder = '.'
    def srcDir = 'src'
    def googleServicesJson = 'google-services.json'

    task switchToDebug(type: Copy) {
        def buildType = 'debug'
        description = 'Switches to DEBUG google-services.json'
        from "${srcDir}/${buildType}"
        include "$googleServicesJson"
        into "$appModuleRootFolder"
    }

    task switchToRelease(type: Copy) {
        def buildType = 'release'
        description = 'Switches to RELEASE google-services.json'
        from "${srcDir}/${buildType}/"
        include "$googleServicesJson"
        into "$appModuleRootFolder"
    }

    afterEvaluate {
        flavourA.dependsOn switchToDebug
        flavourB.dependsOn switchToRelease
    }

错误

ERROR: Could not get unknown property 'flavourA' for project ':app' of type org.gradle.api.Project.

您指的是旧文章。不久前 Firebase 更改了支持不同风格的实现。现在只有一个由 Firebase 生成的 google-services.json 文件,其中包含对所有口味的引用。

您必须在 Firebase 控制台的同一个项目中添加多个应用程序,然后下载更新的 google-service.json 并放入根文件夹。

请遵循 Firebase 提供的最新指南。阅读他们关于如何添加不同变体的说明:
Adding apps to a Firebase project
Add Firebase to your Android project