gradle: apply plugin only for specific flavor
Asked Answered
E

3

30

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.

Edie answered 23/9, 2016 at 2:34 Comment(4)
In your update, include a link to the post that solves the issueSedge
can't you just apply the plugin for a specific build variant with android.applicationVariants.all{ it.variantData.getVariantConfiguration().getFullName().capitalize(); //if here}Antevert
Possible duplicate of How to apply plugin to only one flavor in gradle?Archetype
What is your solution? I am facing the same problem and the link 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.Morie
G
20

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

Update 2021-11-23: Please note the solution by Prasoon Abhishek below.

Grasmere answered 9/7, 2018 at 11:51 Comment(4)
I believe the negation is a mistake (?) - I mean if you're looking for the GCM flavor you then apply the GMS plugin, don't you?Theophylline
Important. The first letter MUST be in upper case as stated here https://mcmap.net/q/267854/-how-to-apply-plugin-to-only-one-flavor-in-gradleNickerson
Thank you Augusto Carmo. I have added this.Grasmere
The solution by Prasoon does not work for me (with another plugin). The check on the Gradle task name does work as intended.Palladous
B
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'
Botts answered 21/6, 2019 at 0:27 Comment(1)
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.Problematic
E
2

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

android{
      productFlavors {

         flavourName{
         apply plugin: 'com.google.gms.google-services'
        }
       }
     }
Extrasystole answered 30/9, 2021 at 7:10 Comment(2)
This should be the accepted answerMeek
This doesn't work. With this solution the plugin is applied in other flavors as wellAlodium

© 2022 - 2024 — McMap. All rights reserved.