Android-Firebase在Instant App中初始化-获取异常

时间:2018-08-19 13:28:10

标签: android firebase android-instant-apps

这是我的应用程序类:

打包com.example.myapp

import android.support.multidex.MultiDexApplication
import com.facebook.appevents.AppEventsLogger
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreSettings

class App : MultiDexApplication() {
    override fun onCreate() {
        super.onCreate()

        // Facebook
        AppEventsLogger.activateApp(this)

        // Firebase
        FirebaseApp.initializeApp(this)

        val settings = FirebaseFirestoreSettings.Builder()
                .setPersistenceEnabled(false)
                .setTimestampsInSnapshotsEnabled(true)
                .build()

        FirebaseFirestore.getInstance().firestoreSettings = settings
    }
}

这是我遇到的错误。在App类的最后一行FirebaseFirestore.getInstance()上失败了,说它没有初始化,但是您可以在上面看到我正在初始化它吗?

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapp, PID: 8328
    java.lang.RuntimeException: Unable to create application com.example.myapp.App: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.myapp. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5876)
        at android.app.ActivityThread.access$1100(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.myapp. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
        at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@17.0.5:49)
        at com.example.myapp.App.onCreate(App.kt:31)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5871)
        at android.app.ActivityThread.access$1100(ActivityThread.java:199) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

有人知道我需要做什么来解决此问题吗? 这是一个即时应用程序。我已经将google services json放入每个模块(基本+应用+即时)中,并且还将插件添加到每个gradle中。

很奇怪,它显然已经超出了initialize语句,但仍然失败。

2 个答案:

答案 0 :(得分:0)

对我来说,任何地方都找不到解决方案。仅此有效。 只需将我的Google服务的评分从4.1.0下载到4.0.0

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
    classpath 'com.google.gms:google-services:4.0.0'
    /*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}

因此,如果您更新了Google服务,请尝试降级或更改为较旧的版本。

答案 1 :(得分:0)

这对我有用。

books