我正在按照 Google Developers 页面 here 中的指南实现新的 Google Cloud Messaging
我已经成功运行并测试了它。但我现在的问题是我有不同的产品风格,具有不同的 applicationId/packageName 和不同的 Google Cloud Messaging Project Id。 google-services.json
必须放在 /app/google-services.json
而不是 flavor 文件夹中。
有什么方法可以使 google-services.json
配置因多种风格而不同?
最佳答案
Google 在 2.0 版的播放服务插件中包含了对 flavor 的支持。由于这个版本的 gradle 插件 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 下载每个 flavor 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 风格项目中的外观:
有多少 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 - google-services.json 用于不同的 productFlavors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30772201/