我正在尝试在应用程序上手动初始化FirebaseApp
,但收到此错误。
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setDatabaseUrl("[DATABASE_URL]")
.setApiKey("API_KEY")
.setApplicationId("PROJECT_ID").build();
FirebaseApp.initializeApp(getApplicationContext(),firebaseOptions);
if (!FirebaseApp.getApps(this).isEmpty()) {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
}
假设我相应地设置了
firebaseOptions
值。我期望这将设置
FirebaseApp
的值。我错过了什么吗? FirebaseApp Documentation
FATAL EXCEPTION: main
Process: com.sample.android, PID: 5490
java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapApplication: java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4331)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!
at com.google.android.gms.common.internal.zzab.zza(Unknown Source)
at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
at com.sample.android.activities.BcodeApplication.onCreate(BcodeApplication.java:21)
at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:369)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4328)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
at dalvik.system.NativeStart.main(Native Method)
最佳答案
目前尚不清楚您的目标是什么。如果您只是想修改(“覆盖”)由FirebaseApp
创建的默认FirebaseInitProvider
,我认为这是不可能的。您引用的文档有些误导,因为它暗示有可能。我认为该文档旨在描述尚未创建的默认应用程序(例如,在辅助过程中)如何被初始化。
您可以使用FirebaseApp.initializeApp()
创建另一个应用程序对象。您需要使用接受参数作为应用名称的方法:
FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setDatabaseUrl("[DATABASE_URL]")
.setApiKey("API_KEY")
.setApplicationId("PROJECT_ID").build();
FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(),firebaseOptions,
"MyAppName");
然后,您可以使用创建的
FirebaseApp
获取FirebaseDatabase,FirebaseStorage,FirebaseAuth,FirebaseCrash或FirebaseInstanceId的实例。例如:FirebaseDatabase database = FirebaseDatabase.getInstance(myApp);
在应用程序的主要过程中,我认为没有一种简单的方法可以禁用由
FirebaseInitProvider
完成的初始化处理。如果要覆盖通常来自google-services.json
的配置参数,则可以使用googlet插件documentation中的信息来为参数创建自己的XML文件。它指出:我不知道那是多么简单或可维护。