I started my first Android project in Kotlin. The official docs recommanded me to use Room which I did.
After adding Room I couldn't compile my project.
Cannot figure out how to save this field into database. You can consider adding a type converter for it.
warning: There are multiple good constructors and Room will pick the
no-arg constructor. You can use the @Ignore annotation to eliminate
unwanted constructors.
public final class TodoData {
^
error: Cannot find setter for field.
private final java.lang.Long id = null;
^
error: Cannot find setter for field.
private final java.lang.String firstName = null;
^
error: Cannot find setter for field.
private final java.lang.String lastName = null;
^
the version of kotlin and room are:
- ext.kotlin_version = '1.2.31'
- ext.room_version = '1.0.0'
My gradle.build dependencies looks like this:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// Room
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
I Also added at the top of the file:
apply plugin: 'kotlin-kapt'
And this is my Dao:
@Entity
data class TodoData(
@PrimaryKey(autoGenerate = true)
var od: Long? = null,
var firstName: String = "",
var lastName: String = ""
){
constructor(): this(od = null, firstName = "", lastName = "")
}
What I tried:
- Change Kotlin version
- Add setters and getters (this seems not allowed in Kotlin)
- Set var to val
Did someone experienced the same problem and fixed it?
Thank you for your time!
ps.: I don't often post stuff on Stackoverflow Feedback on the way I present my problem would be kindly accepted.