Reputation: 181
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()
}
}
Upvotes: 18
Views: 8274
Reputation: 609
If you use any type of a Ai code generator , sometimes the code doesn't have the correct package name in the header
package com.ege.altaga.data
which fails the IDE to import that file to the code file you are working. In my case the Dao file's package path is incorrect and Android studio failed to import that file reference to App Database file. Which caused the above error.
Upvotes: 0
Reputation: 1695
Many answers here, but none worked for me.
Actual Error Faced
[ksp] [MissingType]: Element 'com.*****.****.cache.DataStore' references a type that is not present
There is an issue with KSP room support and which is why it is unable to read BuildConfig file that has version in it and i am using that version as below for room DB
import androidx.room.Database
import androidx.room.RoomDatabase
import com.*****.****.BuildConfig // ISSUE WITH KSP https://github.com/google/ksp/issues/350
@Database(entities = [AbcDSO::class], version= BuildConfig.VERSION_CODE, exportSchema = true)
abstract class DataStore : RoomDatabase() {
abstract fun abcDAO(): AbcDAO
}
Solution
Best thing would be to remove BuildConfig import and hard code the version (or use some other way to apply versioning for room db) until this issue is resolved in version https://github.com/google/ksp/milestone/21
//import com.*****.****.BuildConfig --remove this app specific buildconfig reference
//hardcoded version or apply a suitable way of versioning until the ksp issue is resolved
@Database(entities = [AbcDSO::class], version= 1, exportSchema = true)
Upvotes: 2
Reputation: 1
I didn't sync the gradle after implementing a few dependecies. So just make sure to sync before running the project!
Upvotes: 0
Reputation: 87
I just forgot to add this line to the top of my BloodyDao Class:
package this.bloody.package.BloodyDao
adding this line solved my problem
Upvotes: -1
Reputation: 151
I had the same issue when storing Protobuf messages in Room entities (and adding conversions from/to ByteArray
s), and none of the suggestions here helped.
I fixed this problem by adding the following to app/build.gradle
.
android {
kotlin {
sourceSets {
debug {
kotlin.srcDir("build/generated/source/proto/debug/java")
}
release {
kotlin.srcDir("build/generated/source/proto/release/java")
}
}
}
}
This GitHub issue put me on the right track to fix this issue.
It looks like KSP (and thus Room) doesn't use the same resolution strategy as Android Studio, so even though my project built normally, once Room had references to the generated files, the project stopped building.
Upvotes: 0
Reputation: 4908
Run your application with
./gradlew clean build
command to see what's exactly wrong with your code. Just paste it into the Terminal in Android Studio.
...copied word for word from this post. Thanks Łukasz Kobyliński, please upvote their answer.
Upvotes: 4
Reputation: 1
I had the same problem After checking the code several times, I found that I forgot to add the package name in class Dao
Upvotes: 0
Reputation: 9
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
Upvotes: -1
Reputation: 1
Remove this part of code from your App level Gradle and you'll be good to go
configurations { cleanedAnnotations compile.exclude group: 'org.jetbrains', module: 'annotations' }
Upvotes: 0
Reputation: 59
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.
Upvotes: 1