默认 FirebaseApp 未初始化
Default FirebaseApp is not initialized
我们在刚刚添加了 Firebase 远程配置的 Android 应用中看到消息 Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.
出现一些异常。
堆栈跟踪如下:
Fatal Exception: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(Unknown Source)
at com.example.app.fragments.SomeFragment.updateFooter(SourceFile:295)
at com.example.app.fragments.SomeFragment.onCreateView(SourceFile:205)
at android.support.v4.app.Fragment.performCreateView(SourceFile:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1290)
at android.support.v4.app.BackStackRecord.run(SourceFile:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(SourceFile:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(SourceFile:679)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(SourceFile:143)
at android.support.v4.view.ViewPager.populate(SourceFile:1240)
at android.support.v4.view.ViewPager.populate(SourceFile:1088)
at android.support.v4.view.ViewPager.setAdapter(SourceFile:542)
at com.example.app.SomeActivity.onSomeAsyncCallback(SourceFile:908)
at com.example.app.SomeDataRetriever.onAsyncHttpCompleted(SourceFile:72)
at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:141)
at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:19)
at android.os.AsyncTask.finish(AsyncTask.java:679)
at android.os.AsyncTask.access0(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:696)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5665)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
这是版本 9.6.1,我们还在使用其他 Firebase 组件:
compile 'com.google.firebase:firebase-ads:9.6.1'
compile 'com.google.firebase:firebase-config:9.6.1'
compile 'com.google.firebase:firebase-invites:9.6.1'
compile "com.google.firebase:firebase-messaging:9.6.1"
如我所见from the documentation and the Javadoc在我们的案例中我们不应该进行任何手动初始化。
在各种设备上 Android 4-6 发生异常。
编辑:
我看到这个问题得到了一点关注。我认为这个解释对你们中的一些人来说可能很有趣:https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html
确保添加到您的根级别 build.gradle
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.3.2'
}
}
然后,在您的模块级别 Gradle 文件(通常是 app/build.gradle)中,在文件底部添加 'apply plugin' 行以启用Gradle 插件:
apply plugin: 'com.android.application'
android {
// ...
}
dependencies {
// ...
implementation 'com.google.firebase:firebase-core:9.6.1'
// Getting a "Could not find" error? Make sure you have
// the latest Google Repository in the Android SDK manager
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
如documentation所述。当我忘记在我的 gradle 文件中添加这个时,我遇到了上面的问题。
我前段时间遇到过同样的问题。
您试图在不初始化的情况下获取 Firebase 实例。
在您尝试获取 Firebase 实例之前,请在您的主函数或 FutureBuilder 中添加以下代码行:
FirebaseApp.initializeApp();
首先您需要在根级别添加 com.google.gms:google-services:x.x.x build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
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
}
}
之后你需要应用插件:'com.google.gms.google-services' at app/build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.google.android.gms:play-services-gcm:9.8.0'
compile 'com.google.android.gms:play-services-maps:9.8.0'
compile 'com.google.android.gms:play-services-location:9.8.0'
compile 'com.google.firebase:firebase-messaging:9.8.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
如果您仍然遇到问题,那么您需要添加
FirebaseApp.initializeApp(this);
就在你打电话之前
FirebaseInstanceId.getInstance().getToken();
虽然用 FirebaseApp.initializeApp(this);
手动初始化 Firebase 使错误消失,但它并没有解决根本原因,一些奇怪的问题似乎并没有得到解决,例如
- FCM 需要
com.google.android.c2dm.permission.RECEIVE
许可
仅适用于 GCM
- 令牌在发送第一个通知后取消注册
- 未收到消息/永远不会调用 onMessageReceived(),
使用较新的 Gradle 插件(例如 Android 插件 2.2.3 和 Gradle 2.14.1)修复了所有问题。 (当然设置必须根据 Firebase documentation 正确)
我们必须在应用程序的 onCreate 函数中初始化 Firebase Class。
package com.rocks.music.videoplayer;
import android.app.Application;
import android.content.Context;
import com.google.firebase.FirebaseApp;
/**
* Created by ashish123 on 22/8/15.
*/
public class MyApplication extends Application {
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
try {
FirebaseApp.initializeApp(this);
}
catch (Exception e) {
}
}
public static Context getInstance() {
return mInstance;
}
}
清单文件中的代码:-
<application
android:name="com.rocks.music.videoplayer.MyApplication"
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
发生这种情况的原因之一可能是忘记在 AndroidManifest.xml
中添加 android.permission.INTERNET
权限
<uses-permission android:name="android.permission.INTERNET" />
您需要在 build.gradle(项目级)
中添加 Firebase Gradle buildscript 依赖项
classpath 'com.google.gms:google-services:3.1.0'
并在 app/build 中为 Gradle 添加 Firebase 插件。gradle
apply plugin: 'com.google.gms.google-services'
build.gradle will include these new dependencies:
compile 'com.google.firebase:firebase-database:11.0.4'
来源:Android工作室助手
如果您正在使用 FirebaseUI, no need of FirebaseApp.initializeApp(this);
in your code according the sample。
确保添加到您的 root-level build.gradle :
buildscript {
repositories {
google()
jcenter()
}
dependencies {
...
classpath 'com.google.gms:google-services:3.1.1'
...
}
}
然后,在您的 模块级别 Gradle 文件中:
dependencies {
...
// 1 - Required to init Firebase automatically (THE MAGIC LINE)
implementation "com.google.firebase:firebase-core:11.6.2"
// 2 - FirebaseUI for Firebase Auth (Or whatever you need...)
implementation 'com.firebaseui:firebase-ui-auth:3.1.2'
...
}
apply plugin: 'com.google.gms.google-services'
就是这样。不需要更多了。
我的 app/build.gradle 文件
中缺少以下行
apply plugin: 'com.google.gms.google-services'
一次清理项目,然后再次运行。这对我来说已经解决了。
此过程没有解决我的问题
FirebaseApp.initializeApp(this);
所以我尝试了其他方法,现在我的 firebase 已成功初始化。尝试在应用中添加以下内容 module.gradle
BuildScript{
dependencies {..
classpath : "com.google.firebase:firebase-plugins:1.1.5"
..}
}
dependencies {...
implementation : "com.google.firebase:firebase-perf:16.1.0"
implementation : "com.google.firebase:firebase-core:16.0.3"
..}
看来 google-services:4.1.0
有问题。将其降级为
classpath 'com.google.gms:google-services:4.0.0'
或升级到
classpath 'com.google.gms:google-services:4.2.0'
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
classpath 'com.google.gms:google-services:4.2.0'
/*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}
希望对您有所帮助
在我的例子中,Google 服务 gradle 插件没有从 google-services.json
文件生成所需的 values.xml
文件。 Firebase 库使用这个生成的值文件来初始化自己,如果找不到值文件,它似乎不会抛出错误。检查值文件是否存在于以下位置并使用 google-sevices.json
文件中的适当字符串进行填充:
app/build/generated/res/google-services/{build_type}/values/values.xml
and/or
app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml
详情请见:
https://developers.google.com/android/guides/google-services-plugin
我的特殊情况是由于使用的 gradle 工具版本对于我 运行ning 的 Android Studio 版本来说太高级了(即确保你 运行 对工具 v3.2.X-YYY 使用 Android Studio v3.2).
另一种可能的解决方案 - 如果您使用的是某些测试版,请尝试不同的 Android Studio。对我有帮助。新 Android Studio 根本没有正确添加 Firebase。在我的例子中 3.3preview
经过更多调查,我发现问题是新的 Android 工作室使用较新的 Google 服务版本启动项目,看起来这是最初的问题。正如@Ammar Bukhari 所建议的那样,此更改有所帮助:
类路径'com.google.gms:google-services:4.1.0' ->
类路径 'com.google.gms:google-services:4.0.0'
单击“工具”>“Firebase”打开助手window。
单击以展开所列功能之一(例如 Analytics),然后单击提供的教程 link(例如记录 Analytics 事件)。
单击“连接到 Firebase”按钮以连接到 Firebase 并将必要的代码添加到您的应用程序。
更新各种依赖项后,我在编译时遇到了 Crashlytics 错误,'Crashlytics found an invalid API key: null. Check the Crashlytics plugin to make sure that the application has been added successfully! Contact support@fabric.io for assistance.' 我从反复尝试支持@fabric.io 得到的一个非自动响应错误将您引导至 Fabric和 Crashlytics 是独立的团队,所以他们帮不了我。我避免为 Crashlytics 实现额外的 Fabric 层,并且无法从 Fabric 站点获取新密钥,甚至无法让站点识别我。在尝试通过从我的代码中删除 Crashlytics 来解决这个问题时,我在 运行.
中遇到了 'Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first' 崩溃
我从来不需要添加 'FirebaseApp.initializeApp(this)' 的初始化行,事实上,它已经被注释掉了。文档甚至提到如果只对一个 activity 使用 Firebase 就不需要这个。添加它没有任何区别,仍然出现 运行 终止错误。
原来导致新的模糊错误的原因是更新的 google-services 依赖项。现在,我没有时间花更多的时间来尝试修复新依赖项导致的霰弹枪错误,所以在有人提出解决方案之前,我会坚持使用旧版本。除了奇怪的初始化崩溃之外,新版本可能会迫使 Fabric on Crashlytics 用户。为此,用户也被迫回到旧的依赖版本:Crashlytics found an invalid API key: null. after updated com.google.gms:google-services:4.1.0
//com.google.gms:google-services:4.1.0 BAD
com.google.gms:google-services:4.0.1//GOOD
编辑 10/17/18:再次更新以下依赖项后
implementation 'com.google.firebase:firebase-ads:17.0.0'
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.4
我在使用 'xxx has unexpectedly closed' 尝试安装时立即崩溃,就像我尝试 google-services 依赖项更新时一样。深入研究日志,我发现 link 指示我将其添加到清单中
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxx~xxxxxx"/>
这是新的,这里的设置和插播说明中没有提到 https://firebase.google.com/docs/android/setup and here https://developers.google.com/admob/android/interstitial。
我过去只需为我的应用程序处理一个与广告相关的 ID,即 INTERSTITIAL_UNIT_ID。现在需要处理两个。除了上述添加之外,文档还指示在此处添加 ADMOB_APP_ID(与新清单代码中的 ads.APPLICATION_ID 绑定的相同数字)
MobileAds.initialize(this, ADMOB_APP_ID);
可以在您的 Google AdMob 控制台中找到 INTERSTITIAL_UNIT_ID 和 ADMOB_APP_ID ID。我的游戏应用程序在我第一次更新 firebase 依赖项时停止投放广告并且仍然不投放广告,在
中给出错误代码 0
public void onAdFailedToLoad(int errorCode){...
即使在所有这些添加混乱之后,我仍然无法在没有初始化错误 运行 崩溃的情况下更新 google-services 依赖项。我希望在 google-services:4.0.1 停留一段时间。
编辑 10/24/18:来自 mobileadssdk-advisor+support@google.com 经过数周关于更新后未收到广告服务的通信:
'Thanks for sharing the device logs. From the logs, it looks like an existing issue and this is on our priority list and our team is working on the fix and this is only happening on Android O and P devices.'
只有 O 和 P 设备?这是最后两个版本,O于2017年9月25日发布。哎呀。
21 年 8 月 8 日更新:将 google-服务从 4.3.8 更新到 4.3.9 后,尽管确实调用了初始化(尽管据称不需要)。将不得不再次延迟更新实施:
//com.google.gms:google-services:4.3.9 BAD
com.google.gms:google-services:4.3.8//GOOD
正如@PSIXO 在评论中提到的,这可能是 google-services 的依赖版本的问题。为我改变,
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.1.0'
}
}
到
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.0.1'
}
}
worked.There 可能是 4.1.0 版本的问题。因为我在这上面浪费了很多时间,所以我想写这个作为答案。
更新(2021 年 11 月 11 日):
我们 不需要在任何地方手动调用 FirebaseApp.initializeApp(this);
。我们也不应该。
可能性一:
看来 com.google.gms:google-services:4.3.9
有问题。
我们可以升级到
classpath 'com.google.gms:google-services:4.3.10'
或降级至
classpath 'com.google.gms:google-services:4.3.8'
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.google.gms:google-services:4.3.9'// <-- this was the problem
}
可能性2:
确保在 app/build.gradle 文件中添加以下行
apply plugin: 'com.google.gms.google-services'
然后 清理 项目并再次 运行。它对我有用。
可能性3:
我刚遇到同样的问题,得到了一个意想不到的奇怪解决方案。
来自this answer:
我已经删除了 tools:node="replace"
并且效果很好。
classpath 'com.google.gms:google-services:4.1.0'
有问题。而是使用:
classpath 'com.google.gms:google-services:4.2.0'
发生这种情况的原因是 com.google.gms:google-services version.When 我使用的是 4.1.0,我遇到了同样的错误。然后我降级版本。
之前
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.1.0'
之后
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:3.2.0'
希望,它会解决错误。
我猜 google-services 版本和 firebase 版本存在兼容性问题。
我在项目的 build.gradle 文件中更改了依赖项
类路径 'com.google.gms:google-services:4.1.0' 到 4.2.0
然后将模块的 build.gradle 依赖项更新为:
实施'com.google.firebase:firebase-database:16.0.6'
实施'com.google.firebase:firebase-core:16.0.7'
一切都很顺利,无需键入 FirebaseApp.initializeApp(this);
如果您最近将 Android Studio 更新到 3.3.1 并且 com.google.gms:google-services (Below 4.2.0) dependencies 所以请更新 com.google.gms:google-服务到 4.2.0.
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.google.gms:google-services:4.2.0'
}
通过 Android Studio 工具安装 Firebase...Firebase...
我通过 Android Studio 的内置工具进行了安装(遵循 Firebase 的最新文档)。
这安装了基本的依赖项,但是当我尝试连接到数据库时,它总是给我一个错误,我需要先调用初始化,即使我是:
Default FirebaseApp is not initialized in this process . Make sure to
call FirebaseApp.initializeApp(Context) first.
无论我做什么,我都会遇到这个错误。
最后,在看到其他答案之一的评论后,我将 gradle 中的以下内容从版本 4.1.0 更改为:
classpath 'com.google.gms:google-services:4.0.1'
当我这样做时,我终于看到了一个对我有帮助的错误:
File google-services.json is missing. The Google Services Plugin
cannot function without it. Searched Location:
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\debug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\nullnull\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnullDebug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\google-services.json
这就是问题所在。似乎 4.1.0 版本由于某种原因没有给出构建错误 - 没有提到您缺少 google-services.json 文件。我的应用程序中没有 google-services.json 文件,所以我出去添加了它。
但由于这是使用现有实时 firsbase 数据库的升级,我过去从未生成过该文件。
我去了 firebase 并生成了它并添加了它,它解决了问题。
改回 4.1.0
一旦我发现了所有这些,然后我将类路径变量改回(到 4.1.0)并重新构建,它再次崩溃,并显示尚未初始化的错误。
根问题
- 使用 4.1.0 构建不会在预编译时为您提供有效错误,因此您可能不知道发生了什么。
- 运行 针对 4.1.0 导致初始化错误。
对我来说,这是将 com.google.gms:google-services inside build.gradle 的依赖升级到
buildscript {
repositories {
jcenter()
mavenCentral()
maven {
url 'https://maven.google.com/'
name 'Google'
}
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
使用 com.google.gms:google-services:4.0.1' 而不是 4.1.0
改变
classpath 'com.google.gms:google-services:4.1.0'
至
classpath 'com.google.gms:google-services:4.0.1'
适合我
按照@Gabriel Lidenor 的回答,使用上下文初始化应用程序在我的情况下不起作用。如果您尝试在没有 google-service.json 的情况下创建 firebase-app 怎么办?所以在初始化任意数量的firebase app之前,首先需要初始化为;
FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("APP_ID")
.setGcmSenderId("SENDER_ID").build();
FirebaseApp.initializeApp(context, options, "[DEFAULT]");
如果您正在使用 Xamarin 并来到这里寻找此问题的解决方案,here it's from Microsoft:
In some cases, you may see this error message:
Java.Lang.IllegalStateException: Default FirebaseApp is not
initialized in this process Make sure to call
FirebaseApp.initializeApp(Context) first.
This is a known problem that you can work around by cleaning the
solution and rebuilding the project (Build > Clean Solution, Build >
Rebuild Solution).
我将项目 gms:google_services 降级为
类路径 'com.google.gms:google-services:4.0.1'
它对我有用。
在我的例子中,这个错误发生在我升级我的 Google gms 类路径时。
我
classpath 'com.google.gms:google-services:4.3.9'
所以我必须降级到:
classpath 'com.google.gms:google-services:4.3.8'
而且效果很好。
在我的例子中,我在构建 gradle(应用程序)中停用了服务,如下所示:
android.applicationVariants.all { variant ->
def googleTask = tasks.findByName("process${variant.name.capitalize()}GoogleServices")
googleTask.enabled = "mock" != variant.flavorName
}
由于此变体仅用于测试和模拟服务,因此已停用。在将近 2 年的时间里,这对我没有造成任何问题。一旦我在 gradle 文件中注意到它,那一刻就有点紧张...
如果你也这样做,直接删除它。
对我来说,问题是我没有在 app/build.gradle 文件中添加这一行。
apply plugin: 'com.google.gms.google-services'
我解决了这个问题:
应用插件:'com.google.gms.google-services' in build.gradle 文件并将文件 google-service.json 移动到 app 文件夹下。
我们在刚刚添加了 Firebase 远程配置的 Android 应用中看到消息 Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.
出现一些异常。
堆栈跟踪如下:
Fatal Exception: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(Unknown Source)
at com.example.app.fragments.SomeFragment.updateFooter(SourceFile:295)
at com.example.app.fragments.SomeFragment.onCreateView(SourceFile:205)
at android.support.v4.app.Fragment.performCreateView(SourceFile:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1290)
at android.support.v4.app.BackStackRecord.run(SourceFile:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(SourceFile:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(SourceFile:679)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(SourceFile:143)
at android.support.v4.view.ViewPager.populate(SourceFile:1240)
at android.support.v4.view.ViewPager.populate(SourceFile:1088)
at android.support.v4.view.ViewPager.setAdapter(SourceFile:542)
at com.example.app.SomeActivity.onSomeAsyncCallback(SourceFile:908)
at com.example.app.SomeDataRetriever.onAsyncHttpCompleted(SourceFile:72)
at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:141)
at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:19)
at android.os.AsyncTask.finish(AsyncTask.java:679)
at android.os.AsyncTask.access0(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:696)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5665)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
这是版本 9.6.1,我们还在使用其他 Firebase 组件:
compile 'com.google.firebase:firebase-ads:9.6.1'
compile 'com.google.firebase:firebase-config:9.6.1'
compile 'com.google.firebase:firebase-invites:9.6.1'
compile "com.google.firebase:firebase-messaging:9.6.1"
如我所见from the documentation and the Javadoc在我们的案例中我们不应该进行任何手动初始化。
在各种设备上 Android 4-6 发生异常。
编辑:
我看到这个问题得到了一点关注。我认为这个解释对你们中的一些人来说可能很有趣:https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html
确保添加到您的根级别 build.gradle
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.3.2'
}
}
然后,在您的模块级别 Gradle 文件(通常是 app/build.gradle)中,在文件底部添加 'apply plugin' 行以启用Gradle 插件:
apply plugin: 'com.android.application'
android {
// ...
}
dependencies {
// ...
implementation 'com.google.firebase:firebase-core:9.6.1'
// Getting a "Could not find" error? Make sure you have
// the latest Google Repository in the Android SDK manager
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
如documentation所述。当我忘记在我的 gradle 文件中添加这个时,我遇到了上面的问题。
我前段时间遇到过同样的问题。
您试图在不初始化的情况下获取 Firebase 实例。 在您尝试获取 Firebase 实例之前,请在您的主函数或 FutureBuilder 中添加以下代码行:
FirebaseApp.initializeApp();
首先您需要在根级别添加 com.google.gms:google-services:x.x.x build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
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
}
}
之后你需要应用插件:'com.google.gms.google-services' at app/build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.google.android.gms:play-services-gcm:9.8.0'
compile 'com.google.android.gms:play-services-maps:9.8.0'
compile 'com.google.android.gms:play-services-location:9.8.0'
compile 'com.google.firebase:firebase-messaging:9.8.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
如果您仍然遇到问题,那么您需要添加
FirebaseApp.initializeApp(this);
就在你打电话之前
FirebaseInstanceId.getInstance().getToken();
虽然用 FirebaseApp.initializeApp(this);
手动初始化 Firebase 使错误消失,但它并没有解决根本原因,一些奇怪的问题似乎并没有得到解决,例如
- FCM 需要
com.google.android.c2dm.permission.RECEIVE
许可 仅适用于 GCM - 令牌在发送第一个通知后取消注册
- 未收到消息/永远不会调用 onMessageReceived(),
使用较新的 Gradle 插件(例如 Android 插件 2.2.3 和 Gradle 2.14.1)修复了所有问题。 (当然设置必须根据 Firebase documentation 正确)
我们必须在应用程序的 onCreate 函数中初始化 Firebase Class。
package com.rocks.music.videoplayer;
import android.app.Application;
import android.content.Context;
import com.google.firebase.FirebaseApp;
/**
* Created by ashish123 on 22/8/15.
*/
public class MyApplication extends Application {
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
try {
FirebaseApp.initializeApp(this);
}
catch (Exception e) {
}
}
public static Context getInstance() {
return mInstance;
}
}
清单文件中的代码:-
<application
android:name="com.rocks.music.videoplayer.MyApplication"
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
发生这种情况的原因之一可能是忘记在 AndroidManifest.xml
android.permission.INTERNET
权限
<uses-permission android:name="android.permission.INTERNET" />
您需要在 build.gradle(项目级)
中添加 Firebase Gradle buildscript 依赖项classpath 'com.google.gms:google-services:3.1.0'
并在 app/build 中为 Gradle 添加 Firebase 插件。gradle
apply plugin: 'com.google.gms.google-services'
build.gradle will include these new dependencies:
compile 'com.google.firebase:firebase-database:11.0.4'
来源:Android工作室助手
如果您正在使用 FirebaseUI, no need of FirebaseApp.initializeApp(this);
in your code according the sample。
确保添加到您的 root-level build.gradle :
buildscript {
repositories {
google()
jcenter()
}
dependencies {
...
classpath 'com.google.gms:google-services:3.1.1'
...
}
}
然后,在您的 模块级别 Gradle 文件中:
dependencies {
...
// 1 - Required to init Firebase automatically (THE MAGIC LINE)
implementation "com.google.firebase:firebase-core:11.6.2"
// 2 - FirebaseUI for Firebase Auth (Or whatever you need...)
implementation 'com.firebaseui:firebase-ui-auth:3.1.2'
...
}
apply plugin: 'com.google.gms.google-services'
就是这样。不需要更多了。
我的 app/build.gradle 文件
中缺少以下行apply plugin: 'com.google.gms.google-services'
一次清理项目,然后再次运行。这对我来说已经解决了。
此过程没有解决我的问题
FirebaseApp.initializeApp(this);
所以我尝试了其他方法,现在我的 firebase 已成功初始化。尝试在应用中添加以下内容 module.gradle
BuildScript{
dependencies {..
classpath : "com.google.firebase:firebase-plugins:1.1.5"
..}
}
dependencies {...
implementation : "com.google.firebase:firebase-perf:16.1.0"
implementation : "com.google.firebase:firebase-core:16.0.3"
..}
看来 google-services:4.1.0
有问题。将其降级为
classpath 'com.google.gms:google-services:4.0.0'
或升级到
classpath 'com.google.gms:google-services:4.2.0'
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
classpath 'com.google.gms:google-services:4.2.0'
/*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}
希望对您有所帮助
在我的例子中,Google 服务 gradle 插件没有从 google-services.json
文件生成所需的 values.xml
文件。 Firebase 库使用这个生成的值文件来初始化自己,如果找不到值文件,它似乎不会抛出错误。检查值文件是否存在于以下位置并使用 google-sevices.json
文件中的适当字符串进行填充:
app/build/generated/res/google-services/{build_type}/values/values.xml
and/or
app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml
详情请见: https://developers.google.com/android/guides/google-services-plugin
我的特殊情况是由于使用的 gradle 工具版本对于我 运行ning 的 Android Studio 版本来说太高级了(即确保你 运行 对工具 v3.2.X-YYY 使用 Android Studio v3.2).
另一种可能的解决方案 - 如果您使用的是某些测试版,请尝试不同的 Android Studio。对我有帮助。新 Android Studio 根本没有正确添加 Firebase。在我的例子中 3.3preview
经过更多调查,我发现问题是新的 Android 工作室使用较新的 Google 服务版本启动项目,看起来这是最初的问题。正如@Ammar Bukhari 所建议的那样,此更改有所帮助:
类路径'com.google.gms:google-services:4.1.0' -> 类路径 'com.google.gms:google-services:4.0.0'
单击“工具”>“Firebase”打开助手window。
单击以展开所列功能之一(例如 Analytics),然后单击提供的教程 link(例如记录 Analytics 事件)。
单击“连接到 Firebase”按钮以连接到 Firebase 并将必要的代码添加到您的应用程序。
更新各种依赖项后,我在编译时遇到了 Crashlytics 错误,'Crashlytics found an invalid API key: null. Check the Crashlytics plugin to make sure that the application has been added successfully! Contact support@fabric.io for assistance.' 我从反复尝试支持@fabric.io 得到的一个非自动响应错误将您引导至 Fabric和 Crashlytics 是独立的团队,所以他们帮不了我。我避免为 Crashlytics 实现额外的 Fabric 层,并且无法从 Fabric 站点获取新密钥,甚至无法让站点识别我。在尝试通过从我的代码中删除 Crashlytics 来解决这个问题时,我在 运行.
中遇到了 'Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first' 崩溃我从来不需要添加 'FirebaseApp.initializeApp(this)' 的初始化行,事实上,它已经被注释掉了。文档甚至提到如果只对一个 activity 使用 Firebase 就不需要这个。添加它没有任何区别,仍然出现 运行 终止错误。
原来导致新的模糊错误的原因是更新的 google-services 依赖项。现在,我没有时间花更多的时间来尝试修复新依赖项导致的霰弹枪错误,所以在有人提出解决方案之前,我会坚持使用旧版本。除了奇怪的初始化崩溃之外,新版本可能会迫使 Fabric on Crashlytics 用户。为此,用户也被迫回到旧的依赖版本:Crashlytics found an invalid API key: null. after updated com.google.gms:google-services:4.1.0
//com.google.gms:google-services:4.1.0 BAD
com.google.gms:google-services:4.0.1//GOOD
编辑 10/17/18:再次更新以下依赖项后
implementation 'com.google.firebase:firebase-ads:17.0.0'
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.4
我在使用 'xxx has unexpectedly closed' 尝试安装时立即崩溃,就像我尝试 google-services 依赖项更新时一样。深入研究日志,我发现 link 指示我将其添加到清单中
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxx~xxxxxx"/>
这是新的,这里的设置和插播说明中没有提到 https://firebase.google.com/docs/android/setup and here https://developers.google.com/admob/android/interstitial。
我过去只需为我的应用程序处理一个与广告相关的 ID,即 INTERSTITIAL_UNIT_ID。现在需要处理两个。除了上述添加之外,文档还指示在此处添加 ADMOB_APP_ID(与新清单代码中的 ads.APPLICATION_ID 绑定的相同数字)
MobileAds.initialize(this, ADMOB_APP_ID);
可以在您的 Google AdMob 控制台中找到 INTERSTITIAL_UNIT_ID 和 ADMOB_APP_ID ID。我的游戏应用程序在我第一次更新 firebase 依赖项时停止投放广告并且仍然不投放广告,在
中给出错误代码 0public void onAdFailedToLoad(int errorCode){...
即使在所有这些添加混乱之后,我仍然无法在没有初始化错误 运行 崩溃的情况下更新 google-services 依赖项。我希望在 google-services:4.0.1 停留一段时间。
编辑 10/24/18:来自 mobileadssdk-advisor+support@google.com 经过数周关于更新后未收到广告服务的通信:
'Thanks for sharing the device logs. From the logs, it looks like an existing issue and this is on our priority list and our team is working on the fix and this is only happening on Android O and P devices.'
只有 O 和 P 设备?这是最后两个版本,O于2017年9月25日发布。哎呀。
21 年 8 月 8 日更新:将 google-服务从 4.3.8 更新到 4.3.9 后,尽管确实调用了初始化(尽管据称不需要)。将不得不再次延迟更新实施:
//com.google.gms:google-services:4.3.9 BAD
com.google.gms:google-services:4.3.8//GOOD
正如@PSIXO 在评论中提到的,这可能是 google-services 的依赖版本的问题。为我改变,
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.1.0'
}
}
到
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.0.1'
}
}
worked.There 可能是 4.1.0 版本的问题。因为我在这上面浪费了很多时间,所以我想写这个作为答案。
更新(2021 年 11 月 11 日):
我们 不需要在任何地方手动调用 FirebaseApp.initializeApp(this);
。我们也不应该。
可能性一:
看来 com.google.gms:google-services:4.3.9
有问题。
我们可以升级到
classpath 'com.google.gms:google-services:4.3.10'
或降级至
classpath 'com.google.gms:google-services:4.3.8'
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.google.gms:google-services:4.3.9'// <-- this was the problem
}
可能性2:
确保在 app/build.gradle 文件中添加以下行
apply plugin: 'com.google.gms.google-services'
然后 清理 项目并再次 运行。它对我有用。
可能性3:
我刚遇到同样的问题,得到了一个意想不到的奇怪解决方案。
来自this answer:
我已经删除了 tools:node="replace"
并且效果很好。
classpath 'com.google.gms:google-services:4.1.0'
有问题。而是使用:
classpath 'com.google.gms:google-services:4.2.0'
发生这种情况的原因是 com.google.gms:google-services version.When 我使用的是 4.1.0,我遇到了同样的错误。然后我降级版本。 之前
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.1.0'
之后
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:3.2.0'
希望,它会解决错误。
我猜 google-services 版本和 firebase 版本存在兼容性问题。
我在项目的 build.gradle 文件中更改了依赖项
类路径 'com.google.gms:google-services:4.1.0' 到 4.2.0
然后将模块的 build.gradle 依赖项更新为:
实施'com.google.firebase:firebase-database:16.0.6'
实施'com.google.firebase:firebase-core:16.0.7'
一切都很顺利,无需键入 FirebaseApp.initializeApp(this);
如果您最近将 Android Studio 更新到 3.3.1 并且 com.google.gms:google-services (Below 4.2.0) dependencies 所以请更新 com.google.gms:google-服务到 4.2.0.
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.google.gms:google-services:4.2.0'
}
通过 Android Studio 工具安装 Firebase...Firebase...
我通过 Android Studio 的内置工具进行了安装(遵循 Firebase 的最新文档)。 这安装了基本的依赖项,但是当我尝试连接到数据库时,它总是给我一个错误,我需要先调用初始化,即使我是:
Default FirebaseApp is not initialized in this process . Make sure to call FirebaseApp.initializeApp(Context) first.
无论我做什么,我都会遇到这个错误。
最后,在看到其他答案之一的评论后,我将 gradle 中的以下内容从版本 4.1.0 更改为:
classpath 'com.google.gms:google-services:4.0.1'
当我这样做时,我终于看到了一个对我有帮助的错误:
File google-services.json is missing. The Google Services Plugin cannot function without it. Searched Location: C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\debug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\nullnull\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnullDebug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\google-services.json
这就是问题所在。似乎 4.1.0 版本由于某种原因没有给出构建错误 - 没有提到您缺少 google-services.json 文件。我的应用程序中没有 google-services.json 文件,所以我出去添加了它。
但由于这是使用现有实时 firsbase 数据库的升级,我过去从未生成过该文件。 我去了 firebase 并生成了它并添加了它,它解决了问题。
改回 4.1.0
一旦我发现了所有这些,然后我将类路径变量改回(到 4.1.0)并重新构建,它再次崩溃,并显示尚未初始化的错误。
根问题
- 使用 4.1.0 构建不会在预编译时为您提供有效错误,因此您可能不知道发生了什么。
- 运行 针对 4.1.0 导致初始化错误。
对我来说,这是将 com.google.gms:google-services inside build.gradle 的依赖升级到
buildscript {
repositories {
jcenter()
mavenCentral()
maven {
url 'https://maven.google.com/'
name 'Google'
}
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
使用 com.google.gms:google-services:4.0.1' 而不是 4.1.0
改变
classpath 'com.google.gms:google-services:4.1.0'
至
classpath 'com.google.gms:google-services:4.0.1'
适合我
按照@Gabriel Lidenor 的回答,使用上下文初始化应用程序在我的情况下不起作用。如果您尝试在没有 google-service.json 的情况下创建 firebase-app 怎么办?所以在初始化任意数量的firebase app之前,首先需要初始化为;
FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("APP_ID")
.setGcmSenderId("SENDER_ID").build();
FirebaseApp.initializeApp(context, options, "[DEFAULT]");
如果您正在使用 Xamarin 并来到这里寻找此问题的解决方案,here it's from Microsoft:
In some cases, you may see this error message: Java.Lang.IllegalStateException: Default FirebaseApp is not initialized in this process Make sure to call FirebaseApp.initializeApp(Context) first.
This is a known problem that you can work around by cleaning the solution and rebuilding the project (Build > Clean Solution, Build > Rebuild Solution).
我将项目 gms:google_services 降级为 类路径 'com.google.gms:google-services:4.0.1' 它对我有用。
在我的例子中,这个错误发生在我升级我的 Google gms 类路径时。 我
classpath 'com.google.gms:google-services:4.3.9'
所以我必须降级到:
classpath 'com.google.gms:google-services:4.3.8'
而且效果很好。
在我的例子中,我在构建 gradle(应用程序)中停用了服务,如下所示:
android.applicationVariants.all { variant ->
def googleTask = tasks.findByName("process${variant.name.capitalize()}GoogleServices")
googleTask.enabled = "mock" != variant.flavorName
}
由于此变体仅用于测试和模拟服务,因此已停用。在将近 2 年的时间里,这对我没有造成任何问题。一旦我在 gradle 文件中注意到它,那一刻就有点紧张...
如果你也这样做,直接删除它。
对我来说,问题是我没有在 app/build.gradle 文件中添加这一行。
apply plugin: 'com.google.gms.google-services'
我解决了这个问题: 应用插件:'com.google.gms.google-services' in build.gradle 文件并将文件 google-service.json 移动到 app 文件夹下。