'error: [MissingType]: Element 'xxxxx.AppDatabase.room' references a type that is not present

error: [MissingType]: Element 'xxxxx.AppDatabase.room' references a type that is not present

I get the mentioned error in the title when I try to compile my code. It seems that I have a problem with Room which causes this. Kotlin kapt gives this error. How can I solve it?

Project Level 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
}

App level 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"
}

AppDatabase class

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()
    }
}


Solution 1:[1]

I had the same problem while using room v2.4.1 with kotlin v1.5.31. The solution was to change kotlin version to 1.6.0.

Solution 2:[2]

maybe u can try invalidate caches, go to file -> invalidate caches.. -> then click button Invalidate and Restart for me it works well with tha same error code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kozma Zoltán Balázs
Solution 2 luthfirrDz