Why are some of these imports from the same library returning as unresolved references?

Viewed 412

So I am currently trying to add a room database to an open-source project as part of my thesis, I grabbed the most recent example I could from android itself and modified it for Kotlin DSL, I used the same DAO and database structure but both have import errors. Normally this wouldn't be a problem to fix except I am getting errors from the same library in places which is why I had to ask this question in the first place. The main import problem is to do with androidx.room. Below is the kotlin file in question and after that, is the build.gradle.kts.

Just to add, android studio suggests I use persistence.room.runtime but from what I have tried and what I could find, this does not solve my problem either.


import androidx.room.Database //works
import androidx.room.Room // unresolved reference
import androidx.room.RoomDatabase // unresolved reference

@Database(entities = [GameDatabase::class], version = 1, exportSchema = false)
abstract class Database : RoomDatabase() {

    abstract val DatabaseDAO: DatabaseDAO

    companion object {

        @Volatile
        private var INSTANCE: Database? = null

        fun getInstance(): Database {
            synchronized(this) {
                var instance = INSTANCE

                if (instance == null) {
                    instance = Room.databaseBuilder(
                            Database::class.java,
                            "game_database"
                    )
                            .fallbackToDestructiveMigration()
                            .build()
                    INSTANCE = instance
                }
                return instance
            }
        }
    }
}

Here is the module gradle build:

plugins {
    //id("kotlin")
    kotlin("kapt")
    //kotlin("android")

    //kotlin("android-extensions")
    //id("kotlin-android")
    //id("kotlin-android-extensions")
    //id("androidx.navigation.safeargs")
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_6
}

tasks {
    compileJava {
        options.encoding = "UTF-8"
    }
    compileTestJava {
        options.encoding = "UTF-8"
    }
}

sourceSets {
    main {
        java.srcDir("src/")
    }
}

// added
dependencies {

    val room_version = "2.2.6"
    val life_version = "2.2.0"
    // Room and Lifecycle dependencies
    implementation("androidx.room:room-runtime:$room_version")

    kapt("androidx.room:room-compiler:$room_version")

    //implementation("androidx.room:room-ktx:$room_version")
    implementation("androidx.legacy:legacy-support-v4:1.0.0")
    implementation("androidx.lifecycle:lifecycle-extensions:$life_version")
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$life_version")
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:$life_version")
    implementation("androidx.lifecycle:lifecycle-common-java8:$life_version")
    // adding Firebase dependencies
    //implementation("android.arch.persistence.room:runtime:1.1.1")
    //annotationProcessor("android.arch.persistence.room:compiler:2.2.6")
}

I am quite new to Kotlin DSL so it might be an obvious error but I also couldn't find anything like this in the area.

1 Answers

This is how I usually place the implementations:

//Room
implementation "androidx.room:room-runtime:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:2.2.6"

Also, have you placed the kapt plugin? if not:

plugins {
id 'kotlin-android'
id 'kotlin-kapt'//this one

}

And, try to clean and rebuild the project.

Related