简介

google-servicesbuild.gradle
dependencies {
    classpath 'com.google.gms:google-services:4.4.2'
    // ...
}
google-services
google-services.jsonapp/build.gradleapply plugin: 'com.google.gms.google-services'./gradlew :app:dependencies

添加 JSON 文件

google-services.jsonapp/2.2.0
// dogfood and release are build types.
app/
    google-services.json
    src/dogfood/google-services.json
    src/release/google-services.json
    ...

注意:通过在发布目录中提供 google-services.json 文件,您可以为正式版 APK 维护一个单独的 Firebase 项目。

使用产品变种时,这些更复杂的目录结构也是有效的。

// free and paid are product flavors.
app/
    google-services.json
    src/dogfood/paid/google-services.json
    src/release/free/google-services.json
    ...

处理 JSON 文件

google-services.json
{
  "project_info": {...},
  "client": [...],
}
project_infoclient
client
clientclient_info/android_client_info/package_nameclient
{YOUR_CLIENT}client

JSON 处理的主要结果是生成两个 XML 文件,您可以在 Java 代码中将其作为 Android 资源引用。以下是每个文件的示例:

app/build/generated/res/google-services/{build_type}/values/values.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <! -- Present in all applications -->
    <string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string>

    <! -- Present in applications with the appropriate services configured -->
    <string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
    <string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
    <string name="ga_trackingId" translatable="false">UA-65557217-3</string>
    <string name="firebase_database_url" translatable="false">https://example-url.firebaseio.com</string>
    <string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="project_id" translatable="false">mydemoapp</string>

</resources>

app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="ga_trackingId" translatable="false">UA-65557218-3</string>
</resources>
google-services.jsongoogle-services

google_app_id:

{YOUR_CLIENT}/client_info/mobilesdk_app_id

gcm_defaultSenderId

project_info/project_number

default_web_client_id:

{YOUR_CLIENT}/oauth_client/client_id (client_type == 3)

ga_trackingId:

{YOUR_CLIENT}/services/analytics-service/analytics_property/tracking_id

firebase_database_url:

project_info/firebase_url

google_api_key:

{YOUR_CLIENT}/api_key/current_key

google_crash_reporting_api_key:

{YOUR_CLIENT}/api_key/current_key

project_id

project_info/project_id

问题排查

问:在构建时,我收到以下错误消息:“File google-services.json is missing from module root folder. 没有它,Google 服务插件将无法运行”。

google-services.jsongoogle-services.jsonapp/app/src/{build_type}

问:我找不到“R.string.gcm_defaultSenderId”“R.xml.global_tracker”等符号。

build.gradlegoogle-services.json