不同产品的google-services.json

这篇博客讨论了在Android应用中管理不同产品口味(product flavors)的google-services.json文件的问题。当应用有不同的applicationId和Google Cloud Messaging(GCM,现已被FCM取代)项目ID时,通常google-services.json需要放在app目录而非flavors目录下。作者提到可以为每个口味创建不同的GCM项目ID,或者使用同一个json文件但更改SENDER_ID。随着更新,建议使用Firebase Cloud Messaging(FCM),并引用了关于使用构建变体配置Firebase的博客。尽管有一些权宜之计,但作者希望Google能提供更好的解决方案。
摘要由CSDN通过智能技术生成

本文翻译自:google-services.json for different productFlavors

Update: GCM is deprecated, use FCM 更新:不建议使用GCM ,请使用FCM

I'm implementing the new Google Cloud Messaging following the guides from the Google Developers page here 我实施新的谷歌云消息之后从谷歌开发者页面的导游在这里

I've successfully run and test it. 我已经成功运行并测试了它。 But my problem now is I have different product flavors with different applicationId/packageName and different Google Cloud Messaging Project Id. 但是我现在的问题是我有不同的产品口味,具有不同的applicationId / packageName和不同的Google Cloud Messaging Project ID。 The google-services.json have to be put at the /app/google-services.json not the flavors folder. google-services.json必须放在/app/google-services.json而不是flavors文件夹中。

Is there any way to make the google-services.json config different for many flavors? 有什么方法可以使google-services.json配置与许多口味有所不同吗?


#1楼

参考:https://stackoom.com/question/257Fp/不同产品的google-services-json


#2楼

I'm currently using two GCM Project Id in the same app package. 我目前在同一应用包中使用两个GCM项目ID。 I put the google-service.json of my first GCM project but I switch from the first to the second one only changing the SENDER_ID: 我将第一个GCM项目的google-service.json放在了第一个GCM项目中,但是我只更改了SENDER_ID从第二个转到了第二个:

    String token = instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

(At this point I think that the google-services.json isn't mandatory ) (目前,我认为google-services.json不是必需的)


#3楼

UPDATED: 更新:

In terms of Firebase setup with build variants, please refer to this blog which has detailed instructions. 关于带有构建变体的Firebase设置,请参阅此博客 ,其中有详细说明。


#4楼

You have many flavor, so it mean you will have many difference package id, right? 您有很多口味,所以这意味着您将有许多不同的包装ID,对吗? So, just go to the page where you setup/generate your json file and config for each package name. 因此,只需转到您为每个包名称设置/生成json文件和配置的页面。 All of it will add to json file. 所有这些都将添加到json文件中。

I'm verry lazy to post picture now, but basically: 我很懒惰现在发布图片,但是基本上:

  • go to https://developers.google.com/mobile/add 转到https://developers.google.com/mobile/add
  • select platform 选择平台
  • select your app 选择你的应用
  • IMPORTANT : type your flavor package name to field "android package name" 重要提示 :在“ android package name”字段中输入您的调味包名称
  • ... continue to get your configuration file. ...继续获取您的配置文件。 Download it! 下载它!

When config the file, you can see that google show you the Server API Key + Sender ID. 配置文件时,您可以看到Google向您显示了服务器API密钥+发件人ID。 And it is same for all package (flavors) 所有包装(调味料)都相同

At the end, you just need only one json file for all flavors. 最后,您只需要一个json文件即可使用所有样式。

One more question here that you have to test when you register to get Registration Token, check if is difference for each flavor. 这里还有一个问题,您必须在注册时进行测试才能获得注册令牌,并检查每种口味是否有所不同。 I don't touch on it but it think it should be difference. 我没有提及,但它认为应该有所不同。 Too late now and i so sleepy :) Hope it help! 现在为时已晚,我很困:)希望能有所帮助!


#5楼

Well I am running into the same problem and couldn't get any perfect solution. 好吧,我遇到了同样的问题,无法获得完美的解决方案。 It's just a workaround. 这只是一个解决方法。 I am wondering how Google didn't think about flavors...? 我想知道Google如何不考虑口味...? And i hope they will propose soon a better solution. 我希望他们能尽快提出更好的解决方案。

What I am doing: 我在做什么:

I have two flavors, in each one I put the corresponding google-services.json : src/flavor1/google-services.json and src/flavor2/google-services.json . 我有两种口味,在每种口味中,我都放置了相应的google-services.json: src/flavor1/google-services.jsonsrc/flavor2/google-services.json

Then in build gradle I copy the file depending on the flavor to the app/ directory: 然后在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
}

Limitation: you will have to change myFlavor manually in gradle each time you want to run for a different flavor (because it's hardcoded). 局限性:每次要运行其他风味时,都必须在myFlavor 手动更改myFlavor (因为它是硬编码的)。

I tried many ways to get the current build flavor like afterEvaluate close... couldn't get any better solution until now. 我尝试了多种方法来获取当前的构建风格,例如afterEvaluate close ... afterEvaluate都找不到更好的解决方案。

Update, Another solution: one google-services.json for all the flavors: 更新,另一种解决方案:一个针对所有口味的google-services.json:

You can also, have different package names for each flavor and then in the google developer console you don't have to create two different apps for each flavor, but just two different clients in the same app. 您也可以为每种口味使用不同的程序包名称,然后在google开发人员控制台中不必为每种口味创建两个不同的应用程序,而在同一应用程序中只需创建两个不同的客户端即可。 Then you will have only one google-services.json that contains your both clients. 然后,您将只有一个包含两个客户端的google-services.json Of course, this depends on how you're implementing the backend of your flavors. 当然,这取决于您如何实现口味的后端。 If they're not separated then this solution will not help you. 如果他们没有分开,那么该解决方案将无法为您提供帮助。


#6楼

According to ahmed_khan_89 's answer, you can put you "copy code" inside product flavors. 根据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 '.'
        }
    }
}

Then you don't have to switch settings manually. 然后,您不必手动切换设置。

`com.google.gms.google-services` 是 Google Play 服务的 Gradle 插件,它通常用于处理 Google 服务配置文件(如 `google-services.json`),这些文件包含了与 Google 服务相关的配置信息,比如 Firebase 或 Google 登录。要在你的 Android 工程中写入这个插件,通常需要在项目的根目录下的 `build.gradle` 文件(不是应用模块下的)中添加 Google 服务的类路径依赖,并在应用模块的 `build.gradle` 文件中应用这个插件。 以下是添加 `com.google.gms.google-services` 插件的基本步骤: 1. 打开你的项目根目录下的 `build.gradle` 文件。 2. 在 `buildscript` 部分的 `dependencies` 块中添加 Google 服务插件的类路径依赖: ```gradle buildscript { repositories { google() // Google 的 Maven 仓库 jcenter() // 可选,如果你的项目依赖于此 } dependencies { classpath 'com.google.gms:google-services:4.x.x' // 使用最新的版本号 } } ``` 3. 确保你的项目同步(Synchronize Project with Gradle Files)。 4. 打开应用模块的 `build.gradle` 文件(通常是 `app/build.gradle`)。 5. 在文件底部或合适的位置,添加 `apply plugin: 'com.google.gms.google-services'` 以应用插件。 ```gradle apply plugin: 'com.android.application' android { // Android 配置部分 } dependencies { // 依赖配置部分 } apply plugin: 'com.google.gms.google-services' // 在这里应用插件 ``` 确保你已经正确安装了 Google 服务插件,并且同步了你的项目。在同步之后,Android Studio 会自动处理 `google-services.json` 文件,并且根据该文件内容进行配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值