0

I'm working to add room to my project written in Kotlin but I always got this error.

Users data class

@Entity 
data class Users(

@PrimaryKey
val id: String? = "",

@ColumnInfo(name = "userFullName")
val name: String?,

@ColumnInfo(name = "userEmail")
val email: String?,
@ColumnInfo(name = "userPassword")
val password: String?,
@ColumnInfo(name = "userBDay", defaultValue = "")
val birthday: String?
)

UserDao

@Dao interface UserDao {

@Insert
fun insertUser(user: Users)

@Delete
suspend fun deleteUser(user: Users)

@Query("SELECT * FROM users")
fun getAllUsers(): List<Users>
}

User database

@Database(entities = [Users::class], version = 1)
abstract class UserDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao

    companion object {
        @Volatile
        private var instance: UserDatabase? = null

        fun getUserDatabase(context: Context): UserDatabase {

            return instance ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    UserDatabase::class.java,
                    "usersDB"
                ).build()

                this.instance = instance
                instance
            }
        }
    }
}

Then when I build it, I got this error:

FAILURE: Build failed with an exception.

What went wrong:

2021-02-14T16:35:38.826+0300 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]

Execution failed for task ':app:kaptDebugKotlin'.

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)

I got this error in every code I've written and downloaded from github.

I tried solutions in these links:

Repeated Build fails "To use Coroutine features, you must add `ktx`......."

How to get rid of Incremental annotation processing requested warning?

Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

A failure occurred while executing com.android.build.gradle.internal.tasks

I also thought it could be caused by AS and I downloaded other version of AS, same thing happened

mrkanet
  • 21
  • 6
  • Could you post gradle configuration for the module with the DB, and full error, if any? – S-Sh Feb 14 '21 at 16:29

3 Answers3

1

It was because of the android studio ide and kotlin version. So, when I updated the next version or downgraded to the other versions the problem's solved

mrkanet
  • 21
  • 6
0

@Insert fun insertUser(user: Users)

Change this to

@Insert suspend fun insertUser(user: Users)

Insert shouldn't be done on UI thread. That's is the possible cause of this error.

Varsha Kulkarni
  • 1,389
  • 2
  • 13
  • 17
0

for me the problem was the @Installin annotation with ApplicationComponent::class I just change it to SingletonComponent::class

Befor:

@InstallIn(ApplicationComponent::class)

After: @InstallIn(SingletonComponent::class)

also removed the @Singleton annotation from function in my module class

in your viewModel class you should inject the constructor like this:

class MainViewModel @Inject constructor ( private val repository: Repository, @ApplicationContext application: MyApplication ):AndroidViewModel(application)

and in your activity or fragment init your viewmodel like this:

val mainViewModel: MainViewModel by viewModels()