更新:GCM已弃用,使用FCM

我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南

我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。

有没有办法让谷歌的服务。Json配置不同的许多口味?


当前回答

我发现google-services插件对于想要添加GCM的项目来说是非常无用的。它只生成以下文件,简单地将您的项目ID添加为字符串资源:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Your API key would be on the following line -->
    <string name="gcm_defaultSenderId">111111111111</string>
</resources>

似乎只有在直接从Cloud Messaging for Android指南复制样例代码时才需要它。下面是示例行:

String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),              GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

解决方案

如果您希望能够为不同的构建类型或产品风格切换API项目,您只需定义自己的常量,并在调用getToken() API时选择适当的常量。

private static final String SENDER_ID = "111111111111";
private static final String SANDBOX_SENDER_ID = "222222222222";

String token = instanceID.getToken(
        BuildConfig.DEBUG ? SENDER_ID : SANDBOX_SENDER_ID,
        GoogleCloudMessaging.INSTANCE_ID_SCOPE,
        null);

产品口味

上面的代码用于在调试和发布版本之间切换。对于产品类型,您可以在java源文件中定义不同的API键,并将这些文件放在相应的产品类型目录中。参考:Gradle Build variables

其他回答

Google -services插件的目的是简化谷歌特性的集成。

因为它只从google服务中生成android资源。json文件,过于复杂的gradle逻辑否定了这一点,我认为。

因此,如果google文档没有说明特定的google特性需要哪些资源,我建议为每个相关的buildtype/flavor生成json文件,看看插件生成了哪些资源,然后手动将这些资源放入各自的src/buildtypeORflavor/res目录中。

删除对google-services插件和json文件的引用,就完成了。

有关google-services gradle-plugin内部工作原理的详细信息,请参阅我的另一个答案:

https://stackoverflow.com/a/33083898/433421

写了一篇关于这个问题的文章。

有一个类似的问题(使用BuildTypes而不是flavour),并像这样修复了它。

利用Gradle的依赖管理系统。我创建了两个任务switchToDebug和switchToRelease。要求在运行assemblerrelease时,也运行switchToRelease。调试也是一样。

def appModuleRootFolder = '.'
def srcDir = 'src'
def googleServicesJson = 'google-services.json'

task switchToDebug(type: Copy) {
    def buildType = 'debug'
    description = 'Switches to DEBUG google-services.json'
    from "${srcDir}/${buildType}"
    include "$googleServicesJson"
    into "$appModuleRootFolder"
}

task switchToRelease(type: Copy) {
    def buildType = 'release'
    description = 'Switches to RELEASE google-services.json'
    from "${srcDir}/${buildType}/"
    include "$googleServicesJson"
    into "$appModuleRootFolder"
}

afterEvaluate {
    processDebugGoogleServices.dependsOn switchToDebug
    processReleaseGoogleServices.dependsOn switchToRelease
}

编辑:使用processDebugFlavorGoogleServices/processReleaseFlavorGoogleServices任务在每个flavor级别上修改它。

我正在使用谷歌服务。json文件,从这里创建:https://developers.google.com/mobile/add?platform=android&cntapi=gcm&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fandroid%2Fclient&cntlbl=Continue%20Adding%20GCM%20Support&%3Fconfigured%3Dtrue

在json结构中,有一个称为客户机的json数组。如果你有多种口味,只需在这里添加不同的属性。

{
  "project_info": {
    "project_id": "PRODJECT-ID",
    "project_number": "PROJECT-NUMBER",
    "name": "APPLICATION-NAME"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:PROJECT-NUMBER:android:HASH-FOR-FLAVOR1",
        "client_id": "android:PACKAGE-NAME-1",
        "client_type": 1,
        "android_client_info": {
          "package_name": "PACKAGE-NAME-1"
        }
      },
      "oauth_client": [],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 2,
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 1
        },
        "ads_service": {
          "status": 1
        }
      }
    },
    {
      "client_info": {
        "mobilesdk_app_id": "1:PROJECT-NUMBER:android:HASH-FOR-FLAVOR2",
        "client_id": "android:PACKAGE-NAME-2",
        "client_type": 1,
        "android_client_info": {
          "package_name": "PACKAGE-NAME-2"
        }
      },
      "oauth_client": [],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 2,
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 1
        },
        "ads_service": {
          "status": 1
        }
      }
    }
  ],
  "client_info": [],
  "ARTIFACT_VERSION": "1"
}

在我的项目中,我使用相同的项目id,当我在上面的url中添加第二个package-name时,谷歌为我提供了一个包含json-data中的多个客户端的文件。

事实上,只有一个谷歌——服务。MyApp/app/目录下的com.google.gms:google-services:3.0.0是很好的,不需要额外的脚本。但是要小心删除文件google-services。为了避免错误类型为“:app:processDebugGoogleServices”的任务执行失败,请从应用目录MyApp/app/src/flavor1/res/”中删除。没有为包找到匹配的客户端

不需要任何额外的gradle脚本。

谷歌开始在'android_client_info'的名称中添加不同的包名。它在google-services.json中如下所示

"android_client_info": {
      "package_name": "com.android.app.companion.dev"
    }

因此,以下步骤足以拥有不同的谷歌服务。json的选择。

有两种口味 在谷歌分析配置页面添加一个新的开发风格的包,并下载google-services.json。 注意在新的配置文件中,两个flavor的包id都在那里 准备你的调味料。

就是这样!