25

My Android gradle currently set a flavor dimension for using different push service. (One for baidu push and one for GCM) I would like to have my Android app only to import google-services for a GCM push build flavor. Is it possible to do it?

P.S. Because in order to use GCM in Android, I have to add the apply plugin: 'com.google.gms.google-services' line be at the bottom of my app/build.gradle file as detailed here.

As a workaround in order to let the baidu flavor to be built successfully, I may need to place a dummy google-services.json for baidu.

Update: I seem to find the answer in this long github issue thread.

chub
  • 5,131
  • 4
  • 35
  • 59
  • 1
    In your update, include a link to the post that solves the issue – drhr Nov 04 '16 at 22:24
  • can't you just apply the plugin for a specific build variant with `android.applicationVariants.all{ it.variantData.getVariantConfiguration().getFullName().capitalize(); //if here}` – Leonard Arnold Feb 14 '17 at 11:00
  • 2
    Possible duplicate of [How to apply plugin to only one flavor in gradle?](http://stackoverflow.com/questions/31379795/how-to-apply-plugin-to-only-one-flavor-in-gradle) – Simon Featherstone Mar 09 '17 at 11:34
  • What is your solution? I am facing the same problem and the link https://github.com/googlesamples/google-services/issues/54 doesn't seem to help. I have multiple flavor and similar to your situation, some of them are for China and hence doesn't require Firebase. I am looking for ways to not run apply plugin: 'com.google.gms.google-services' for specific variants. – Noel Chew May 17 '17 at 06:51

2 Answers2

16

In your build.gradle(App)file add this code:

if (getGradle().getStartParameter().getTaskRequests()
        .toString().contains("YourGCMFlavorName")){
    apply plugin: 'com.google.gms.google-services'
}

N.B.: First letter of flavor name must be in Uppercase

Anisuzzaman Babla
  • 4,563
  • 3
  • 25
  • 50
5

I had this very same problem - I have a project that builds multiple apps, each app has two variants one distributed via Google Play and uses Google Play Services amd Firebase APIs, the other variant is distributed via Web download (mainly for AOSP devices) and cannot include either Google Play Services or Firebase APIs.

In our app/build.gradle file we didn't want any funky condition tests that didn't seem totally obvious, by that we meant "if variant == web then do not apply plug google play services". We also didn't want multiple copies of the file google-services.json, i.e. one per app, we wanted one that contained all the app bundles enabled for Google Play Services. This is because we add and remove apps quite regularly and want to manage those apps as one project in the Firebase console.

The solution was to create a distribution dimension, this dimension must be placed first in the flavorDimensions array (the com.google.gms.google-services plugin only looks in the first dimension for google-services.json).

    flavorDimensions 'distribution', 'application'
    productFlavors {
        store {
            dimension 'distribution'
        }
        web {
            dimension 'distribution'
            applicationIdSuffix ".nogms"
        }
        app1 {
            dimension 'application'
            applicationId 'com.example.app1'
        }
        app2 {
            dimension 'application'
            applicationId 'com.example.app2'
        }

The distribution dimension had two values - "store" and "web"

The google-services.json generated by the Firebase console was placed in the directory app/src/store.

In the app/src/web directory we placed a dummy google-services.json file with the following contents:

{
  "project_info": {
    "project_number": "0",
    "project_id": "api-project-0"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:0:android:0",
        "android_client_info": {
          "package_name": "com.example.app1.nogms"
        }
      },
      "api_key": [
        {
          "current_key": "none"
        }
      ]
    },
    {
      "client_info": {
        "mobilesdk_app_id": "1:0:android:0",
        "android_client_info": {
          "package_name": "com.example.app2.nogms"
        }
      },
      "api_key": [
        {
          "current_key": "none"
        }
      ]
    }
  ]
}

This keeps the plugin happy. (Non GMS app bundles need to be added to the "client":[] array as needed).

The GMS and Firebase libraries were conditionally included into the store flavors only:

dependencies {
    storeImplementation 'com.google.firebase:firebase-core:16.0.8'
    storeImplementation 'com.google.firebase:firebase-iid:17.1.2'
    storeImplementation 'com.google.firebase:firebase-messaging:17.6.0'
}

And finally the Google Play Services plugin was applied globally at the end of the build.gradle as instructed in https://firebase.google.com/docs/android/setup

apply plugin: 'com.google.gms.google-services'
BitByteDog
  • 2,080
  • 2
  • 21
  • 32
  • Thank you! Seems like you can't really keep the plugin from applying so using a dummy google-services.json seems to be the way to go. – dreyln Aug 15 '19 at 23:39