BUG:Cannot find implementation for xxx. database. xxx. xxx_Impl does not exist

发布于:2025-04-18 ⋅ 阅读:(29) ⋅ 点赞:(0)

问题背景

        使用Jetpack Compose将数据存储在room本地数据库时,编译报错:

java. lang. RuntimeException: Cannot find implementation for com. example. androidproject. practice. roomdmeo. database. AppDatabase. AppDatabase_Impl does not exist

问题解决

        该错误主要是因为构建时 Room 生成的数据库实现类没有找到,通常是因为 Room 相关的注解处理器没有正确执行,或者构建时出现了问题。在StackOverflow中已经有大佬已经给出答案,链接为:

https://stackoverflow.com/questions/46665621/android-room-persistent-appdatabase-impl-does-not-exist

        主要原因就是org.jetbrains.kotlin.android的 版本问题,需要将其升级到"1.9.21"版本及以上。我也是按照这个思路去解决的,解决过程比好繁琐,就是不断的切换版本,看它能不能编译成功。我在这里也就直接贴出我的build.gradle.kts文件供大家参考。

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android") version "1.9.22"
    id("kotlin-kapt")  // 启用 kapt 插件,用于支持 Room 注解处理器
}



android {
    namespace = "com.example.androidproject"
    compileSdk = 36

    defaultConfig {
        applicationId = "com.example.androidproject"
        minSdk = 26
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
//        kotlinCompilerExtensionVersion = "1.5.1"
        kotlinCompilerExtensionVersion = "1.5.9"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.15.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
    implementation("androidx.activity:activity-compose:1.10.1")
    implementation(platform("androidx.compose:compose-bom:2025.03.01"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    implementation("androidx.constraintlayout:constraintlayout:2.2.1")

    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.2.1")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2025.03.01"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")


    // 页面导航依赖
    implementation("androidx.navigation:navigation-compose:2.8.9")  // 最新版本

    // 网络请求依赖
    implementation("com.squareup.retrofit2:retrofit:2.9.0") // Retrofit 核心库
    implementation("com.squareup.retrofit2:converter-gson:2.9.0") // Gson 转换器
    implementation("com.squareup.okhttp3:logging-interceptor:4.12.0") // 网络日志
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.7") // Jetpack Compose ViewModel

    // room数据库
    implementation("androidx.room:room-runtime:2.6.1")
    implementation("androidx.room:room-ktx:2.6.1")   // 可选的 Kotlin 扩展
    kapt("androidx.room:room-compiler:2.6.1")  // 使用 kapt 代替 annotationProcessor
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.2.2" apply false
    id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}


网站公告

今日签到

点亮在社区的每一天
去签到