Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeConverter() has private access in TypeConverter error with Room in Android

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.

like image 905
Rahul Rastogi Avatar asked Jul 20 '18 09:07

Rahul Rastogi


1 Answers

Don't change the object keyword to class (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

}
like image 54
lcnicolau Avatar answered Oct 18 '22 13:10

lcnicolau