什么是 Firestore 组件不存在以及如何修复它

What is Firestore component is not present and how to fix it

什么是:

Unable to start activity ComponentInfo: java.lang.NullPointerException: Firestore component is not present

最近我将代码从 java 转换为 Kotlin。几天它运行良好,但从过去 1 周开始,我不知道为什么,但每当我 运行 出现以下错误的应用程序时。

Kotlin 错误 -

java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException: Firestore component is not present

下面是我的应用gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'com.google.gms.google-services'
}

android {
    compileSdkVersion 31

    defaultConfig {
        configurations.all {
            resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
        }
        applicationId "in.xxx"
        minSdkVersion 26
        targetSdkVersion 31
        versionCode 2
        versionName "1.2"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    buildFeatures {
        viewBinding false
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {

    // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:29.0.0')
    implementation 'com.google.firebase:firebase-firestore-ktx'
    implementation 'com.google.firebase:firebase-storage-ktx'
    implementation 'com.google.firebase:firebase-auth-ktx'
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.firebase:firebase-core'
    implementation 'com.github.OneTouchPro:One:3.4'   
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'

    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

项目Gradle:

buildscript {
    ext.kotlin_version = "1.6.10"
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Kotlin 文件:

class MainActivity : AppCompatActivity() {

    private lateinit var mStorageRef: StorageReference
    private lateinit var oneSharedPref: OneSharedPref
    private lateinit var database: FirebaseFirestore
    private lateinit var mProgress: OneProgress
    private lateinit var postAdapter: PostAdapter
    private lateinit var categoryAdapter: CategoryAdapter

    private var startDate: Timestamp? = null
    private var dateValue: Int? = 0
    private var calendar: Calendar? = Calendar.getInstance()
    private var query: Query? = null
    private var splash: Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)      

        FirebaseApp.initializeApp(this)
        database = Firebase.firestore
    }

此行出错

database = Firebase.firestore

我找到了解决问题的方法。在我的脚本中添加了绑定选项并将项目依赖项移动到 gradle 设置。