Search code examples
androidandroid-roomkapt

Room: "Invalid return type for a type converter"


I have a Room database with the following entity:

data class NoteEntity(
    val startRef: BibleRef,
    val endRef: BibleRef,
    val content: String
)

I added the following type converter to store BibleRef fields as Ints:

class Converters {
    @TypeConverter
    fun bibleRefToInt(ref: BibleRef?): Int? {
        // performs conversion...
    }

    @TypeConverter
    fun bibleRefFromInt(refInt: Int?): BibleRef? {
        // performs conversion...
    }
}

I get the following build error (using kapt):

Invalid return type for a type converter. - Converters.bibleRefFromInt(java.lang.Integer)

BibleRef is a simple Kotlin data class.

data class BibleRef(
    val book: Int,
    val chapter: Int,
    val verse: Int
)

Solution

  • You appear to have not included an @TypeConverters annotation.

    • Noting the plural (defines the class or classes where the TypeConverters are) rather than the singular (defines a method/function as a TypeConverter).

    In you case this would be :-

    @TypeConverters(Converters::class)
    

    And it is suggested that it is coded in the class that has the @Database annotation either immediately before or after the @Database annotation e.g.

    @TypeConverters(Converters::class)
    @Database(entities = [NoteEntity::class], version = 1)
    

    This provides the greatest scope


    Full code used to verify the above:-

    BibleRef (unchanged)

    data class BibleRef(
        val book: Int,
        val chapter: Int,
        val verse: Int
    )
    

    NoteEntity (see note)

    @Entity
    data class NoteEntity(
        @PrimaryKey
        val id: Long? = null,
        val startRef: BibleRef,
        val endRef: BibleRef,
        val content: String
    )
    
    • Added id column as Primary Key as otherwise error: An entity must have at least 1 field annotated with @PrimaryKey public final class NoteEntity {

    • Added @Entity annotation otherwise error: Entity class must be annotated with @Entity public final class NoteEntity {

    Converters (see notes)

    class Converters {
        @TypeConverter
        fun bibleRefToInt(ref: BibleRef?): Int? {
            return 0
        }
    
        @TypeConverter
        fun bibleRefFromInt(ref: Int?): BibleRef? {
            return BibleRef(0,0,0)
        }
    }
    
    • both changed to return a value of the appropriate type

    TheDatabase added so that full compilation of Room code is undertaken and that generated java is generated.

    @TypeConverters(Converters::class)
    @Database(entities = [NoteEntity::class], version = 1)
    abstract class TheDatabase: RoomDatabase() {
    }
    
    • Note that no @Dao annotated classes have been used.

    Build Log :-

    Executing tasks: [:app:assembleDebug] in project E:\AndroidStudioApps\SO70425430KotlinRoomTypeConverterInvalidReturnType
    
    > Task :app:preBuild UP-TO-DATE
    > Task :app:preDebugBuild UP-TO-DATE
    > Task :app:compileDebugAidl NO-SOURCE
    > Task :app:compileDebugRenderscript NO-SOURCE
    > Task :app:generateDebugBuildConfig UP-TO-DATE
    > Task :app:checkDebugAarMetadata UP-TO-DATE
    > Task :app:generateDebugResValues UP-TO-DATE
    > Task :app:generateDebugResources UP-TO-DATE
    > Task :app:mergeDebugResources UP-TO-DATE
    > Task :app:createDebugCompatibleScreenManifests UP-TO-DATE
    > Task :app:extractDeepLinksDebug UP-TO-DATE
    > Task :app:processDebugMainManifest UP-TO-DATE
    > Task :app:processDebugManifest UP-TO-DATE
    > Task :app:processDebugManifestForPackage UP-TO-DATE
    > Task :app:processDebugResources UP-TO-DATE
    > Task :app:kaptGenerateStubsDebugKotlin
    > Task :app:javaPreCompileDebug UP-TO-DATE
    > Task :app:mergeDebugNativeDebugMetadata NO-SOURCE
    > Task :app:mergeDebugShaders UP-TO-DATE
    > Task :app:compileDebugShaders NO-SOURCE
    > Task :app:generateDebugAssets UP-TO-DATE
    > Task :app:mergeDebugAssets UP-TO-DATE
    > Task :app:compressDebugAssets UP-TO-DATE
    > Task :app:processDebugJavaRes NO-SOURCE
    > Task :app:checkDebugDuplicateClasses UP-TO-DATE
    > Task :app:desugarDebugFileDependencies UP-TO-DATE
    > Task :app:mergeExtDexDebug UP-TO-DATE
    > Task :app:mergeLibDexDebug UP-TO-DATE
    > Task :app:mergeDebugJniLibFolders UP-TO-DATE
    > Task :app:mergeDebugNativeLibs NO-SOURCE
    > Task :app:stripDebugDebugSymbols NO-SOURCE
    > Task :app:validateSigningDebug UP-TO-DATE
    > Task :app:writeDebugAppMetadata UP-TO-DATE
    > Task :app:writeDebugSigningConfigVersions UP-TO-DATE
    
    > Task :app:kaptDebugKotlin
    E:\AndroidStudioApps\SO70425430KotlinRoomTypeConverterInvalidReturnType\app\build\tmp\kapt3\stubs\debug\a\a\so70425430kotlinroomtypeconverterinvalidreturntype\TheDatabase.java:8: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
    public abstract class TheDatabase extends androidx.room.RoomDatabase {
                    ^
    
    > Task :app:compileDebugKotlin
    w: E:\AndroidStudioApps\SO70425430KotlinRoomTypeConverterInvalidReturnType\app\src\main\java\a\a\so70425430kotlinroomtypeconverterinvalidreturntype\Converters.kt: (9, 23): Parameter 'ref' is never used
    w: E:\AndroidStudioApps\SO70425430KotlinRoomTypeConverterInvalidReturnType\app\src\main\java\a\a\so70425430kotlinroomtypeconverterinvalidreturntype\Converters.kt: (14, 25): Parameter 'ref' is never used
    
    > Task :app:compileDebugJavaWithJavac
    > Task :app:compileDebugSources
    > Task :app:mergeDebugJavaResource UP-TO-DATE
    > Task :app:dexBuilderDebug
    > Task :app:mergeProjectDexDebug
    > Task :app:packageDebug
    > Task :app:assembleDebug
    
    Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
    Use '--warning-mode all' to show the individual deprecation warnings.
    See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings
    
    BUILD SUCCESSFUL in 2s
    30 actionable tasks: 7 executed, 23 up-to-date
    
    Build Analyzer results available