更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。Json配置不同的许多口味?
更新:GCM已弃用,使用FCM
我正在实现新的谷歌云消息,遵循从谷歌开发人员页面这里的指南
我已经成功地运行并测试了它。但我现在的问题是,我有不同的产品口味,具有不同的applicationId/packageName和不同的谷歌云消息传递项目Id。谷歌服务。Json必须放在/app/google-services。Json,而不是flavor文件夹。
有没有办法让谷歌的服务。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 variables
其他回答
只需在同一个项目中添加调味应用程序id和口味的名称,下载google-service。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
简短的回答:
实现: 默认情况下,您应该复制google-services。Json到app dir。
如果想要获得其他效果,可以复制谷歌的服务。Json到app/src/{flavor-name}目录
测试: 尝试构建,打开构建选项卡,然后使用解析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”
根据@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。替换您所使用的风味名称。