更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。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级别上修改它。
其他回答
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”
所以如果你想通过编程复制google的服务。Json文件从你的所有变体到你的根文件夹。当你切换到特定的变体时,这里有一个解决方案
android {
applicationVariants.all { variant ->
copy {
println "Switches to $variant google-services.json"
from "src/$variant"
include "google-services.json"
into "."
}
}
}
这种方法有一个警告,那就是你需要有谷歌服务。Json文件在你的每个变体文件夹这里是一个例子。
我们对调试构建有不同的包名(*.debug),所以我想要一些基于flavor和buildType的东西,而不必在processDebugFlavorGoogleServices的模式中编写任何与flavor相关的东西。
我在每个版本中创建了一个名为“google-services”的文件夹,包含了json文件的调试版本和发布版本:
在gradle文件的buildTypes部分,添加以下内容:
applicationVariants.all { variant ->
def buildTypeName = variant.buildType.name
def flavorName = variant.productFlavors[0].name;
def googleServicesJson = 'google-services.json'
def originalPath = "src/$flavorName/google-services/$buildTypeName/$googleServicesJson"
def destPath = "."
copy {
if (flavorName.equals(getCurrentFlavor()) && buildTypeName.equals(getCurrentBuildType())) {
println originalPath
from originalPath
println destPath
into destPath
}
}
}
当你切换构建变量时,它会自动在你的应用模块的根处复制正确的json文件。
在build.gradle的根中添加这两个被调用的方法,以获取当前风格和当前构建类型
def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern;
if( tskReqStr.contains( "assemble" ) )
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher( tskReqStr )
if( matcher.find() ) {
println matcher.group(1).toLowerCase()
return matcher.group(1).toLowerCase()
}
else
{
println "NO MATCH FOUND"
return "";
}
}
def getCurrentBuildType() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
if (tskReqStr.contains("Release")) {
println "getCurrentBuildType release"
return "release"
}
else if (tskReqStr.contains("Debug")) {
println "getCurrentBuildType debug"
return "debug"
}
println "NO MATCH FOUND"
return "";
}
就是这样,你不必担心从你的gradle文件中删除/添加/修改口味,它得到调试或发布谷歌服务。自动json。
你有很多口味,所以这意味着你会有很多不同的包装id,对吧?所以,只需转到您设置/生成json文件和配置每个包名的页面。所有这些都将添加到json文件中。
我现在很懒发布照片,但基本上:
访问https://developers.google.com/mobile/add 选择平台 选择你的应用 重要:输入你的口味包名称字段“android包名称” ... 继续获取配置文件。下载它!
当配置文件时,您可以看到谷歌向您显示服务器API密钥+发送方ID。所有包装(口味)都是一样的
最后,您只需要一个json文件来处理所有类型。
这里还有一个问题,当你注册获得注册令牌时,你必须测试,检查每个口味是否有差异。我没有触及它,但它认为应该是不同的。现在太晚了,我很困:)希望它能帮助!
1)。谷歌提供什么服务?Json真的有用吗?
关注这个:https://stackoverflow.com/a/31598587/2382964
2)。谷歌是如何服务的。Json文件影响你的android工作室项目?
关注这个:https://stackoverflow.com/a/33083898/2382964
简单来说就是第二个url,如果你添加google-services。Json在你的项目中必须有一个自动生成的google-services文件夹在这个路径下调试变量
app/build/generated/res/google-services/debug/values/values.xml
3)。该怎么做,才能把事情做好?
在project_level构建中添加google-services依赖项。Gradle,如果你使用app_compact库,你也可以使用3.0.0版本。
// Top-level build.gradle file
classpath 'com.google.gms:google-services:2.1.2'
现在在app_level build中。你必须在底部添加Gradle。
// app-level build.gradle file
apply plugin: 'com.google.gms.google-services'
注意:在gradle文件底部添加这一行非常重要。否则Gradle构建不会给你任何错误,但它不能正常工作。
4)。谷歌服务该放在哪里?Json文件在你的结构。
如果你没有build_flavor,就把它放在inside /app/google-service中。json文件夹。
情况2.)如果你有多个build_flavor并且你有不同的google_services。Json文件放在app/src/build_flavor/google-service.json中。
情况3.)如果你有多个build_flavor和单个google_services。Json文件放在app/google-service.json中。