gradle: apply plugin only for specific flavor gradle: apply plugin only for specific flavor android android

gradle: apply plugin only for specific flavor


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


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'


Simply apply the dependency in specific build flavour definition in build.gradle

android{      productFlavors {         flavourName{         apply plugin: 'com.google.gms.google-services'        }       }     }