Search code examples
androidkindle-firegoogle-play-services

Conditionally apply GMS google-services plugin


I have been very careful not to introduce Google-Android dependencies into my Android application over the years and have two flavors keeping that segregation, withAmazon and withGoogle.

app
 |_ main
 |_ test
 |_ withAmazon
 |_ withGoogle

Recently, I've been experimenting with Firebase Analytics which is very much a Google OHA only product and I do not believe it is meant to work outside of the Google ecosystem (although there are some reports it does). Part of the set-up for firebase is applying the google-services plugin.

 apply plugin: 'com.google.gms.google-services'

However, the withAmazon flavour does not include a google-service.json ...because...well, it's not for Google.

app
 |_ main
 |_ test
 |_ withAmazon
    |- java
    |- AndroidManifest.xml
 |_ withGoogle
    |- java
    |- AndroidManifest.xml
    |- google-services.json

...but since Gradle plugins appear to need to have to be applied project wide is it possible to only apply the gms plugin to one of the flavours? The build fails in the scenario described above when compiling the Amazon flavor since it complains about the google-services.json not existing...which is correct.

Right now, my workaround is to move the google-services.json under the app/ directory. Problem solved. However, it doesn't feel right that my very clear divide between Google & the 'pure' Android OS is being encroached upon.

Any assistance graciously received.


Solution

  • I solved this issue by modifying my parent build.gradle file like so;

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

    Now the plugin (and associated Google Play Services dependencies) are not added to the Amazon flavour of my app. Moreover, the google-services.json now lives back under the "withGoogle" flavor.

    Note: withGoogle is a def defined elsewhere with value "withGoogle". Acknowledgement: https://stackoverflow.com/a/43336869/1348379