android - 是否需要调用 FirebaseApp.initializeApp() 来初始化 firebase?

标签 android firebase

我将 firebase 添加到我的 android 项目以使用 firebase 云消息传递。我关注了documentation而且我没有找到任何调用 FirebaseApp.initializeApp() 的说明。

我的应用运行良好,除了一次崩溃并出现以下错误。

Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.my.app. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
at com.my.app.core.ApplicationEx.onCreate(ApplicationEx.java:79)

当我搜索错误时,resolution给出的是在启动时调用 FirebaseApp.initializeApp()

我想知道这是否真的有必要,因为文档没有提到它,而且我的应用程序在没有它的情况下(大部分)运行良好。

有谁知道调用 FirebaseApp.initializeApp() 是否真的有必要,还有什么可能导致我上面提到的错误?

以下是我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.my.app"
        minSdkVersion 17
        targetSdkVersion 26
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    flavorDimensions "appType"
    productFlavors {
        passenger {
            dimension "appType"
            applicationId "com.my.app.passenger"
            versionCode 1
            versionName "1"
        }
        driver {
            dimension "appType"
            applicationId "com.my.app.driver"
            versionCode 1
            versionName "1"
        }
        admin {
            dimension "appType"
            applicationId "com.my.app.admin"
            versionCode 1
            versionName "1"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
        packagingOptions {
            exclude 'META-INF/ASL2.0'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/MANIFEST.MF'
        }
    }
}

repositories {
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

dependencies {
    implementation project(path: ':cards')
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:design:${supportVersion}"
    implementation "com.android.support:support-v4:${supportVersion}"
    implementation "com.android.support:appcompat-v7:${supportVersion}"
    implementation "com.android.support:cardview-v7:${supportVersion}"
    implementation "com.android.support:gridlayout-v7:${supportVersion}"
    implementation "com.google.android.gms:play-services-maps:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-location:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-places:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-gcm:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-ads:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-auth:${googlePlayServicesVersion}"
    implementation 'com.google.maps:google-maps-services:0.2.5'
    implementation "com.google.firebase:firebase-messaging:${googlePlayServicesVersion}"
    implementation "com.loopj.android:android-async-http:${asyncHttpVersion}"
    implementation "com.android.support.test.espresso:espresso-idling-resource:${espressoVersion}"
    implementation 'com.android.support:multidex:1.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'org.slf4j:slf4j-api:1.7.25'
    implementation 'com.github.tony19:logback-android-core:1.1.1-6'
    implementation 'ch.acra:acra:4.9.2'
    implementation('com.github.tony19:logback-android-classic:1.1.1-6') {
        exclude group: 'com.google.android', module: 'android'      // workaround issue #73
    }
    testImplementation 'org.testng:testng:6.9.6'
    testImplementation 'org.mockito:mockito-core:1.10.19'
    testImplementation 'org.powermock:powermock-api-mockito:1.6.5'
    testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.5'
    testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.5'
    testImplementation 'org.powermock:powermock-module-junit4:1.6.5'
    androidTestImplementation "com.android.support:support-annotations:${supportVersion}"
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test:rules:1.0.1'
    androidTestImplementation 'org.testng:testng:6.9.6'
    androidTestImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
    androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
    androidTestImplementation("com.android.support.test.espresso:espresso-core:${espressoVersion}", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
}

apply plugin: 'com.google.gms.google-services'

最佳答案

Firebase SDK 通常不支持使用主进程以外的进程。如果当 ACRA 启动并启动另一个进程时,它自己的进程将为该进程创建一个新的 Application 子类。这是因为每个应用程序进程都必须至少实例化一个应用程序对象。

对于您的应用而言,这意味着此其他进程永远不应使用 Firebase API。这意味着您需要找到另一个地方来获取该 IID token 。

(请注意,Firebase SDK 由默认情况下合并到您的应用程序中的 ContentProvider 自动初始化 - 您永远不必调用 FirebaseApp.initializeApp() 除非您已删除此 ContentProvider 或您没有使用 google-服务插件。)

通常,当应用程序需要获取 IID token 时,它们会创建 FirebaseInstanceIdService 的子类,如 documentation 中所述.每次知道新 token 时都会通知此服务。这是您应该检索它并将其发送到您的服务器的地方。

关于android - 是否需要调用 FirebaseApp.initializeApp() 来初始化 firebase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47764058/

相关文章:

android - 如何在不影响位图大小的情况下降低位图质量

ios - 设置 Firebase iOS 应用程序

javascript - Firebase 云功能停止工作 - event.data undefined

ios - 在 Firebase iOS SDK 中更新实时数据库对象

ios - 由于 Firebase 手机身份验证,App Store 被拒绝

javascript - Firebase .then 与 .on ('value' )

android - 没有可用于离线模式的 androidx.databinding :databinding-compiler:3. 4.2 的缓存版本

android - 无法使用 Gradle (decard-gradle) 构建测试

android - 如何使用触摸板保持旋转不变?

java - 将 ArrayList URL 加载到 ViewHolder 中