我将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子类。这是因为每个应用程序进程必须恰好具有一个实例化的Application对象。
这对您的应用程序意味着该其他进程永远不要使用Firebase API。这意味着您需要找到另一个位置来获取该IID token 。
(请注意,默认情况下,Firebase SDK由合并到您应用中的ContentProvider自动初始化-除非您已删除此ContentProvider或不使用google-services插件,否则您不必调用FirebaseApp.initializeApp()。 )
通常,当应用程序需要获取IID token 时,它们将创建FirebaseInstanceIdService的子类,如documentation中所述。每次知道新 token 时都会通知此服务。那是您应该检索它并将其发送到服务器的地方。