更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。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
其他回答
受到上面@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
}
我目前在同一个应用程序包中使用两个GCM项目Id。我加入了谷歌服务。我的第一个GCM项目的json,但我从第一个切换到第二个只改变SENDER_ID:
String token = instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
(在这一点上,我认为谷歌的服务。Json不是强制性的)
只需在同一个项目中添加调味应用程序id和口味的名称,下载google-service。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"
}
]
}
根据@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。替换您所使用的风味名称。