更新:不推荐使用 GCM,请使用 FCM
我正在按照 Google Developers 页面 here 中的指南实施新的 Google Cloud Messaging
我已经成功运行并测试了它。但我现在的问题是我有不同的产品风格,具有不同的 applicationId/packageName 和不同的 Google Cloud Messaging Project Id。 google-services.json
必须放在 /app/google-services.json
而不是风味文件夹中。
有什么方法可以使 google-services.json
配置因多种口味而异?
apply plugin: 'com.google.gms.google-services'
行似乎将 gcm
字符串放入 app/build/generated/res/google-services/debug/values/values.xml
...
Google 在播放服务插件的 2.0 版中包含了对风味的支持。由于此版本的 gradle plugin com.google.gms:google-services:2.0.0-alpha3
你可以这样做
第 1 步:添加到 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" />
第 3 步:从 firebase 下载每个风味 JSON 文件并添加它
app/src/
flavor1/google-services.json
flavor2/google-services.json
3.0.0 版插件在这些位置搜索 JSON 文件(考虑到您有 flavor
flavor1 和构建类型 debug
):
/app/src/debug/google-services.json
/app/src/debug/flavor1/google-services.json
/app/google-services.json
即使使用flavorDimensions,这也对我有用。我在一个维度有免费和付费,在另一个维度有 Mock & Prod。我也有 3 种 buildType:调试、发布和登台。这是它在我的 FreeProd 风格项目中的外观:
https://i.stack.imgur.com/yekEo.png
有多少 google-services.json 文件取决于您的项目特征,但每个 Google 项目至少需要一个 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
更新:以下说明适用于一个 Android Studio 项目,该项目中有一个 Firebase 项目和不同的 Firebase 应用程序。如果目标是在同一个 Android Studio 项目中的不同 Firebase 项目中为不同的 Firebase 应用提供不同的 JSON 文件,(或者如果您不知道有什么区别)look here.。
每个 Android 应用程序 ID(通常是包名称)都需要一个 Firebase 应用程序。每个 Gradle 构建变体通常有一个应用程序 ID(如果您使用 Gradle 构建类型和 Gradle 构建风格,这很可能)
从 Google Services 3.0 开始,使用 Firebase 无需为不同的风格创建不同的文件。如果您拥有相互组合的 productFlavours 和 Build 类型,则为不同的风格创建不同的文件可能并不清晰或直接。
在同一个文件中,您将拥有所有构建类型和风格所需的所有配置。
在 Firebase 控制台中,您需要为每个包名称添加一个应用。想象一下,您有 2 种风格(开发和实时)和 2 种构建类型(调试和发布)。根据您的配置,但您可能有 4 个不同的包名称,例如:
com.stackoverflow.example(实时发布)
com.stackoverflow.example.dev(实时 - 开发)
com.stackoverflow.example.debug(调试 - 发布)
com.stackoverflow.example.dev.debug(调试-开发)
Firebase 控制台中需要 4 个不同的 Android 应用。 (在每台计算机上,您都需要添加 SHA-1 以进行调试并为您使用的每台计算机实时运行)
当您下载 google-services.json 文件时,实际上从哪个应用下载它并不重要,它们都包含与您的所有应用相关的相同信息。
现在您需要在应用级别 (app/) 中找到此文件。
https://i.stack.imgur.com/zTTGg.png
如果您打开该文件,您会看到它包含所有包名称的所有信息。
一个痛点曾经是插件。为了让它工作,您需要在文件底部找到插件。所以这条线..
apply plugin: 'com.google.gms.google-services'
...需要位于您的应用程序 build.gradle 文件的底部。
对于这里所说的大部分内容,它也适用于以前的版本。我从来没有为不同的配置提供不同的文件,但现在使用 Firebase 控制台更容易,因为它们提供了一个文件,其中包含所有配置所需的一切。
Firebase 文档
Firebase 云消息传递
Firebase 控制台
就这个问题写了一篇Medium post。
有一个类似的问题(使用 BuildTypes 而不是 Flavors),并像这样修复它。
利用 Gradle 的依赖管理系统。我创建了两个任务,switchToDebug
和 switchToRelease
。要求在任何时候运行 assembleRelease
,同时运行 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
任务在每个风味级别对其进行修改。
根据 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 '.'
}
}
}
这样您就不必手动切换设置。
好吧,我遇到了同样的问题,无法得到任何完美的解决方案。这只是一种解决方法。我想知道谷歌怎么没有考虑口味......?我希望他们能尽快提出更好的解决方案。
我在做什么:
我有两种口味,每种我都放了相应的 google-services.json : src/flavor1/google-services.json
和 src/flavor2/google-services.json
。
然后在 build gradle 中,我根据风格将文件复制到 app/
目录:
android {
// set build flavor here to get the right gcm configuration.
//def myFlavor = "flavor1"
def myFlavor = "flavor2"
if (myFlavor.equals("flavor1")) {
println "--> flavor1 copy!"
copy {
from 'src/flavor1/'
include '*.json'
into '.'
}
} else {
println "--> flavor2 copy!"
copy {
from 'src/flavor2/'
include '*.json'
into '.'
}
}
// other stuff
}
限制:每次您想要运行不同的风格(因为它是硬编码的)时,都必须在 gradle 中手动更改myFlavor
。
我尝试了很多方法来获得当前的构建风格,比如 afterEvaluate
close...直到现在都没有更好的解决方案。
更新,另一种解决方案:一个 google-services.json 用于所有口味:
您还可以为每种风格设置不同的软件包名称,然后在 google developer console 中您不必为每种风格创建两个不同的应用程序,而只需在同一个应用程序中创建两个不同的客户端。那么您将只有一个包含您的两个客户端的 google-services.json
。当然,这取决于您如何实现风味的后端。如果它们没有分开,那么此解决方案将无济于事。
release
和 debug
使用一个 google-services.json
对我有用。我认为如果您只是像我一样尝试拆分 debug
构建,这是最简单的解决方案。作为参考,您可以在此处生成文件:developers.google.com/mobile/add?platform=android
我正在使用从此处创建的 google-services.json 文件:https://developers.google.com/mobile/add?platform=android&cntapi=gcm&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fandroid%2Fclient&cntlbl=Continue%20Adding%20GCM%20Support&%3Fconfigured%3Dtrue
在 JSON 结构中有一个称为客户端的 JSON 数组。如果您有多种口味,只需在此处添加不同的属性。
{
"project_info": {
"project_id": "PRODJECT-ID",
"project_number": "PROJECT-NUMBER",
"name": "APPLICATION-NAME"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:PROJECT-NUMBER:android:HASH-FOR-FLAVOR1",
"client_id": "android:PACKAGE-NAME-1",
"client_type": 1,
"android_client_info": {
"package_name": "PACKAGE-NAME-1"
}
},
"oauth_client": [],
"api_key": [],
"services": {
"analytics_service": {
"status": 1
},
"cloud_messaging_service": {
"status": 2,
"apns_config": []
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"google_signin_service": {
"status": 1
},
"ads_service": {
"status": 1
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:PROJECT-NUMBER:android:HASH-FOR-FLAVOR2",
"client_id": "android:PACKAGE-NAME-2",
"client_type": 1,
"android_client_info": {
"package_name": "PACKAGE-NAME-2"
}
},
"oauth_client": [],
"api_key": [],
"services": {
"analytics_service": {
"status": 1
},
"cloud_messaging_service": {
"status": 2,
"apns_config": []
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"google_signin_service": {
"status": 1
},
"ads_service": {
"status": 1
}
}
}
],
"client_info": [],
"ARTIFACT_VERSION": "1"
}
在我的项目中,我使用了相同的项目 ID,当我在上面的 url 中添加第二个包名时,谷歌为我提供了一个文件,其中包含 json-data 中的多个客户端。
1.) google-services.json 的真正作用是什么?
按照这个:https://stackoverflow.com/a/31598587/2382964
2.) google-services.json 文件如何影响您的 android studio 项目?
按照这个:https://stackoverflow.com/a/33083898/2382964
简而言之,第二个 url,如果您在项目中添加 google-services.json,则此路径中必须有一个为 debug
变体自动生成的 google-services
文件夹
app/build/generated/res/google-services/debug/values/values.xml
3.) 做什么,完成它?
在 project_level
build.gradle 中添加 google-services 依赖项,如果您使用的是 app_compact 库,也可以使用 version 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.) 将 google-service.json 文件放在结构中的什么位置。
案例 1.) 如果您没有 build_flavor,只需将其放在 /app/google-service.json
文件夹中。
情况 2.) 如果您有多个 build_flavor 并且在 app/src/build_flavor/google-service.json
中放置了不同的 google_services.json 文件。
情况 3.) 如果您有多个 build_flavor 并且在 app/google-service.json
中放置了一个 google_services.json 文件。
接收通知不需要 google-services.json 文件。只需在 build.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);
Firebase 现在通过一个 google-services.json 文件支持多个应用程序 ID。
This blog post 对其进行了详细描述。
您将在 Firebase 中创建一个用于所有变体的父项目。然后,您在 Firebase 中为您拥有的每个应用程序 ID 在该项目下创建单独的 Android 应用程序。
创建所有变体后,您可以下载支持所有应用程序 ID 的 google-services.json。当需要单独查看数据(即崩溃报告)时,您可以使用下拉菜单进行切换。
我们有一个用于调试构建的不同包名称 (*.debug),所以我想要一些基于风味和 buildType 的东西,而不必在 processDebugFlavorGoogleServices
的模式中编写任何与风味相关的东西。
我在每种风格中创建了一个名为“google-services”的文件夹,其中包含 json 文件的调试版本和发布版本:
https://i.stack.imgur.com/o8sRo.png
在 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 文件中删除/添加/修改风味,它会自动获取调试或发布 google-services.json。
根据 Firebase docs,您还可以使用 string resources 代替 google-services.json。
由于此提供程序只是读取具有已知名称的资源,因此另一种选择是将字符串资源直接添加到您的应用程序中,而不是使用 Google Services gradle 插件。您可以通过以下方式执行此操作: 从根 build.gradle 中删除 google-services 插件 从您的项目中删除 google-services.json 直接添加字符串资源 从您的项目中删除 apply plugin: 'com.google.gms.google-services'应用程序构建.gradle
示例 strings.xml
:
<string name="google_client_id">XXXXXXXXX.apps.googleusercontent.com</string>
<string name="default_web_client_id">XXXX-XXXXXX.apps.googleusercontent.com</string>
<string name="gcm_defaultSenderId">XXXXXX</string>
<string name="google_api_key">AIzaXXXXXX</string>
<string name="google_app_id">1:XXXXXX:android:XXXXX</string>
<string name="google_crash_reporting_api_key">AIzaXXXXXXX</string>
<string name="project_id">XXXXXXX</string>
从您的项目中删除现有的 google-services.json。 Build > Clean Project 编译并运行您的应用程序查看出现的错误消息,以确定您可以将 google-services.json..mine 放在哪里看起来像这个文件 google-services.json 丢失。没有它,Google 服务插件将无法运行。搜索位置: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\后缀\debug\google-services.json C:\Users\用户名\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\桌面\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\HelloWor ld\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"
不需要任何额外的 gradle 脚本。
Google 开始以“android_client_info”的名称添加不同的包名称。在 google-services.json 中如下所示
"android_client_info": {
"package_name": "com.android.app.companion.dev"
}
因此,以下步骤足以有不同的 google-services.json 选择。
有 2 种风味 将新的开发风味包添加到 google 分析配置页面并下载 google-services.json。请注意,在新的配置文件中,您的两个风味包 id 都在那里准备您的任何风味构建。
这就对了!..
我知道,您怀疑 google-services.json
文件 should be put in the root
app
文件夹,是吗?我会打破这个神话——不一定。您也可以将 google-services.json 文件放入 flavor folder
。像这样:
https://i.stack.imgur.com/T4Geh.png
根据@ZakTaccardi 的回答,并假设您不想要两种风格的单个项目,请将其添加到 build.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-services 插件对于想要添加 GCM 的项目来说毫无用处。它仅生成以下文件,该文件只是将您的项目 ID 添加为字符串资源:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Your API key would be on the following line -->
<string name="gcm_defaultSenderId">111111111111</string>
</resources>
如果您直接从 Cloud Messaging for Android 指南中逐字复制示例代码,您似乎只需要它。这是示例行:
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
解决方案
如果您希望能够为不同的构建类型或产品风格切换 API 项目,您只需定义自己的常量并在调用 getToken()
API 时选择合适的常量。
private static final String SENDER_ID = "111111111111";
private static final String SANDBOX_SENDER_ID = "222222222222";
String token = instanceID.getToken(
BuildConfig.DEBUG ? SENDER_ID : SANDBOX_SENDER_ID,
GoogleCloudMessaging.INSTANCE_ID_SCOPE,
null);
产品风味
上面的代码适用于在调试和发布版本之间切换。对于产品风味,您可以在 java 源文件中定义不同的 API 密钥,并将文件放在其相应的产品风味目录中。供参考:Gradle Build Variants
简化@Scotti 所说的内容。您需要根据产品风格为特定项目创建具有不同包名称的 Multiples 应用程序。
假设您的项目是具有不同产品风格 X、Y 的 ABC,其中 X 的包名称为 com.x,Y 的包名称为 com.y,然后在 firebase 控制台中,您需要创建一个项目 ABC,您需要在其中创建 2 个应用程序包名称为 com.x 和 com.y。然后您需要下载 google-services.json 文件,其中将有 2 个包含这些 pacakges 的客户端信息对象,您就可以开始使用了。
json的片段将是这样的
{
"client": [
{
"client_info": {
"android_client_info": {
"package_name": "com.x"
}
{
"client_info": {
"android_client_info": {
"package_name": "com.y"
}
]
}
因此,如果您想以编程方式将所有变体中的 google-services.json
文件复制到您的根文件夹中。当您切换到特定变体时,这里有适合您的解决方案
android {
applicationVariants.all { variant ->
copy {
println "Switches to $variant google-services.json"
from "src/$variant"
include "google-services.json"
into "."
}
}
}
https://i.stack.imgur.com/AGldc.png
简短的回答:
实现:默认情况下,您应该将 google-services.json 复制到应用程序目录。
对于其他风格,将 google-services.json
复制到 app/src/{flavor-name} 目录
测试:尝试构建,打开构建选项卡,然后使用 Parsing json 文件检查输出消息:.....
google-services 插件的重点是简化 Google 功能的集成。
由于它只从 google-services.json 文件生成 android-resources,我认为过于复杂的 gradle-logic 否定了这一点。
因此,如果 Google 文档没有说明特定 Google 功能需要哪些资源,我建议为每个相关的构建类型/风格生成 JSON 文件,查看插件生成了哪些资源,然后手动放置这些资源进入他们各自的 src/buildtypeORflavor/res 目录。
之后删除对 google-services 插件和 JSON 文件的引用,就完成了。
有关 google-services gradle-plugin 内部工作原理的详细信息,请参阅我的其他答案:
https://stackoverflow.com/a/33083898/433421
嘿朋友还寻找名称只使用小写然后你没有得到这个错误
实际上,只需在 MyApp/app/
目录中添加一个 google-services.json 就可以了,不需要带有 com.google.gms:google-services:3.0.0
的附加脚本。但要小心从应用目录 MyApp/app/src/flavor1/res/
中删除文件 google-services.json
以避免错误类型 Execution failed for task ':app:processDebugGoogleServices'. > No matching client found for package
只需在同一个项目中添加风味应用程序 ID 和风味名称作为应用程序,最后下载 google-service.json 文件,它将在客户端数组 [] 中包含所有风味,适用于所有风味。
https://i.stack.imgur.com/Vkyf8.png
你有很多口味,所以这意味着你会有很多不同的包ID,对吧?因此,只需转到您设置/生成 json 文件并为每个包名称配置的页面。所有这些都将添加到 json 文件中。
我现在很懒惰发布图片,但基本上:
转到 https://developers.google.com/mobile/add
选择平台
选择您的应用
重要提示:在“android 包名称”字段中输入您的风味包名称
...继续获取您的配置文件。下载它!
配置文件时,您可以看到 google 向您显示服务器 API 密钥 + 发件人 ID。所有包装(口味)都相同
最后,您只需要一个 json 文件即可满足所有需求。
这里还有一个问题,您在注册以获得注册令牌时必须测试,检查每种口味是否存在差异。我没有触及它,但它认为应该有所不同。现在太晚了,我很困 :) 希望对您有所帮助!
我目前在同一个应用程序包中使用两个 GCM 项目 ID。我放置了我的第一个 GCM 项目的 google-service.json,但我从第一个切换到第二个,只更改了 SENDER_ID:
String token = instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
(此时我认为 google-services.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
}
将您的“google-services.json”文件分别放在 app/src/flavors 下,然后在 app 的 build.gradle 中,在 android 下添加以下代码
gradle.taskGraph.beforeTask { Task task ->
if (task.name ==~ /process.*GoogleServices/) {
android.applicationVariants.all { variant ->
if (task.name ==~ /(?i)process${variant.name}GoogleServices/) {
copy {
from "/src/${variant.flavorName}"
into '.'
include 'google-services.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'
}
}
}
}
}
不定期副业成功案例分享
File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it.
所以我将求助于每次通过构建脚本将风味文件复制到根文件夹中。