google-services.json 到底有什么作用? [英] What does google-services.json really do?

查看:43
本文介绍了google-services.json 到底有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我致力于将 Google Analytics 和 GCM 服务添加到我当前的应用中.在这两个服务实现的指南中,google 要求开发者生成一个 json 文件:google-services.json 并将其放在应用程序的根目录下.

I work on adding Google Analytics and GCM services to my current app. On the guide for both services implementation, google asks developer to generate a json file: google-services.json and put it under root directory of the app.

我发现即使我从我的应用程序中删除了这个 json 文件,服务仍然有效.

I found that even if I delete this json file from my app, the services still works.

只是想确定一下,这个文件到底是干什么用的?它的用途是什么?它是如何工作的?

Just want to know for sure, what is this file really for? What its usage and how does it work?

推荐答案

我对 google-services 插件和 json 进行了一些调查,并找到了该插件的来源.

I investigated a bit regarding the google-services plugin and json and found the sources to this plugin.

classpath 引用并带有 apply 的 gradle-plugin google-services 只是一个构建时插件!所以它只影响你应用的构建过程,而不影响运行时过程!

The gradle-plugin google-services that is referenced by classpath and with apply is a build-time plugin only! So it only influences the build-process of your app, but not the runtime-process!

此插件仅作为快速入门助手,可在您的应用中快速集成 Google 服务.显然,该过程有些复杂且没有记录,因此 Google 应该明确说明该过程的作用.

This plugin is only meant as a quickstart-helper to integrating Google-services quickly in your app. Obviously, the process is somewhat convoluted and not documented, so Google should have made it clear what this process does.

事实上,我找到了插件版本 com.google.gms:google-services:1.4.0-beta3 的源代码,但没有找到任何关于 appinvites 的具体参考,也没有找到任何 Google API for App Invites!(但也许它只是使用了一个带有项目 id 的通用 API 项目,我没有尝试过)

In fact, I found the source code for the plugin version com.google.gms:google-services:1.4.0-beta3 and didnt find any specific reference in it regarding appinvites nor did I find any Google API for App Invites! (But maybe it just uses a generic API project with its project id, I didnt try this)

google-services gradle-plugin 在您的应用模块中查找提到的 google-services.json 文件.然后它会在 google-services.json 文件中查找由 Google API 开发人员控制台生成的配置设置,如项目 ID 和跟踪 ID 等.从它找到的设置中,Android 资源值生成到以下路径:

The google-services gradle-plugin looks for the mentioned google-services.json file in your app-module. Then it looks for configured settings like project-id's and tracking-id's and such, generated by the Google API developer console into the google-services.json file. From the settings it found, Android resource values are generated into the following path:

$project.buildDir/generated/res/google-services/$variant.dirName/values/values.xml

例如对于您的应用程序的调试构建:

For example for a debug-build of your app:

app/build/generated/res/google-services/debug/values/values.xml

例如如果您遵循 GCM 教程,则 JSON 文件将包含 API 项目的 id 作为以下 android-resource:

E.g. if you followed the GCM tutorial, the JSON file would include the API project's id as the following android-resource:

<string name="gcm_defaultSenderId">project-id</string>

因此,此插件和 JSON 文件对于运行或发布您的应用并不是必不可少的,它只是生成一些基本的 android 资源文件以便更轻松地集成特定 Google API 功能的快速入门助手.

So this plugin and JSON file are not essential to running or publishing your app, it is just a quickstart helper to generate some basic android-resource files for easier integration of specific Google API features.

请注意,在下面引用的源代码中,google-services 插件始终为您的 app/build.gradle 中定义的每个应用变体生成那些 android-resources.

Notice in the source code referenced below that the google-services plugin always generates those android-resources for every app-variant that is defined in your app/build.gradle.

如果你不想那样,你应该在你想要的应用变体中使用那些生成的资源,并删除其他的.不要忘记从 app/build.gradle 中删除 google-services 插件 apply,否则它会为所有 app-variants 重新生成.

If you don't want that, you should use those generated resources in the app-variants you want, and delete the others. Don't forget to remove the google-services plugin apply from app/build.gradle, or else it will be regenerated for all app-variants.

此插件和 JSON 文件不会直接影响您应用的上述 Google 功能的内部工作!如果您已经在 developer.android.com 上学习了有关如何集成的旧教程,例如GCM 或 Google Analytics,那么您甚至不需要集成 gradle-plugin google-services 或 google-services.json 文件!

This plugin and JSON-file do NOT directly influence the inner workings of said Google-features for your app! If you already have followed older tutorials on developer.android.com on how to integrate e.g. GCM or Google Analytics, then you don't even need to integrate either the gradle-plugin google-services or the google-services.json file!

在您集成 google-services gradle-plugin 并同步您的项目后,Gradle 会自动将 google-services 依赖项下载到类似于此的路径(在 Windows 上,您可能需要查看您的 home/.gradle for Linux):

After you integrated the google-services gradle-plugin and when sync your project, Gradle automatically downloads the google-services dependency to a path similar to this (on Windows, you might need to look into your home/.gradle for Linux):

C:Usersuser.gradlecachesmodules-2files-2.1com.google.gmsgoogle-services1.4.0-beta3f1580f62e3be313eba041ce19b64fd3f44cf8951google-services-1.4.0-beta3-sources.jar

如果你解压这个jar文件,你会发现两个文件:

If you extract this jar-file, you will find two files:

GoogleServicesPlugin.groovy
GoogleServicesTask.java

其中包含 gradle-plugin 的纯源代码.

which contain the plain source code of the gradle-plugin.

GoogleServicesPlugin.groovy

包含应用变体的处理和路径的基本定义等.

contains the handling of the app-variants and basic definitions of paths etc.

GoogleServicesTask.java

包含实际的任务定义,查找以下方法以了解其真正作用:

contains the actual task-definition, look for the following method to see what it really does:

@TaskAction
public void action() throws IOException { 

这篇关于google-services.json 到底有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