gradle:仅针对特定风味应用插件

Dav*_*ung 19 android android-gradle-plugin android-build-flavors android-flavordimension

我的Android gradle目前为使用不同的推送服务设置了风味维度.(一个用于baidu推送,一个用于GCM)我想让我的Android应用程序仅导入google-servicesGCM推送构建风格.有可能吗?

PS因为为了在Android中使用GCM,我要补充的apply plugin: 'com.google.gms.google-services'线在我的应用程序/文件的build.gradle的底部详见这里.

作为一个解决方案,为了让百度味道成功建立,我可能需要google-services.json为百度放置一个假人.

更新: 我似乎在这个长github问题线程中找到答案.

Ani*_*bla 7

在您的build.gradle(App)文件中添加以下代码:

if (!getGradle().getStartParameter().getTaskRequests()
        .toString().contains("YOUR_GCM_FLAVOR_NAME")){
    apply plugin: 'com.google.gms.google-services'
}
Run Code Online (Sandbox Code Playgroud)

  • 我相信这种否定是一个错误(?)-我的意思是,如果您正在寻找GCM风格,则可以应用GMS插件,不是吗? (2认同)

Bit*_*Dog 6

我遇到了同样的问题 - 我有一个构建多个应用程序的项目,每个应用程序都有两个变体,一个通过 Google Play 分发并使用 Google Play Services amd Firebase API,另一个变体通过网络下载分发(主要用于 AOSP 设备)和不能包含 Google Play 服务或 Firebase API。

在我们的 app/build.gradle 文件中,我们不想要任何看起来并不完全明显的时髦条件测试,我们的意思是“如果变体 == 网络则不应用即插即用的 google play 服务”。我们也不想要 google-services.json 文件的多个副本,即每个应用一个,我们想要一个包含为 Google Play 服务启用的所有应用程序包的副本。这是因为我们经常添加和删除应用程序,并且希望在 Firebase 控制台中将这些应用程序作为一个项目进行管理。

解决方案是创建一个分布维度,这个维度必须首先放在 flavorDimensions 数组中(com.google.gms.google-services 插件只在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'
        }

Run Code Online (Sandbox Code Playgroud)

分配方面有两个值- “存储”和“网络”

Firebase 控制台生成的google-services.json放在目录app/src/store 中

app/src/web目录中,我们放置了一个包含以下内容的虚拟 google-services.json 文件:

{
  "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"
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这让插件开心。(非 GMS 应用程序包需要根据需要添加到 "client":[] 数组中)。

GMS 和 Firebase 库仅有条件地包含在商店风格中:

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'
}
Run Code Online (Sandbox Code Playgroud)

最后,按照https://firebase.google.com/docs/android/setup 中的说明,在 build.gradle 的末尾全局应用了 Google Play 服务插件

apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)