android 4.4 kitkat 上的房间持久性库崩溃问题

Room persistence library crash issue on android 4.4 kitkat

我正在处理一个使用房间持久性库版本 1.1.1 的项目,它在 android 4.4 以上的所有设备(例如:棒棒糖、棉花糖等)上运行良好。但每当我尝试安装该应用程序时,它都会在 android 4.4 版上崩溃。我附上了崩溃日志和其他相关文件。

错误日志:

java.lang.RuntimeException: Unable to instantiate application com.example.dmr.MyApp: 
java.lang.RuntimeException: cannot find implementation for 
com.example.dmr.data.local_db.RoomDB. RoomDB_Impl does not exist
    at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317)
    at android.app.ActivityThread.access00(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: cannot find implementation for 
com.example.dmr.data.local_db.RoomDB. RoomDB_Impl does not exist
    at androidx.room.Room.getGeneratedImplementation(Room.java:94)
    at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:667)
    at com.example.dmr.data.local_db.RoomHelper.<init>(RoomHelper.kt:20)
    at com.example.dmr.MyApp.setDataManager(MyApp.kt:34)
    at com.example.dmr.MyApp.attachBaseContext(MyApp.kt:19)
    at android.app.Application.attach(Application.java:181)
    at android.app.Instrumentation.newApplication(Instrumentation.java:991)
    at android.app.Instrumentation.newApplication(Instrumentation.java:975)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317) 
    at android.app.ActivityThread.access00(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method) ode here

Gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

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

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.example.dmr"
    minSdkVersion 18
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    dataBinding.enabled = true

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

defaultConfig {
    multiDexEnabled true // enable mun
}

}
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'


// Retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"
implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"


implementation 'com.github.bumptech.glide:glide:4.8.0'

//Okhttp3
implementation "com.squareup.okhttp3:okhttp:$okhttp3_version"
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
implementation 'com.google.code.gson:gson:2.8.5'



// Room 
implementation 'android.arch.persistence.room:runtime:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
annotationProcessor  'android.arch.persistence.room:compiler:1.1.1'
androidTestImplementation 'android.arch.persistence.room:testing:1.1.1'



// Support Design
implementation "com.android.support:design:$android_support_version"

// RecyclerView
implementation "com.android.support:recyclerview-v7:$android_support_version"

// Constraint Layout
implementation "com.android.support.constraint:constraint-layout:1.1.3"

// LiveData & ViewModel
implementation "android.arch.lifecycle:extensions:$lifecycle_version"


// Dagger 2
implementation "com.google.dagger:dagger:$dagger2_version"
compileOnly "org.glassfish:javax.annotation:3.1.1"

//Rx
implementation "io.reactivex.rxjava2:rxjava:2.2.2"
implementation "io.reactivex.rxjava2:rxandroid:2.1.0"


implementation 'com.google.firebase:firebase-core:17.0.1'
implementation 'com.google.firebase:firebase-messaging:19.0.1'


testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

// Google maps
implementation 'com.google.android.gms:play-services-maps:17.0.0'

// Google Maps Android API Utility Library
implementation 'com.google.maps.android:android-maps-utils:0.5'


//circular image view
implementation 'de.hdodenhof:circleimageview:3.0.0'


implementation 'com.google.android.gms:play-services-location:17.0.0'


//Smart Location Library
implementation 'io.nlopez.smartlocation:library:3.3.3'

implementation 'com.github.ybq:Android-SpinKit:1.2.0'

}

数据库:

package com.example.dmr.data.local_db


import androidx.room.Database
import androidx.room.RoomDatabase
import com.example.dmr.data.local_db.dao.*
import com.example.dmr.data.local_db.entity.AreaInfo
import com.example.dmr.data.local_db.entity.LocalAuthorityContactModel
import com.example.dmr.data.local_db.entity.UserInfo
import com.example.dmr.data.local_db.entity.Volunteer
import com.example.dmr.model.DRM_models.DisasterActivityModel
import com.example.dmr.model.DRM_models.RescueRequestListModel
import com.example.dmr.model.DRM_models.ShelterData


@Database(entities = arrayOf(UserInfo::class,AreaInfo::class,
    DisasterActivityModel:: class,LocalAuthorityContactModel::class,
    ShelterData::class,Volunteer::class, RescueRequestListModel::class),
    version = 1, exportSchema = false)
abstract class RoomDB : RoomDatabase() {

    abstract fun userDao(): UserDao
    abstract fun areaListDao(): AreaListDao
    abstract fun activityListDao(): DisasterActivityListDao
    abstract fun localAuthorityListDao(): LocalAuthorityContactListDao
    abstract fun shelterListDao(): ShelterListDao
    abstract fun volunteerDao(): VolunteerDao
    abstract fun rescueRequestDao(): RescueRequestListDao
}

数据库助手:

package com.example.dmr.data.local_db


import android.content.Context
import androidx.room.Room
import com.example.dmr.data.local_db.entity.AreaInfo
import com.example.dmr.data.local_db.entity.LocalAuthorityContactModel
import com.example.dmr.data.local_db.entity.UserInfo
import com.example.dmr.data.local_db.entity.Volunteer
import com.example.dmr.model.DRM_models.DisasterActivityModel
import com.example.dmr.model.DRM_models.RescueRequestListModel
import com.example.dmr.model.DRM_models.ShelterData

class RoomHelper(context: Context) : IRoomHelper {


    private var context = context
    private val db = Room.databaseBuilder(context, RoomDB::class.java, "BD_NAME")
        .fallbackToDestructiveMigration()
        .allowMainThreadQueries().build()


    //------------activity list---------//

    override fun roomGetRescueRequestList(): List<RescueRequestListModel> {
        return db.rescueRequestDao().roomGetRescueRequestList()
    }

    override fun roomGetRescueRequestListByOperationId(operation_code: String): List<RescueRequestListModel> {
        return db.rescueRequestDao().roomGetRescueRequestListByOperationId(operation_code)
    }

    override fun roomInsertRescueRequestList(rescue_list: List<RescueRequestListModel>){
        db.rescueRequestDao().roomInsertRescueRequestList(rescue_list)
    }

    override fun roomDeleteRescueRequestList(): Int {
       return  db.rescueRequestDao().roomDeleteRescueRequestList()
    }


}

将此添加到 Application.attachBaseContext。

MultiDex.install(this)