更新:GCM已弃用,使用FCM

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

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

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


当前回答

我也遇到了同样的问题,但找不到完美的解决方案。这只是个变通办法。 我在想谷歌怎么就没想过口味呢?我希望他们能尽快提出更好的解决方案。

我在做什么:

我有两种风格,在每一种风格中我都放入了相应的google服务。Json: src/flavor1/google-services。Json和src/flavor2/google-services。json。

然后在构建gradle中,我根据应用程序/目录的口味复制文件:

android {

// set build flavor here to get the right gcm configuration.
//def myFlavor = "flavor1"
def myFlavor = "flavor2"

if (myFlavor.equals("flavor1")) {
    println "--> flavor1 copy!"
    copy {
        from 'src/flavor1/'
        include '*.json'
        into '.'
    }
} else {
    println "--> flavor2 copy!"
    copy {
        from 'src/flavor2/'
        include '*.json'
        into '.'
    }
}

// other stuff
}

限制:每次你想运行一个不同的口味时,你必须在gradle中手动更改myFlavor(因为它是硬编码的)。

我尝试了很多方法来获得当前的构建风格,比如afterEvaluate关闭…直到现在都找不到更好的解决办法了。

更新,另一个解决方案:一个谷歌服务。Json的所有口味:

你也可以,为每个口味有不同的包名,然后在谷歌开发控制台,你不需要为每个口味创建两个不同的应用程序,只需要在同一个应用程序中创建两个不同的客户端。 那么你就只有一个google服务了。Json,包含你的两个客户端。 当然,这取决于你如何实现你的口味的后端。如果它们没有分开,那么这个解决方案就不能帮助你。

其他回答

简化@Scotti说的话。您需要为特定项目创建多个具有不同包名的应用程序,具体取决于产品风格。

假设您的项目是ABC,具有不同的产品风味X,Y,其中X有一个包名com。x和Y有一个包名为com。然后在firebase控制台中,你需要创建一个项目ABC,其中你需要创建2个包名为com的应用程序。X和com.y。然后你需要下载谷歌服务。Json文件中有2个client-info对象,其中包含这些包,你就可以开始了。

json的代码片段如下所示

{
  "client": [
    {
      "client_info": {
        "android_client_info": {
          "package_name": "com.x"
        }

    {
      "client_info": {
        "android_client_info": {
          "package_name": "com.y"
        }
      ]

    }

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

有一个类似的问题(使用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级别上修改它。

我发现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

受到上面@ahmed_khan_89答案的启发。我们可以直接这样保存在gradle文件中。

android{

// set build flavor here to get the right Google-services configuration(Google Analytics).
    def currentFlavor = "free" //This should match with Build Variant selection. free/paidFull/paidBasic

    println "--> $currentFlavor copy!"
    copy {
        from "src/$currentFlavor/"
        include 'google-services.json'
        into '.'
    }
//other stuff
}

我知道,你怀疑谷歌的服务。Json文件应该放在根应用程序文件夹,是吗?我将打破这个神话——不一定。你可以写谷歌服务。Json文件到flavor文件夹中。是这样的: