I'm using the Room library, and I'm getting the following error message when I try to build the app:
e: [kapt] An exception occurred: java.util.NoSuchElementException: Collection contains no element matching the predicate.
Here is a more detailed error message:
FAILURE: Build failed with an exception
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Compilation error. See log for more details
I isolated the problem in the code to be somewhere in the following, although I don't know what exactly is throwing this exception:
package com.example.pomoplay
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(entities = [(Category::class)], version = 1)
abstract class PomoPlayDatabase: RoomDatabase() {
abstract fun categoryDao(): CategoryDao
companion object {
private var INSTANCE: PomoPlayDatabase? = null
internal fun getDatabase(context: Context): PomoPlayDatabase?
{
if (INSTANCE == null) {
synchronized(PomoPlayDatabase::class.java) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder<PomoPlayDatabase>(
context.applicationContext,
PomoPlayDatabase::class.java,
"pomoplay_database").build()
}
}
}
return INSTANCE
}
}
}
Gradle.build - project
buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0'
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Gradle.build - module
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: 'kotlin-kapt'
android {
packagingOptions {
exclude 'META-INF/atomicfu.kotlin_module'
}
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.pomoplay"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
//Room Components
implementation "androidx.room:room-runtime:2.2.3"
kapt "androidx.room:room-compiler:2.2.3"
//Testing Components
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
No need to create separate Data class. Just make sure its Serializable
After spending a day for this error, I identified the issue to be presence of an empty constructor in the @Entity class.
constructor() {}
Its not that always this constructor should be absent. I have another @Entity class where it is very much needed or else it fails with this error
Entities and POJOs must have a usable public constructor.
You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class Question implements java.io.Serializable
Not understood the reasoning behind it, but must be way of our usage. That shouldn't mean Android Studio should go silent on Line number of compilation error. It felt like a mouse trap