更新:GCM已弃用,使用FCM

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

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

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


当前回答

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

其他回答

根据@ZakTaccardi的回答,并且假设您不想在两个版本中都使用一个项目,请将此添加到构建的末尾。gradle文件:

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

task switchToStaging(type: Copy) {
    outputs.upToDateWhen { false }
    def flavor = 'staging'
    description = "Switches to $flavor $googleServicesJson"
    delete "$appModuleRootFolder/$googleServicesJson"
    from "${srcDir}/$flavor/"
    include "$googleServicesJson"
    into "$appModuleRootFolder"
}

task switchToProduction(type: Copy) {
    outputs.upToDateWhen { false }
    def flavor = 'production'
    description = "Switches to $flavor $googleServicesJson"
    from "${srcDir}/$flavor/"
    include "$googleServicesJson"
    into "$appModuleRootFolder"
}

afterEvaluate {
    processStagingDebugGoogleServices.dependsOn switchToStaging
    processStagingReleaseGoogleServices.dependsOn switchToStaging
    processProductionDebugGoogleServices.dependsOn switchToProduction
    processProductionReleaseGoogleServices.dependsOn switchToProduction
}

你需要src/staging/google-services文件。Json和src/production/google-services.json。替换您所使用的风味名称。

嘿,朋友也寻找名称只使用小写,那么你不会得到这个错误

根据ahmed_khan_89的回答,您可以将“复制代码”放在产品风味中。

productFlavors {
    staging {
        applicationId = "com.demo.staging"

        println "Using Staging google-service.json"
        copy {
            from 'src/staging/'
            include '*.json'
            into '.'
        }
    }
    production {
        applicationId = "com.demo.production"

        println "Using Production google-service.json"
        copy {
            from 'src/production/'
            include '*.json'
            into '.'
        }
    }
}

这样你就不必手动切换设置了。

Firebase现在支持多个应用程序id和一个google-services。json文件。

这篇博文详细描述了它。

您将在Firebase中创建一个父项目,用于您的所有变体。然后在Firebase项目下为每个应用程序id创建单独的Android应用程序。

当你创建了所有的变体,你可以下载一个google-services。支持所有应用程序id的Json。当需要单独查看数据时(例如崩溃报告),你可以用下拉菜单进行切换。

简短的回答:

实现: 默认情况下,您应该复制google-services。Json到app dir。

如果想要获得其他效果,可以复制谷歌的服务。Json到app/src/{flavor-name}目录

测试: 尝试构建,打开构建选项卡,然后使用解析json文件检查输出消息:.....