项目结构:
- 移动应用
- 事物应用
- 共享模块
共享模块作为应用程序的依赖项链接。
问题
在我的共享模块中,我有一些通用代码,例如我的 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 文件
项目等级: https://gist.github.com/tscholze/ad81ded78fc5d3a6481436cb3e8587e1
共享模块 gradle: https://gist.github.com/tscholze/ecb8a860157d3a52b8a79b53f293f104
Android Things 等级: https://gist.github.com/tscholze/bdcd2efd5ee6fae1be4433838678e559
更新 2 它与我的 Android Things 项目有关。该代码在我的“移动”Android 项目中运行良好。
最佳答案
我在这里看到两个问题:
app-build.gradle
中的apply plugin
行(您目前已将其注释掉)应该位于底部文件(参见 Firebase instructions )。这很可能是导致您当前错误的原因。您必须使用与您的 Android Things 构建 bundle 在一起的 Play 服务版本(请参阅文档中的 Google Services)。您可以在 Release Notes 中找到与每个预览 bundle 在一起的 Play 服务版本。 .
关于java - "Default FirebaseApp is not initialized"错误发生在模块依赖的静态帮助方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47498946/