更新: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的回答,您可以将“复制代码”放在产品风味中。
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不是强制性的)
我发现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
事实上,只有一个谷歌——服务。MyApp/app/目录下的com.google.gms:google-services:3.0.0是很好的,不需要额外的脚本。但是要小心删除文件google-services。为了避免错误类型为“:app:processDebugGoogleServices”的任务执行失败,请从应用目录MyApp/app/src/flavor1/res/”中删除。没有为包找到匹配的客户端
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中。
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”