更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
当前回答
写了一篇关于这个问题的文章。
有一个类似的问题(使用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级别上修改它。
其他回答
事实上,只有一个谷歌——服务。MyApp/app/目录下的com.google.gms:google-services:3.0.0是很好的,不需要额外的脚本。但是要小心删除文件google-services。为了避免错误类型为“:app:processDebugGoogleServices”的任务执行失败,请从应用目录MyApp/app/src/flavor1/res/”中删除。没有为包找到匹配的客户端
我也遇到了同样的问题,但找不到完美的解决方案。这只是个变通办法。 我在想谷歌怎么就没想过口味呢?我希望他们能尽快提出更好的解决方案。
我在做什么:
我有两种风格,在每一种风格中我都放入了相应的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,包含你的两个客户端。 当然,这取决于你如何实现你的口味的后端。如果它们没有分开,那么这个解决方案就不能帮助你。
不需要任何额外的gradle脚本。
谷歌开始在'android_client_info'的名称中添加不同的包名。它在google-services.json中如下所示
"android_client_info": {
"package_name": "com.android.app.companion.dev"
}
因此,以下步骤足以拥有不同的谷歌服务。json的选择。
有两种口味 在谷歌分析配置页面添加一个新的开发风格的包,并下载google-services.json。 注意在新的配置文件中,两个flavor的包id都在那里 准备你的调味料。
就是这样!
根据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 '.'
}
}
}
这样你就不必手动切换设置了。
写了一篇关于这个问题的文章。
有一个类似的问题(使用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级别上修改它。