CodexBloom - Programming Q&A Platform

Trouble Setting Up Room Database with Kotlin Coroutines in Android 14 - Missing Entities

πŸ‘€ Views: 72 πŸ’¬ Answers: 1 πŸ“… Created: 2025-08-25
android kotlin room Kotlin

I'm reviewing some code and After trying multiple solutions online, I still can't figure this out... I'm trying to implement a Room database in my Android 14 application using Kotlin Coroutines, but I keep running into a `java.lang.IllegalArgumentException: No entity found for class`. I have defined my entity class, DAO, and the database class properly, but the app crashes when I attempt to access the database. Here’s how I set it all up: My entity class looks like this: ```kotlin @Entity(tableName = "user") data class User( @PrimaryKey(autoGenerate = true) val id: Long = 0, val name: String, val email: String ) ``` The DAO interface is defined like this: ```kotlin @Dao interface UserDao { @Insert suspend fun insert(user: User) @Query("SELECT * FROM user") suspend fun getAllUsers(): List<User> } ``` And my database class is set up as follows: ```kotlin @Database(entities = [User::class], version = 1, exportSchema = false) abstract class AppDatabase : RoomDatabase() { abstract fun userDao(): UserDao } ``` In my Repository class, I initialize the database like this: ```kotlin class UserRepository(context: Context) { private val db = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "app_database") .build() private val userDao = db.userDao() } ``` I've double-checked the entity annotations and ensured that the database version is correct. I also tried cleaning and rebuilding the project, but the error persists when I call `userDao.getAllUsers()` in my ViewModel: ```kotlin class UserViewModel(private val repository: UserRepository) : ViewModel() { fun fetchUsers() = viewModelScope.launch { val users = repository.userDao.getAllUsers() // Crashes here } } ``` What could be causing this `IllegalArgumentException`, and how can I resolve it? I've made sure that the database is created before I access it, and I’m using the latest version of Room (2.5.0). Any insights would be greatly appreciated! For context: I'm using Kotlin on Linux. Am I missing something obvious? My development environment is Ubuntu 22.04. Thanks in advance!