I've integrated Room in my project. In this project some classes are in Kotlin and some are in Java. After I converted my Java file to Kotlin using Android Studio Ctrl+Alt+Shift+K combination, I've started facing this error:
TypeConverter() has private access in TypeConverter
in the generated java class, at this line:
private final PointOfInterest.TypeConverter __typeConverter_5 = new PointOfInterest.TypeConverter();
But TypeConverter
in PointOfInterest
class is public.
Don't change the
object
keyword toclass
(as the accepted answer suggests). The object declaration guarantees the Singleton pattern.
After automatics conversion of TypeConverter
java file to kotlin file, you should mark all inner converter functions with @JvmStatic
so Room can use them as regular static functions.
Take a look at the official Android Architecture Components samples, specifically the GithubTypeConverters.kt. Also, this discussion can be useful. And this is my DateTypeConverter.kt
:
object DateTypeConverter {
@TypeConverter
@JvmStatic
fun toDate(timestamp: Long?) = timestamp?.let { Date(timestamp) }
@TypeConverter
@JvmStatic
fun toTimestamp(date: Date?) = date?.time
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With