更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
当前回答
根据@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。替换您所使用的风味名称。
其他回答
根据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 '.'
}
}
}
这样你就不必手动切换设置了。
我目前在同一个应用程序包中使用两个GCM项目Id。我加入了谷歌服务。我的第一个GCM项目的json,但我从第一个切换到第二个只改变SENDER_ID:
String token = instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
(在这一点上,我认为谷歌的服务。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答案的启发。我们可以直接这样保存在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
}
简短的回答:
实现: 默认情况下,您应该复制google-services。Json到app dir。
如果想要获得其他效果,可以复制谷歌的服务。Json到app/src/{flavor-name}目录
测试: 尝试构建,打开构建选项卡,然后使用解析json文件检查输出消息:.....