androidfirebase-cloud-messagingandroid-flavors

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


I am trying to implement multiple google-service.json file as per different flavors in android project. I had tried using code from : https://medium.com/google-cloud/automatic-per-variant-google-services-json-configurations-with-gradle-d3d3e40abc0e

Below is the path where i have both google-service.json file-

app/flavourA/google-services.json
app/flavourB/google-services.json

in 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:

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

Solution

  • The article you are referring to is old. A little while back Firebase changed the implementation of supporting different flavors. There is now only one google-services.json file generated by Firebase which holds a reference to all the flavors.

    You have to add multiple apps in the same Project in the Firebase console, and download the updated google-service.json and put in the root folder.

    Follow the latest guide provided by Firebase. Read their instructions on how to add different variants:
    Adding apps to a Firebase project
    Add Firebase to your Android project