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

How to integrate two or more google-services.json file together for different google service in same project using Android Studio

我正在制作一个项目,我想在其中集成 GCMGoogle sign in,但问题是它们都有 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"
}

您不需要两个不同的 google-services.json 文件。两者将具有相同的配置。无论您激活了多少服务,这些配置文件对于每个项目都是唯一的。

在生成配置文件之前,请确保您已激活这两项服务。生成的配置文件对两者都有效。

按照 "Download and install Configuration" link 下方显示的 "Continue to Sign In" link 获取有关下一步操作的文档。

最后我做了如下:

第 1 步: 打开您想要的 google 服务页面,在我的例子中是 google sign in and GCM。将有一个按钮显示 获取配置文件,单击该按钮并输入您的详细信息并获取配置文件。

第 2 步: 检查两个配置文件,它在 project_info 对象和 [=26= 中具有相同的配置]client_info对象。区别在于 services 对象,如果您添加了两个或更多服务,则必须检查状态 status 值是 2 这意味着它们是已启用的服务。您可以在下面的配置文件中看到我为 Google sign inGCM 两个服务生成的配置文件。

您只需要在 services 对象中检查您的 status2 你在项目中集成的所有服务都是你必须添加的配置文件。

{
  "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.

生成,您将不会获得该字段值