Search code examples
androidandroid-studiokotlinandroid-roomkapt

Execution failed for task ':app:kaptDebugKotlin'; while using room library


I am trying to make a simple book record app file using Room library for data storage. After finishing first two fragments I decided to test them. Building the code gives me this error:

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> java.lang.reflect.InvocationTargetException (no error message)

Here are my room implementations:

Entity

package com.example.books.data

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
data class Book(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,

    @ColumnInfo(name = "book name")
    val bookName: String,
    @ColumnInfo(name = "author name")
    val bookAuthor: String,

    @ColumnInfo(name = "topic")
    val bookTopic: String,
    @ColumnInfo(name = "status")
    val bookStatus: String,
    @ColumnInfo(name = "chapter")
    val chapter: String,

    @ColumnInfo(name = "read percent")
    val readPercent: Double,
    @ColumnInfo(name = "pages read")
    val pagesRead: Int,
    @ColumnInfo(name = "total page")
    val totalPage: Int,

    @ColumnInfo(name = "date")
    val dateCreated: String?,
    @ColumnInfo(name = "last read")
    val lastRead: String?,

    @ColumnInfo(name = "notes")
    val notes: String?
)

Dao

package com.example.books.data

import androidx.room.*
import kotlinx.coroutines.flow.Flow

@Dao
interface BookDao {

    @Query("SELECT * FROM book ORDER BY `last read` DESC")
    fun getBooks(): Flow<List<Book>>

    @Query("SELECT * FROM book WHERE id = :id")
    fun getBook(id: Int) : Flow<Book>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(book: Book)
    @Update
    suspend fun update(book: Book)
    @Delete
    suspend fun delete(book: Book)
}

Database

package com.example.books.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

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

    abstract fun bookDao(): BookDao

    companion object {
        @Volatile
        private var INSTANCE: BookDatabase? = null

        fun getDatabase(context: Context) : BookDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    BookDatabase::class.java,
                    "book_database"
                )
                    .fallbackToDestructiveMigration()
                    .build()
                INSTANCE = instance
                instance
            }
        }
    }
}

Application


import android.app.Application
import com.example.books.data.BookDatabase

class BookListApplication : Application() {
    val database : BookDatabase by lazy { BookDatabase.getDatabase(this) }
}

Gradle dependencies

dependencies {
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    kapt ("androidx.room:room-compiler:$room_version")
}

I tried to run the gradle console with .\gradlew clean build --debug --stacktrace. After running for a while, the command line gives me a warning saying Debug process may lick some sensitive information leaving the command incomplete.

As I loop back to where I begun, I decide to take it forward to the StackOverflow community. I appreciate your time. Thanks!


Solution

  • After days of frustration and double-checking, I figured a way out; although the solution is not quite acceptable to me.

    Initially, I thought there must be some minor mistakes/typos implementing the room properties that I somehow missed, but that was not the case.

    All I did was downgrade the kotlin_versoin. In my buildscript.ext of project build.gradle I changed to this

    buildscript {
        ext {
            ...
            kotlin_version = '1.4.32'
        }
        ...
    }
    

    From

    buildscript {
        ext {
            ...
            kotlin_version = '1.5.10'
        }
        ...
    }
    

    To avoid Kotlin jar files in classpath should have same version warning I used the same kotlin_version in kotlin-reflect to make it compatible with kotlin-stdlib. In my gradle dependencies, I added

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"  //added
    ...
    

    I don't know why it didn't work with the newest version. Besides, I don't really wanna downgrade.

    Anyone here knows why? Knowing the answer would be satisfactory to me. Thanks!