默认FirebaseApp未初始化

Roy*_*erg 214 android firebase-remote-config

我们Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.在Android应用中看到了一些例外情况,我们刚刚添加了Firebase远程配置.

堆栈跟踪如下:

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.access$500(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)
Run Code Online (Sandbox Code Playgroud)

这是版本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"
Run Code Online (Sandbox Code Playgroud)

正如我从文档Javadoc中看到的那样,我们不应该在我们的情况下进行任何手动初始化.

Android 4-6在各种设备上发生异常.

编辑:

我看到这个问题引起了一些关注.我认为这个解释对你们中的一些人来说很有意思:https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html

小智 305

确保添加到根级build.gradle

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.3.2'
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,在模块级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'
Run Code Online (Sandbox Code Playgroud)

文档中所述.忘记在我的gradle文件中添加它时,我在上面的问题中有例外.

  • 在我的情况下没有帮助.这个问题影响极小的玩家.因此,很可能它不是代码或设置的问题,而是与用户设备上安装的播放服务的互操作性:-( (6认同)
  • 'apply plugin:'com.google.gms.google-services'为我工作. (3认同)
  • 为什么插件必须是最后一个? (3认同)
  • 太棒了..分析得很好 (3认同)
  • 检查并检查.刚刚验证了我们所描述的Gradle文件.我应该提一下,代码适用于*很多用户. (2认同)
  • 谢谢:)我忘了在build.gradle中添加这一行:apply plugin:'com.google.gms.google-services' (2认同)

Gab*_*nor 125

我前段时间遇到过同样的问题.

您尝试在不初始化的情况下获取Firebase的实例.在尝试获取Firebase实例之前,请添加以下代码行:

FirebaseApp.initializeApp(this);
Run Code Online (Sandbox Code Playgroud)

  • 重要的是要注意,除了这个初始化,谷歌服务必须按如下方式应用:`apply plugin:'com.google.gms.google-services'` (59认同)
  • 对我来说,版本com.google.gms:google-services:4.1.0崩溃,但com.google.gms:google-services:4.0.1工作 (21认同)
  • 嗯,这对我来说不起作用(生产中仍有一些崩溃).我将尝试将init代码添加到我的第一个Activity onCreate()中. (4认同)
  • @Henrik仅在ExtendedApplication.onCreate()中. (3认同)
  • 我必须在哪个文件中添加这个 MainActivity.java? (3认同)
  • 在我的项目的 build.gradle 中将 google 服务插件从 4.1.0 版降级到 4.0.1 版对我有用。类路径 'com.google.gms:google-services:4.0.1' (2认同)
  • 伙计们,我们到底应该把 `FirebaseApp.initializeApp(this);` 放在哪个文件中??? (2认同)

Amm*_*ari 122

这似乎google-services:4.1.0有一个问题.要么将其降级为

classpath 'com.google.gms:google-services:4.0.0'
Run Code Online (Sandbox Code Playgroud)

或升级到

classpath 'com.google.gms:google-services:4.2.0'
Run Code Online (Sandbox Code Playgroud)
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 */
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你

  • 更新到4.2.0为我解决了这个问题 (11认同)
  • 它对我也有用.似乎4.1.0存在一些问题 (9认同)
  • 根据[问题跟踪器](https://issuetracker.google.com/issues/112716914)将在4.2.0中修复 (4认同)
  • 天哪,从字面上看,花了2周以上的时间才能使它正常工作,我要做的就是将其从4.1.0降级到4.0.0。谢谢吨 (2认同)

Rum*_*tel 49


更新(2021 年 8 月 11 日):


我们不需要手动调用FirebaseApp.initializeApp(this);任何地方。我们不应该太。

可能性一:

好像com.google.gms:google-services:4.3.9有问题。

我们可以将其升级

classpath 'com.google.gms:google-services:4.3.10'
Run Code Online (Sandbox Code Playgroud)

或将其降级

classpath 'com.google.gms:google-services:4.3.8'
Run Code Online (Sandbox Code Playgroud)
dependencies {
    classpath 'com.android.tools.build:gradle:4.2.2'
    classpath 'com.google.gms:google-services:4.3.9'// <-- this was the problem
}
Run Code Online (Sandbox Code Playgroud)

可能性2:

我刚刚遇到了同样的问题,并得到了一个意想不到的奇怪解决方案。

这个答案:

我已经删除了tools:node="replace",它的工作就像魅力一样。

  • 降级到类路径 'com.google.gms:google-services:4.3.8' 为我解决了这个问题。十分感谢! (9认同)
  • 升级到 4.3.10 对我有用 (5认同)
  • 从 4.3.9 降到 4.3.8 也为我解决了这个问题,谢谢!我记得从 4.0.x 升级到 4.1.0 也有一个非常相似的问题,令人困惑的是 google-services 版本完全破坏项目的频率。 (3认同)

var*_*jsi 48

我在app/build.gradle文件中缺少以下行

apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)

