Nailuj29
Nailuj29

Reputation: 888

Unresolved reference: RoomDatabase

I am trying to get Room set-up in my application (following the documentation). Everything is compiling fine, except my Database class. The RoomDatabase class is not found. It is probably worth noting that I am using a Jetpack Compose Desktop app, not on Android.

import androidx.room.*

@Entity
data class User(
    @PrimaryKey val uid: Int,
    @ColumnInfo(name = "first_name") val firstName: String?,
    @ColumnInfo(name = "last_name") val lastName: String?
)


@Dao
interface UserDao {
    @Query("SELECT * FROM user")
    fun getAll(): List<User>

    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
    fun loadAllByIds(userIds: IntArray): List<User>

    @Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
            "last_name LIKE :last LIMIT 1")
    fun findByName(first: String, last: String): User

    @Insert
    fun insertAll(vararg users: User)

    @Delete
    fun delete(user: User)
}

@Database(entities = arrayOf(User::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}
plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.4.10'
}

apply plugin: 'kotlin-kapt'

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    google()
    jcenter()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib"
    def room_version = "2.2.5"
    implementation 'androidx.core:core-ktx:1.2.0'

    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"

}

Upvotes: 5

Views: 2481

Answers (2)

Nailuj29
Nailuj29

Reputation: 888

I managed to get this to work adding

id 'com.stepango.aar2jar' version "0.6"

to my plugins block. However, after adding the database initialization code to my main function, the code would no longer compile because the android Context class is required for DB initialization.

Upvotes: 0

beastlyCoder
beastlyCoder

Reputation: 2401

The problem is that everything is in one file. You need to create a User.kt file:

@Entity
data class User(
    @PrimaryKey val uid: Int,
    @ColumnInfo(name = "first_name") val firstName: String?,
    @ColumnInfo(name = "last_name") val lastName: String?
)

Then, a UserDao kotlin file:

@Dao
interface UserDao {
    @Query("SELECT * FROM user")
    fun getAll(): List<User>

    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
    fun loadAllByIds(userIds: IntArray): List<User>

    @Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
           "last_name LIKE :last LIMIT 1")
    fun findByName(first: String, last: String): User

    @Insert
    fun insertAll(vararg users: User)

    @Delete
    fun delete(user: User)
}

Then an app database kotlin file:

@Database(entities = arrayOf(User::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}

My suggestion, would be to follow this tutorial. It will teach you the basics and walk you through a full fledge app. Good luck :)

Upvotes: 1

Related Questions