错误:[MissingType]:元素“xxxxx.AppDatabase.room”引用不存在的类型

Ege*_*han 14 android kotlin android-room

错误:[MissingType]:元素“xxxxx.AppDatabase.room”引用不存在的类型

当我尝试编译代码时,出现标题中提到的错误。看来我的房间有问题导致了这个问题。Kotlin kapt 给出了这个错误。我该如何解决?

项目级别 Gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {


    ext.kotlin_version = '1.5.31'
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
        classpath 'com.google.gms:google-services:4.3.10'

        def nav_version = "2.3.5"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        maven { url "https://maven.google.com" } // Google's Maven repository
        maven { url 'https://developer.huawei.com/repo/' } // HUAWEI Maven repository

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

应用程序级别 Gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.0"
    flavorDimensions "default"
    viewBinding {
        enabled = true
    }

    defaultConfig {
        applicationId "com.ege.altaga"
        minSdkVersion 21
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        multiDexEnabled true


        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        release {
            storeFile file("key.keystore.jks")
            storePassword 'android'
            keyAlias 'altaga'
            keyPassword 'android'
        }

    }

    buildTypes {
        debug {
            minifyEnabled false
            shrinkResources false
            debuggable true
            aaptOptions.cruncherEnabled = false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'


//            resValue("string", "google_device_verification_api_key", "AIzaSyDNLDM08-vLp9uszlNJH1SJW1y7oUqsA-U")
        }

        release {
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            minifyEnabled true
            shrinkResources false
            debuggable false
            signingConfig signingConfigs.release
            zipAlignEnabled true
//            resValue("string", "google_device_verification_api_key", "AIzaSyDNLDM08-vLp9uszlNJH1SJW1y7oUqsA-U")
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
//        coreLibraryDesugaringEnabled true
    }
    configurations {
        cleanedAnnotations
        compile.exclude group: 'org.jetbrains', module: 'annotations'
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.32"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation 'com.google.code.gson:gson:2.8.7'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'io.reactivex:rxjava:1.1.6'
    implementation 'io.reactivex:rxandroid:1.2.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'

    kapt 'com.github.bumptech.glide:compiler:4.12.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
    def room_version =  "2.4.0-beta02"

    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
//    annotationProcessor "androidx.room:room-compiler:$room_version"

    // optional - RxJava2 support for Room
    implementation "androidx.room:room-rxjava2:$room_version"
}
Run Code Online (Sandbox Code Playgroud)

应用程序数据库类

package com.ege.altaga.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.ege.altaga.data.database.player.PlayerDAO
import com.ege.altaga.data.database.player.PlayerEntity
import com.ege.altaga.data.database.square.SquareDAO
import com.ege.altaga.data.database.square.SquareEntity

@Database(entities = [(PlayerEntity::class),(SquareEntity::class)],version = 1)
abstract class AltagaDB : RoomDatabase() {

    abstract  fun playerDAO() : PlayerDAO<PlayerEntity>
    abstract  fun squareDAO() : SquareDAO<SquareEntity>

    companion object {
        @Volatile var instance: AltagaDB? = null

         fun getDatabase(context: Context): AltagaDB =
             instance ?: synchronized(this) { instance ?: buildDatabase(context.applicationContext).also { instance = it } }

         private fun buildDatabase(appContext: Context) =
             Room.databaseBuilder(appContext, AltagaDB::class.java, "AltagaDB")
                 .fallbackToDestructiveMigration()
                 .build()
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

我在使用 room v2.4.1 和 kotlin v1.5.31 时遇到了同样的问题。解决方案是将kotlin版本更改为1.6.0。