一旦清理项目并再次运行.这为我解决了这个问题.


Alp*_*nel 17

    classpath 'com.google.gms:google-services:4.1.0'
Run Code Online (Sandbox Code Playgroud)

有问题.改为使用:

    classpath 'com.google.gms:google-services:4.2.0'
Run Code Online (Sandbox Code Playgroud)


ank*_*ngh 14

首先,您需要在root级别build.gradle中添加com.google.gms:google-services:xxx

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
}
Run Code Online (Sandbox Code Playgroud)

}

之后,您需要在app/build.gradle中应用插件:'com.google.gms.google-services'

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'
Run Code Online (Sandbox Code Playgroud)

如果你仍然遇到问题,那么你需要添加

FirebaseApp.initializeApp(this);
Run Code Online (Sandbox Code Playgroud)

就在你打电话之前

FirebaseInstanceId.getInstance().getToken();
Run Code Online (Sandbox Code Playgroud)


And*_*der 11

在更新各种依赖项后,我在编译中遇到了Crashlytics错误,'Crashlytics发现了一个无效的API密钥:null.检查Crashlytics插件以确保已成功添加应用程序!联系support@fabric.io寻求帮助.我从反复尝试发送的一个非自动响应中发现,错误指示您的错误是Fabric和Crashlytics是独立的团队,所以他们无法帮助我.我已经避免将额外的Fabric层实现到Crashlytics,并且无法从Fabric站点获取新密钥,甚至无法让站点识别我.在尝试通过从我的代码中删除Crashlytics来解决这个问题时,我得到了'默认FirebaseApp未在此过程中初始化com.example.app.确保在运行中首先调用FirebaseApp.initializeApp(Context)崩溃.

我从来没有必要添加'FirebaseApp.initializeApp(this)'的初始化行,事实上已将其注释掉了.文档甚至提到如果仅将Firebase用于一个活动,则不需要此操作.添加它没有任何区别,仍然有运行查杀错误.

结果是导致新的模糊错误的原因是更新的谷歌服务依赖性.现在我没有时间花更多时间来修复新依赖导致的霰弹枪错误,所以在有人提出解决方案之前我会坚持使用旧版本.除了奇怪的初始化崩溃,新版本可能会迫使Crashlytics用户使用Fabric.用户也被迫回到旧的依赖版本:Crashlytics发现了一个无效的API密钥:null.更新后的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
Run Code Online (Sandbox Code Playgroud)

编辑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
Run Code Online (Sandbox Code Playgroud)

我在安装尝试时立即崩溃'xxx意外关闭',就像我尝试google-services依赖关系更新一样.挖掘日志我找到了一个链接,指示我将其添加到清单中

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxx~xxxxxx"/>
Run Code Online (Sandbox Code Playgroud)

这是新功能,在https://firebase.google.com/docs/android/setuphttps://developers.google.com/admob/android/interstitial中的设置和插页式说明中未提及.

我以前只需要为我的应用处理一个与广告相关的ID,即INTERSTITIAL_UNIT_ID.现在需要处理两个问题.除了上面添加的内容之外,文档还指示在此处添加ADMOB_APP_ID(与新清单代码中的ads.APPLICATION_ID相同的数字)

MobileAds.initialize(this, ADMOB_APP_ID);
Run Code Online (Sandbox Code Playgroud)

