Firestore 导致应用崩溃,并具有新的依赖项

Firestore crashing app with new dependencies

提问人:Rajarshi 提问时间:10/5/2018 最后编辑:Rajarshi 更新时间:3/15/2019 访问量:1948

问:

昨天我用 更新了 Firestore 的依赖项,并使用 .现在,在启动应用程序时,它会崩溃。17.1.14.1.0

注意:

如果我将 Google 服务更改为 ,Firestore 会正确初始化,并且应用程序可以按预期工作。4.0.2

classpath 'com.google.gms:google-services:4.0.2'

更新:

更改为工作。4.2.0


原木:

Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project.
... I/FirebaseInitProvider: FirebaseApp initialization unsuccessful

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{\...\/\...\.ui.MessageActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process \...\. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process \...\. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.2:240)
        at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@17.1.1:68)
        at \...\.ServiceLocator.provideFirestore(ServiceLocator.java:18)
        at \...\.ServiceLocator.provideMessageRepository(ServiceLocator.java:28)
        at \...\.ServiceLocator.provideMessageViewModelFactory(ServiceLocator.java:33)
        at \...\.ui.MessageActivity.onCreate(MessageActivity.java:112)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

gradle 项目:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0-alpha13'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:4.1.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

Gradle 应用:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "..."
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 8
        versionName "1.0.8"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    // ...
    buildToolsVersion '28.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    // ...
    // Firestore
    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.google.firebase:firebase-firestore:17.1.1'
    implementation 'com.google.firebase:firebase-auth:16.0.4'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'com.google.firebase:firebase-ads:16.0.1'
    implementation 'com.firebaseui:firebase-ui-auth:4.1.0'
    // Crash Reports
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5'
}

apply plugin: 'com.google.gms.google-services'
安卓 Firebase 谷歌云 Firestore

评论

0赞 kAliert 10/5/2018
在使用 Firebase 实体之前,您是否通过调用 正确启动 Firebase?FirebaseApp.initializeApp(context)
0赞 Rajarshi 10/5/2018
但 Firestore 通过以下方式初始化组件 firebase.google.com/docs/firestore/quickstart#initializeFirebaseFirestore db = FirebaseFirestore.getInstance();
0赞 kAliert 10/5/2018
但在调用之前,您需要使用 .通常这是在你的课堂上完成的。FirebaseFirestore db = FirebaseFirestore.getInstance()FirebaseApp.initializeApp(context)Application
0赞 Rajarshi 10/5/2018
我已经试过了,没有帮助。app = FirebaseApp.initializeApp(context); FirebaseFirestore.getInstance(app);
1赞 Alex Mamo 10/5/2018
没有必要使用.仅使用依赖项就足够了。您是否尝试过使用:而不是.它有效吗?请回复@。FirebaseApp.initializeApp(context)classpath 'com.android.tools.build:gradle:3.2.0'classpath 'com.android.tools.build:gradle:3.3.0-alpha13'

答:

5赞 Alex Mamo 10/5/2018 #1

没有必要使用.仅使用依赖项就足够了。因此,如果您执行了标准集成,则不必手动调用它,因为它将通过启动自动调用,该启动将在任何其他活动或服务之前初始化。您可以阅读 Doug 关于如何在 Android 上初始化 Firebase 的帖子。FirebaseApp.initializeApp(context)ContentProvider

代码中的问题是,您正在为项目使用gradle的版本。要解决此问题,请更改以下代码行:alpha13

classpath 'com.android.tools.build:gradle:3.3.0-alpha13'

classpath 'com.android.tools.build:gradle:3.2.0'

将解决此问题。

4赞 Kirill Rakhman 10/22/2018 #2

根据 https://issuetracker.google.com/issues/112716914,这是 play-services 插件 4.1.0 中已确认的错误。答案是使用早期版本,例如 4.0.2。

编辑:版本4.2.0发布,其中修复了错误。

评论

0赞 Rajarshi 10/22/2018
或使用 使用classpath 'com.android.tools.build:gradle:3.2.1'
2赞 Rajarshi 11/10/2018
4.2.0已发布,并且不会使应用程序崩溃。
0赞 Farry 3/15/2019 #3

切换到 4.2.0 解决了该问题。