更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
当前回答
...
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'
}
}
}
}
}
其他回答
谷歌在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
我目前在同一个应用程序包中使用两个GCM项目Id。我加入了谷歌服务。我的第一个GCM项目的json,但我从第一个切换到第二个只改变SENDER_ID:
String token = instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
(在这一点上,我认为谷歌的服务。Json不是强制性的)
谷歌服务。Json文件是不必要的接收通知。只要在你的构建中为每种味道添加一个变量。gradle文件:
buildConfigField "String", "GCM_SENDER_ID", "\"111111111111\""
使用这个变量BuildConfig。GCM_SENDER_ID代替getString(R.string.gcm_defaultSenderId)注册时:
instanceID.getToken(BuildConfig.GCM_SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
根据@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。替换您所使用的风味名称。
所以如果你想通过编程复制google的服务。Json文件从你的所有变体到你的根文件夹。当你切换到特定的变体时,这里有一个解决方案
android {
applicationVariants.all { variant ->
copy {
println "Switches to $variant google-services.json"
from "src/$variant"
include "google-services.json"
into "."
}
}
}
这种方法有一个警告,那就是你需要有谷歌服务。Json文件在你的每个变体文件夹这里是一个例子。