可以在您的Google AdMob控制台中挖掘INTERSTITIAL_UNIT_ID和ADMOB_APP_ID ID.我的游戏应用程序在我第一次更新firebase依赖项时停止投放广告,但仍然没有投放广告,在广告中提供错误代码0

public void onAdFailedToLoad(int errorCode){...
Run Code Online (Sandbox Code Playgroud)

即使在所有这些混乱之后,我仍然无法在没有初始化错误运行崩溃的情况下更新google-services依赖项.我预计会坚持使用谷歌服务:4.0.1一段时间.

编辑10/24/18:来自mobileadssdk-advisor+support@google.com的电子邮件数周之后,在更新后未获得广告服务:

'感谢您分享设备日志.从日志中看,它看起来像是一个现有的问题,这是我们的优先级列表,我们的团队正在进行修复,这只发生在Android O和P设备上.

只有O和P设备?这是最后两个版本,O于2017年9月25日发布.Yikes.

  • 我也经历过这种情况.希望Google每次推送更新时都不会破坏我的应用版本. (3认同)

Aay*_*gla 9

正如@PSIXO在评论中所提到的,这可能是google-services依赖版本的问题.对我而言,

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.1.0'
    }
}
Run Code Online (Sandbox Code Playgroud)

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.0.1'
    }
}
Run Code Online (Sandbox Code Playgroud)

4.1.0版本可能存在一些问题.因为我浪费了很多时间,所以我想把它写成答案.

  • 将所有版本更新到最新版本(4.2.0)之后,我就解决了这个问题 (3认同)

小智 9

对我来说,问题是我没有在 app/build.gradle 文件中添加这一行。

apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)


J. *_*Doe 9

我在 2023 年创建了我的 Android 项目,它与其他答案略有不同。

  1. 你不需要给FirebaseApp.initializeApp(this)自己打电话
  2. 你不需要<uses-permission android:name="android.permission.INTERNET" />
  3. 转到项目的 build.gradle 并id 'com.google.gms.google-services' version '4.3.15' apply false在下面添加plugins {
  4. 转到模块的 build.gradle 并id 'com.google.gms.google-services'在下面添加plugins {


Phi*_*hil 7

如果您正在使用FirebaseUI,没有必要FirebaseApp.initializeApp(this);在你的代码,根据样品.

确保添加到根级 build.gradle:

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        ...
        classpath 'com.google.gms:google-services:3.1.1'
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的模块级 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'
Run Code Online (Sandbox Code Playgroud)

而已.不需要更多.


Art*_*ioo 5

您需要在build.gradle(项目级)中添加Firebase Gradle构建脚本依赖

classpath 'com.google.gms:google-services:3.1.0'
Run Code Online (Sandbox Code Playgroud)

并在app / build.gradle中为Gradle添加Firebase插件

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

build.gradle will include these new dependencies:
    compile 'com.google.firebase:firebase-database:11.0.4'
Run Code Online (Sandbox Code Playgroud)

资料来源:Android Studio Assistant


The*_*eIT 5

就我而言,Google Services gradle 插件没有values.xml从该google-services.json文件生成所需的文件。Firebase 库使用此生成的值文件来初始化自身,并且如果找不到值文件,它似乎不会引发错误。检查以下位置是否存在值文件,并使用文件中的适当字符串填充google-sevices.json

app/build/generated/res/google-services/{build_type}/values/values.xml
Run Code Online (Sandbox Code Playgroud)

和/或

app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅: https ://developers.google.com/android/guides/google-services-plugin

我的特殊情况是由于使用的 gradle 工具版本对于我运行的 Android Studio 版本而言过于高级(即确保您使用 Android Studio v3.2 运行 Grade 工具 v3.2.X-YYY)。


Але*_*бич 5

另一种可能的解决方案-如果您使用的是Beta,请尝试使用其他Android Studio。对我有帮助。新的Android Studio根本没有正确添加Firebase。就我而言3.3预览

经过更多调查后,我发现问题是新的Android Studio使用较新的Google Services版本启动了项目,这似乎是原始问题。正如@Ammar Bukhari所建议的那样,此更改有所帮助:

classpath'com.google.gms:google-services:4.1.0'-> classpath'com.google.gms:google-services:4.0.0'