如何使用 Android Studio 为同一项目中的不同谷歌服务集成两个或多个 google-services.json 文件

如何使用 Android Studio 为同一项目中的不同谷歌服务集成两个或多个 google-services.json 文件

问题描述:

我正在制作一个项目,我想在其中集成 GCMGoogle 登录,但问题是两者都有 google-services.json我们需要在我们的项目中添加配置文件.

I am making a project where I want to integrate GCM and Google sign in but the problem is both have google-services.json configuration file which we need to add in our project.

那么,如何集成 google-services.json 配置文件在我的项目中.

So, how can I integrate both google-services.json configuration file in my project.

这是我的配置文件之一

{
  "project_info": {
    "project_id": "default-xxx",
    "project_number": "xxx",
    "name": "xxx"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:xxx",
        "client_id": "x.package",
        "client_type": 1,
        "android_client_info": {
          "package_name": "x.package_name"
        }
      },
      "oauth_client": [],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 1,
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 1
        },
        "ads_service": {
          "status": 1
        }
      }
    }
  ],
  "client_info": [],
  "ARTIFACT_VERSION": "1"
}

最后我做了如下:

步骤 1: 在我的例子中打开您想要的 google 服务页面 谷歌登录GCM.会有一个按钮说获取配置文件,点击它并输入您的详细信息并获取配置文件.

Step1: Open your desired google service page in my case its google sign in and GCM. There will be a button saying Get configuration file, click on that and input your details and get the configuration files.

步骤 2: 检查 project_info 对象和 client_info 对象中的配置文件是否相同代码> 对象.不同之处在于 services 对象,如果您添加了两个或更多服务,您必须检查状态,status 值将是 2 这意味着它们是启用的服务.您可以在下面的配置文件中看到我为 Google 登录GCM 这两个服务生成的配置文件.

Step2: Check both of the configuration file it would have same configuration in project_info object and client_info object. The difference would be in services object where you have to check for status if you have added two or more service the status value would be 2 which means they are enabled services. You can see in below configuration file which I have generated for two of the services which is Google sign in and GCM.

您只需要在 services 对象中检查您的 status 值,该对象表示您在项目中集成的所有服务的 2是你必须添加的配置文件.

You just have to check your status values in services object where it is saying 2 for all the services you have integrated in your project is the configuration file you have to add.

{
  "project_info": {
    "project_id": "xxxxxxxxxx",
    "project_number": "xxxxxxxx",
    "name": "xxxxxxxxxxx"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "xxxxxxxxxxx",
        "client_id": "xxxxxxxxxxx",
        "client_type": 1,
        "android_client_info": {
          "package_name": "xxxxxxxxxx"
        }
      },
      "oauth_client": [
        {
          "client_id": "xxxxxxxxxx",
          "client_type": 1,
          "android_info": {
            "package_name": "xxxxxxxx",
            "certificate_hash": "xxxxxxxxxxx"
          }
        }
      ],
      "api_key": [],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "cloud_messaging_service": {
          "status": 2, <======= this is my gcm service status
          "apns_config": []
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "google_signin_service": {
          "status": 2   <===== this my google sign in service status
        },
        "ads_service": {
          "status": 1
        }
      }
    }
  ],
  "client_info": [],
  "ARTIFACT_VERSION": "1"
}

注意:我正在使用 Google 登录服务,因此它会生成 oauth_client 字段值,如果您只生成,则不会获得GCM.

Note: I am using Google sign in service so its generating oauth_client field value also which you wouldn't get if you generate only for GCM.