I am attempting to implement a Room library in an Android Studio project. However, I keep getting the following error:
[MissingType]: Element ‘com.example.XXX.AppDatabase’ references a type that is not present
This is the implementation of my AppDatabase:
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(entities = arrayOf(GameReview::class), version = 8)
abstract class AppDatabase : RoomDatabase() {
abstract fun gameDao(): GameDao
companion object {
private var INSTANCE: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
// context.deleteDatabase("gamereview")
if (INSTANCE == null) {
synchronized(AppDatabase::class) {
INSTANCE = buildRoomDB(context)
}
}
return INSTANCE!!
}
private fun buildRoomDB(context: Context) =
Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"gamereview"
).fallbackToDestructiveMigration().build() //fallbackToDestructiveMigration().
}
and this is my build.gradle(Module:app) file:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'androidx.navigation.safeargs'
id 'kotlin-kapt'
}
android {
namespace 'com.example.rma23_19084_projekat'
compileSdk 33
defaultConfig {
applicationId "com.example.rma23_19084_projekat"
minSdk 29
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.ext:junit:+'
androidTestImplementation 'androidx.test:core-ktx:+'
androidTestImplementation 'androidx.test.ext:junit-ktx:+'
androidTestImplementation 'androidx.test.espresso:espresso-core:+'
androidTestImplementation 'androidx.test.espresso:espresso-intents:+'
testImplementation("org.hamcrest:hamcrest:2.2")
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.5.1'
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'
androidTestImplementation 'androidx.test:core-ktx:1.5.0'
implementation 'androidx.tracing:tracing:1.1.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
implementation("androidx.navigation:navigation-dynamic-features-fragment:2.5.3")
androidTestImplementation("androidx.navigation:navigation-testing:2.5.3")
implementation("androidx.navigation:navigation-compose:2.5.3")
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:+"
implementation 'com.github.bumptech.glide:glide:4.15.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.15.1'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
implementation 'com.squareup.retrofit2:retrofit:+'
implementation 'com.squareup.retrofit2:converter-gson:+'
implementation("androidx.room:room-runtime:2.5.2")
annotationProcessor "androidx.room:room-compiler:2.5.2"
implementation("androidx.room:room-ktx:2.5.2")
kapt("androidx.room:room-compiler:2.5.2")
}
I have also implemented entity, dao and a repository for utilising AppDatabase in the Main activity. If it’s necessary, I can include these files as well, but I was not sure since this is the first time I am encountering this error and I am not sure what it means.
To anyone answering this, thank you in advance!