androidandroid-gradle-pluginandroid-build-flavorsandroid-flavordimension

gradle: apply plugin only for specific flavor


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.


Solution

  • 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.