如何在Android中添加google-services.json?

How to add google-services.json in Android?

错误是:

File google-services.json is missing from module root folder. The Google Quickstart Plugin cannot function without it.

根据 developer.google.com https://developers.google.com/cloud-messaging/android/client#get-config

中的文档,上述问题已得到解决

2018 Edit : GCM Deprecated, use FCM

文件google-services.json应该粘贴到app/目录。 在此之后,当我将项目与 gradle 文件同步时,出现意外的顶级异常错误。这是因为:

项目级 Gradle 文件具有

dependencies {
    classpath 'com.android.tools.build:gradle:1.0.0'
    classpath 'com.google.gms:google-services:1.3.0-beta1'
}

和应用级 Gradle 文件具有:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.google.android.gms:play-services:7.5.0' // commenting this lineworks for me
}

顶行在这和classpath 'com.google.gms:google-services:1.3.0-beta1'之间造成了冲突所以我发表评论它现在工作正常并且没有错误 模块根文件夹中缺少文件 google-services.json。没有它,Google 快速启动插件将无法运行。

document says:

Copy the file into the app/ folder of your Android Studio project, or into the app/src/{build_type} folder if you are using multiple build types.

单击应用正上方,即 android(下拉列表) android studio.Select 项目 从下拉菜单中右键单击 应用程序包 并粘贴 json 文件,然后同步它....

google-services.json 文件像 API 键一样工作意味着它以 json 格式存储您的 project_id 和 api 键google 服务(由您在 google 控制台启用)因此无需在不同的地方进行管理。

使用时的重要过程google-services.json

在申请时 gradle 你应该添加

apply plugin: 'com.google.gms.google-services'.

在顶层 gradle 你应该添加下面的依赖项

  dependencies {
        // Add this line
        classpath 'com.google.gms:google-services:3.0.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

不要像 firebase 文档中那样放入根文件夹,只需将 google-json 文件复制到 projectname/app 的根文件夹中,然后就可以正常工作了。 很简单!

WINDOWS

  1. 在 Android Studio (Alt+F12 or View->Tool Windows->Terminal). 中打开终端 window 然后输入

"move file_path/google-services.json app/"

没有双引号。

例如

move C:\Users\siva\Downloads\google-services.json app/

LINUX

  1. 打开 Android Studio 终端并输入此

scp file_path/google-services.json app/

例如:

scp '/home/developer/Desktop/google-services.json' 'app/'

此错误表明您的 package_name 中的 google-services.json 可能有误。我个人在使用

时遇到过这个问题
buildTypes {
    ...

    debug {
        applicationIdSuffix '.debug'
    }
}

在我的 build.gradle 中。所以,当我想调试时,应用程序的名称是 ("all of a sudden") app.something.debug 而不是 app.something。当我更改上述 package_name...

时,我能够 运行 进行调试

它应该在 Project -> app 文件夹中

请从 Firebase 网站找到屏幕截图

  1. 从 Firebase 下载 "google-service.json" 文件
  2. 在 windows 资源管理器中转到此地址 "C:\Users\Your-Username\AndroidStudioProjects" 您将看到 Android Studio 项目的列表
  3. 打开所需的项目,导航到 "app" 文件夹并粘贴 .json 文件
  4. 转到 Android Studio 并单击位于下拉菜单(文件>与文件系统同步)中的 "Sync with file system"
  5. 现在与 Gradle 同步,一切都应该没问题