java - "Default FirebaseApp is not initialized"错误发生在模块依赖的静态帮助方法中

标签 java android gradle firebase-realtime-database

项目结构:

  • 移动应用
  • 事物应用
  • 共享模块

共享模块作为应用程序的依赖项链接。

问题

在我的共享模块中,我有一些通用代码,例如我的 FirebaseManager,它使用静态方法简化了新数据库条目的创建。

public class FirebaseManager
{
    /**
     * Saves a given object as child to the root
     * @param root Name of the root node
     * @param obj Object to save
     */
    public static void create(String root, Object obj)
    {
        DatabaseReference database = FirebaseDatabase.getInstance().getReference(root);
        String id = database.push().getKey();
        database.child(id).setValue(obj);
    }
}

如果我现在在应用程序中调用这个创建方法,我将得到一个包含以下错误的堆栈跟踪:

Default FirebaseApp is not initialized in this process com.example. Make sure to call FirebaseApp.initializeApp(Context) first.

我尝试过但没有解决我的问题:

  • 在我的 `MainActivity.OnCreate(...) 方法上调用了 FirebaseApp.initializeApp(this)
  • 将 MainActivity 的上下文作为参数添加到我的创建方法中
  • 其他堆栈溢出帖子建议,将 apply plugin: 'com.google.gms.google-services' 添加到 m apps 的 build.gradle 文件中。这将导致一个奇怪的“Multiple Dex Archive ...”错误

更新 1:添加了 gradle 文件

更新 2 它与我的 Android Things 项目有关。该代码在我的“移动”Android 项目中运行良好。

最佳答案

我在这里看到两个问题:

  1. app-build.gradle 中的 apply plugin 行(您目前已将其注释掉)应该位于底部文件(参见 Firebase instructions )。这很可能是导致您当前错误的原因。

  2. 您必须使用与您的 Android Things 构建 bundle 在一起的 Play 服务版本(请参阅文档中的 Google Services)。您可以在 Release Notes 中找到与每个预览 bundle 在一起的 Play 服务版本。 .

关于java - "Default FirebaseApp is not initialized"错误发生在模块依赖的静态帮助方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47498946/

相关文章:

android - 如何在 Android Gradle 中自动增加 versionCode

java - Ubuntu 中的 Bash 脚本错误 : awk: line 1: regular expression exceeds implementation size limit

java - 如何向选项卡添加图标

android - android xml中app命名空间的用途是什么

android - 如何通过在 Android 中滑动或滑动来删除回收站 View 行?

java - Activiti + Spring Boot + Gradle 构建挂起,而 gradle clean 测试

continuous-integration - Gradle任务依次调用其他任务

java - URL 中的 & 符号使 XML 格式不正确?

java - Apache POI - 负颜色值

android - 重叠 fragment 和触摸事件