0

我正在尝试根据 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.
4

1 回答 1

1

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

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

遵循 Firebase 提供的最新指南。阅读他们关于如何添加不同变体的说明:
将应用程序添加到 Firebase 项目
将 Firebase 添加到您的 Android 项目

于 2020-03-18T03:04:32.190 回答