更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
当前回答
受到上面@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
}
其他回答
根据@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文件在你的每个变体文件夹这里是一个例子。
你有很多口味,所以这意味着你会有很多不同的包装id,对吧?所以,只需转到您设置/生成json文件和配置每个包名的页面。所有这些都将添加到json文件中。
我现在很懒发布照片,但基本上:
访问https://developers.google.com/mobile/add 选择平台 选择你的应用 重要:输入你的口味包名称字段“android包名称” ... 继续获取配置文件。下载它!
当配置文件时,您可以看到谷歌向您显示服务器API密钥+发送方ID。所有包装(口味)都是一样的
最后,您只需要一个json文件来处理所有类型。
这里还有一个问题,当你注册获得注册令牌时,你必须测试,检查每个口味是否有差异。我没有触及它,但它认为应该是不同的。现在太晚了,我很困:)希望它能帮助!
我目前在同一个应用程序包中使用两个GCM项目Id。我加入了谷歌服务。我的第一个GCM项目的json,但我从第一个切换到第二个只改变SENDER_ID:
String token = instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
(在这一点上,我认为谷歌的服务。Json不是强制性的)
remove the existing google-services.json from your project. Build > Clean Project compile and run your app look at the error message that comes up to figure out where you can put your google-services.json..mine looked like this File google-services.json is missing. The Google Services Plugin cannot function without it. Searched Location: C:\Users\username\Desktop\HelloWorld\app\src\devSuffixYes_EnvQaApistaging_\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\devSuffixYes_EnvQaApistaging_Debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\devDebug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\devSuffixYes_EnvQaApistaging_\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\debug\devSuffixYes_EnvQaApistaging_\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffixDebug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_Debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\envDebug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qaDebug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\apistaging_\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\apistaging_Debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\src\dev\suffix\yes_\env\qa\apistaging_\debug\google-services.json C:\Users\username\Desktop\HelloWorld\app\google-services.json
注意:它还关心flavorDimensions中声明的顺序。我的是flavorDimensions“dev_suffix”,“environment”