不同产品的google-services.json

本文翻译自: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. 然后,您不必手动切换设置。

你可能感兴趣的:(android)