GoogleService 初始化失败

我使用谷歌地图在我的机器人应用程序。我已经创建了密钥,并在清单文件中添加了必要的权限。但是很快我就启动了应用程序,我在调试器中得到了以下信息:

GoogleService 初始化失败,状态: 10,缺少一个期望值 资源: ‘ R.string.Google _ app _ id’用于初始化 Google 服务。 可能的原因是缺少 google-services. json 或 Com.google.gms. google-services gradle plugin.

我不知道出了什么问题。地图工作正常,我可以使用它没有任何问题。我的档案里有这么一条:

编译‘ com.google.android.gms: play-services: 8.4.0’

它在抱怨什么? 我该如何减轻它?

100940 次浏览

You need to place the configuration file (google-services.json) generated by developer.google.com, as mentioned in the 2nd step of the official docs here

The process is simple

  1. You can select your project or create a new one.
  2. Then after selecting desired services (in this case the maps service), you can generate the configuration file.

    For people who have migrated to the Firebase projects they can get the same by going to Firebase Console, selecting your project and under settings you will find the configuration file.

  3. Then as quoted in step 3 of the official docs here

    Copy the google-services.json file you just downloaded into the app/ or mobile/ directory of your Android

P.S : For people downvoting this answer, please do leave a comment as to why you are down voting it.

This also happen to me. In my case, it is because Android studio tried to insert some code to my main activity. Removing the code fixes the error

Inserted code is about App Indexing:

https://developers.google.com/app-indexing/android/publish

In my case, the cause of this error was that the Google Services plugin for Gradle and the Play Services library were incompatible versions. Instructions for compatible versions:

1) Add the dependency to your project-level build.gradle:

classpath 'com.google.gms:google-services:1.5.0-beta2'

2) Add the plugin to your app-level build.gradle:

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

3) If you're using Android Studio, this is the string to add to the dependency section of your application's build.gradle file:

dependencies {
compile "com.google.android.gms:play-services:8.3.0"
}

Source: https://developers.google.com/cloud-messaging/android/client

I met the same problem and solved it followed the official solution.

Here are the steps:

  1. get the configuration file google-services.json from this link.

  2. Copy the google-services.json file you just downloaded into the app/ or mobile/ directory of your Android Studio project.

  3. Add the dependency to your project-level build.gradle:

    classpath 'com.google.gms:google-services:1.5.0-beta2'
    
  4. Add the plugin to your app-level build.gradle:

    apply plugin: 'com.google.gms.google-services'
    
  5. Add this dependency to your app-level build.gradle:

    dependencies {
    compile "com.google.android.gms:play-services:8.3.0"
    }
    

I had the same issue back then. Was able to solve it by using only the necessary play services library, which in my case GCM.

Instead of com.google.android.gms:play-services:8.4.0, I use com.google.android.gms:play-services-gcm:8.4.0. See here for more info; this also solves multidex problem.

Then I applied both @Radix and @Alexander's approach to remove the message GCM has been output in the logcat regarding the google-services.json.

Just have another solution i'm remove accidentally

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

Add this and work, is for new solution

[From Product Manager @ Google]

You can fix this issue by downloading and copying the google-services.json file for your Android app by following the steps below:

  • Select your app/project name and Android packagename from this link and click Continue to Choose and configure services.
  • Click Continue to Generate Configuration files.
  • Download google-services.json and copy the file to the app/ or mobile/ module directory in your Android project.

If you have previously imported your Google project into Firebase, you can get the updated google-services.json from the Firebase console under Project Settings.

DO NOT COPY the PROJECT_NUMBER as suggested by one of the other answers since the google_app_id refers to your app within a Project and not the project itself.

For those who face this problem even after correctly setting up play services and placing google-services.json file in project/app folder, the actual solution is to

Build > Rebuild Project

Possibly due to the fact that strings from json file are not integrated into compiled resources until a full rebuild is performed.

You can change the versionCode and versionName of your app in your Gradle file.

A quick fix I used is to disable signing. If you aren't wanting to create production code you can set your build variant to debug, localOldDebug or localDebug.

Click on Build Variants on the bottom left in Android Studio.

how-to-disable-generated-signed-apk-android-studio

For Xamarin/Visual Studio Mac I needed to add this to the bottom of my Droid.csproj

<Target Name="ProcessGoogleServicesJson" Condition=" '@(GoogleServicesJson)' != '' AND '$(AndroidApplication)' == 'True' AND '$(DesignTimeBuild)' != 'True'" BeforeTargets="$(ProcessGoogleServicesJsonBeforeTargets)" AfterTargets="$(ProcessGoogleServicesJsonAfterTargets)">
<Message Text="Using ProcessGoogleServicesJson override" Importance="high" />
<ProcessGoogleServicesJson GoogleServicesJsons="@(GoogleServicesJson)" IntermediateOutputPath="$(IntermediateOutputPath)" MonoAndroidResDirIntermediate="$(MonoAndroidResDirIntermediate)" AndroidPackageName="$(_AndroidPackage)">
<Output ItemName="_AndroidResourceDest" TaskParameter="GoogleServicesGeneratedResources" />
<Output ItemName="FileWrites" TaskParameter="GoogleServicesGeneratedResources" />
</ProcessGoogleServicesJson>
<ItemGroup>
<FileWrites Include="$(IntermediateOutputPath)\ProcessGoogleServicesJson.stamp" />
</ItemGroup>

https://github.com/xamarin/GooglePlayServicesComponents/issues/64