GoogleService 初始化失败
GoogleService failed to initialize
我在 android 应用程序中使用 google 地图。我已经创建了密钥并在清单文件中添加了必要的权限。但是很快我启动了应用程序,我在调试器中收到了这条消息:
GoogleService failed to initialize, status: 10, Missing an expected
resource: 'R.string.google_app_id' for initializing Google services.
Possible causes are missing google-services.json or
com.google.gms.google-services gradle plugin.
我不确定出了什么问题。地图工作正常,我可以毫无问题地使用它。我的 gradle 文件有这个条目:
compile 'com.google.android.gms:play-services:8.4.0'
它在抱怨什么,我该如何缓解?
您需要放置由developer.google.com, as mentioned in the 2nd step of the official docs here
生成的配置文件(google-services.json
)
过程简单
- 您可以select您的项目或创建一个新项目。
然后在 selecting 所需的服务(在本例中为地图服务)之后,您可以生成配置文件。
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步here
Copy the google-services.json
file you just downloaded into the app/ or mobile/ directory of your Android
P.S :对于反对这个答案的人,请务必发表评论说明你为什么反对它。
这也发生在我身上。在我的例子中,这是因为 Android studio 试图在我的主 activity 中插入一些代码。删除代码修复了错误
插入的代码是关于 App Indexing 的:
在我的例子中,这个错误的原因是 Google Gradle 的服务插件和 Play 服务库是不兼容的版本。兼容版本说明:
1) 将依赖项添加到您的 project-level build.gradle:
classpath 'com.google.gms:google-services:1.5.0-beta2'
2) 将插件添加到您的 app-level build.gradle:
apply plugin: 'com.google.gms.google-services'
3) 如果您使用的是 Android Studio,这是要添加到应用程序 build.gradle 文件的依赖项部分的字符串:
dependencies {
compile "com.google.android.gms:play-services:8.3.0"
}
来源:https://developers.google.com/cloud-messaging/android/client
我遇到了同样的问题并按照 official solution.
解决了它
步骤如下:
从this link获取配置文件google-services.json
。
将刚刚下载的 google-services.json
文件复制到 Android Studio 项目的 app/ 或 mobile/ 目录中。
将依赖项添加到您的项目级别 build.gradle:
classpath 'com.google.gms:google-services:1.5.0-beta2'
将插件添加到您的应用级别 build.gradle:
apply plugin: 'com.google.gms.google-services'
将此依赖项添加到您的应用级别 build.gradle:
dependencies {
compile "com.google.android.gms:play-services:8.3.0"
}
我当时也有同样的问题。能够通过仅使用必要的播放服务库来解决它,在我的例子中是 GCM。
我使用 com.google.android.gms:play-services-gcm:8.4.0
而不是 com.google.android.gms:play-services:8.4.0
。有关详细信息,请参阅 here;这也解决了 multidex 问题。
然后我应用@Radix 和@Alexander 的方法来删除消息 GCM has been output in the logcat regarding the google-services.json
.
我不小心删除了另一个解决方案
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
添加这个并工作,用于新解决方案
[来自产品经理@Google]
您可以按照以下步骤为您的 Android 应用下载并复制 google-services.json
文件来解决此问题:
- Select 您的 app/project 名称和 Android 来自 this link 的软件包名称,然后单击
Continue to Choose and configure services
。
- 点击
Continue to Generate Configuration files
。
- 下载
google-services.json
并将文件复制到 Android 项目中的 app/
或 mobile/
模块目录。
如果您之前已将 Google 项目导入 Firebase,则可以
从项目设置下的 Firebase console 获取更新的 google-services.json。
不要按照其他答案之一的建议复制 PROJECT_NUMBER 因为 google_app_id 指的是项目中的应用程序而不是项目本身。
对于那些即使在正确设置播放服务并将 google-services.json
文件放入 project/app
文件夹后仍面临此问题的人,实际的解决方案是
Build > Rebuild Project
可能是因为 json 文件中的字符串在执行完全重建之前未集成到编译资源中。
您可以在 Gradle 文件中更改应用的 versionCode
和 versionName
。
我使用的快速修复 是禁用签名。如果您不想创建生产代码,您可以将构建变体设置为调试、localOldDebug 或 localDebug。
单击 Android Studio 左下角的“构建变体”。
how-to-disable-generated-signed-apk-android-studio
对于 Xamarin/Visual Studio Mac 我需要将其添加到我的 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
我在 android 应用程序中使用 google 地图。我已经创建了密钥并在清单文件中添加了必要的权限。但是很快我启动了应用程序,我在调试器中收到了这条消息:
GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
我不确定出了什么问题。地图工作正常,我可以毫无问题地使用它。我的 gradle 文件有这个条目:
compile 'com.google.android.gms:play-services:8.4.0'
它在抱怨什么,我该如何缓解?
您需要放置由developer.google.com, as mentioned in the 2nd step of the official docs here
生成的配置文件(google-services.json
)
过程简单
- 您可以select您的项目或创建一个新项目。
然后在 selecting 所需的服务(在本例中为地图服务)之后,您可以生成配置文件。
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步here
Copy the
google-services.json
file you just downloaded into the app/ or mobile/ directory of your Android
P.S :对于反对这个答案的人,请务必发表评论说明你为什么反对它。
这也发生在我身上。在我的例子中,这是因为 Android studio 试图在我的主 activity 中插入一些代码。删除代码修复了错误
插入的代码是关于 App Indexing 的:
在我的例子中,这个错误的原因是 Google Gradle 的服务插件和 Play 服务库是不兼容的版本。兼容版本说明:
1) 将依赖项添加到您的 project-level build.gradle:
classpath 'com.google.gms:google-services:1.5.0-beta2'
2) 将插件添加到您的 app-level build.gradle:
apply plugin: 'com.google.gms.google-services'
3) 如果您使用的是 Android Studio,这是要添加到应用程序 build.gradle 文件的依赖项部分的字符串:
dependencies {
compile "com.google.android.gms:play-services:8.3.0"
}
来源:https://developers.google.com/cloud-messaging/android/client
我遇到了同样的问题并按照 official solution.
解决了它步骤如下:
从this link获取配置文件
google-services.json
。将刚刚下载的
google-services.json
文件复制到 Android Studio 项目的 app/ 或 mobile/ 目录中。将依赖项添加到您的项目级别 build.gradle:
classpath 'com.google.gms:google-services:1.5.0-beta2'
将插件添加到您的应用级别 build.gradle:
apply plugin: 'com.google.gms.google-services'
将此依赖项添加到您的应用级别 build.gradle:
dependencies { compile "com.google.android.gms:play-services:8.3.0" }
我当时也有同样的问题。能够通过仅使用必要的播放服务库来解决它,在我的例子中是 GCM。
我使用 com.google.android.gms:play-services-gcm:8.4.0
而不是 com.google.android.gms:play-services:8.4.0
。有关详细信息,请参阅 here;这也解决了 multidex 问题。
然后我应用@Radix 和@Alexander 的方法来删除消息 GCM has been output in the logcat regarding the google-services.json
.
我不小心删除了另一个解决方案
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
添加这个并工作,用于新解决方案
[来自产品经理@Google]
您可以按照以下步骤为您的 Android 应用下载并复制 google-services.json
文件来解决此问题:
- Select 您的 app/project 名称和 Android 来自 this link 的软件包名称,然后单击
Continue to Choose and configure services
。 - 点击
Continue to Generate Configuration files
。 - 下载
google-services.json
并将文件复制到 Android 项目中的app/
或mobile/
模块目录。
如果您之前已将 Google 项目导入 Firebase,则可以 从项目设置下的 Firebase console 获取更新的 google-services.json。
不要按照其他答案之一的建议复制 PROJECT_NUMBER 因为 google_app_id 指的是项目中的应用程序而不是项目本身。
对于那些即使在正确设置播放服务并将 google-services.json
文件放入 project/app
文件夹后仍面临此问题的人,实际的解决方案是
Build > Rebuild Project
可能是因为 json 文件中的字符串在执行完全重建之前未集成到编译资源中。
您可以在 Gradle 文件中更改应用的 versionCode
和 versionName
。
我使用的快速修复 是禁用签名。如果您不想创建生产代码,您可以将构建变体设置为调试、localOldDebug 或 localDebug。
单击 Android Studio 左下角的“构建变体”。
how-to-disable-generated-signed-apk-android-studio
对于 Xamarin/Visual Studio Mac 我需要将其添加到我的 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