更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
当前回答
放置你的“谷歌服务”。Json”文件分别在app/src/flavors下 然后是构建。Gradle的应用程序,在android下添加以下代码
gradle.taskGraph.beforeTask { Task task ->
if (task.name ==~ /process.*GoogleServices/) {
android.applicationVariants.all { variant ->
if (task.name ==~ /(?i)process${variant.name}GoogleServices/) {
copy {
from "/src/${variant.flavorName}"
into '.'
include 'google-services.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级别上修改它。
根据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 '.'
}
}
}
这样你就不必手动切换设置了。
...
gradle.taskGraph.beforeTask { Task task ->
if (task.name ==~ /process.*GoogleServices/) {
android.applicationVariants.all { variant ->
if (task.name ==~ /(?i)process${variant.flavorName}(Debug|Release)GoogleServices/) {
copy {
from "src/tenants/${variant.flavorName}"
include 'google-services.json'
into '.'
}
}
}
}
}
gradle.taskGraph.afterTask { Task task ->
if (task.name ==~ /process.*GoogleServices/) {
android.applicationVariants.all { variant ->
if (task.name ==~ /(?i)process${variant.flavorName}(Debug|Release)GoogleServices/) {
delete fileTree(".").matching {
include 'google-services.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,包含你的两个客户端。 当然,这取决于你如何实现你的口味的后端。如果它们没有分开,那么这个解决方案就不能帮助你。
谷歌在play服务插件的2.0版本中包含了对不同口味的支持。由于这个版本的gradle插件com.google.gms:google-services:2.0.0-alpha3
你可以这样做
第一步:添加到gradle
// To auto-generate google map api key of google-services.json
implementation 'com.google.android.gms:play-services-maps:17.0.0'
第二步:在应用程序标签中添加到AndroidManifest.xml
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key" />
第三步:从firebase下载每个flavor JSON文件并添加
app/src/
flavor1/google-services.json
flavor2/google-services.json
3.0.0版本的插件在这些位置搜索JSON文件(考虑到你有一个flavor flavor1和一个构建类型调试):
/app/src/debug/google-services.json
/app/src/debug/flavor1/google-services.json
/app/google-services.json
即使使用flavorDimensions,我也能做到。一个维度是免费和付费,另一个维度是模拟和刺激。我还有3个构建类型:调试、发布和登台。这是它在我的FreeProd项目中的外观:
有多少谷歌服务?json文件将取决于你的项目的特点,但你将需要至少一个json文件为每个谷歌项目。
如果你想了解更多关于这个插件如何处理这些JSON文件的细节,下面是: https://github.com/googlesamples/google-services/issues/54#issuecomment-165824720
官方文档链接: https://developers.google.com/android/guides/google-services-plugin
更新信息的博客文章:https://firebase.googleblog.com/2016/08/organizing-your-firebase-enabled-android-app-builds.html
去这里查看这个插件的最新版本:https://mvnrepository.com/artifact/com.google.gms/google-services?repo=google