有条件地应用 GMS google-services 插件

Conditionally apply GMS google-services plugin

多年来,我一直非常小心,不将 Google-Android 依赖项引入我的 Android 应用程序,并且有两种风格保持这种隔离,withAmazonwithGoogle.

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

最近,我一直在试验 Firebase Analytics,这非常 Google OHA only product and I do not believe it is meant to work outside of the Google ecosystem (although there are some reports 确实如此)。 firebase 的部分设置是应用 google-services 插件。

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

但是,withAmazon 风味不包括 google-service.json ...因为...嗯,它不适用于 Google。

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

...但是由于 是否可以只将 gms 插件应用于其中一种口味?在编译 Amazon 风格时,在上述场景中构建失败,因为它抱怨 google-services.json 不存在...这是正确的。

现在,我的解决方法是将 google-services.json 移动到 app/ 目录下。问题解决了。然而,我在 Google 和 'pure' Android OS 之间非常明确的界限被侵犯了,这感觉不对。

感谢您提供的任何帮助。

我通过像这样修改父 build.gradle 文件解决了这个问题;

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

现在,插件(以及关联的 Google Play 服务依赖项)未添加到我的应用程序的 Amazon 风格中。此外,google-services.json 现在回归 "withGoogle" 风格。

注意:withGoogle 是在别处定义的值为 "withGoogle" 的定义。 致谢: