https://kotlinlang.org logo
#android
Title
# android
m

MiSikora

10/01/2017, 4:20 PM
Can I make Room work with separate Gradle modules and Kotlin? When I define a class like below in one module and build a database in another one I get a compilation error regarding parameter naming.
Copy code
data class Round(
    @ColumnInfo(name = "id")
    val id: Long,

    @ColumnInfo(name = "name")
    val name: String,

    @ColumnInfo(name = "description")
    val description: String,

    @ColumnInfo(name = "measurements_count")
    val measuredCount: Int
)
Copy code
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).

Tried the following constructors but they failed to match:
Round(long,java.lang.String,java.lang.String,int) : [arg0 : null, arg1 : null, arg2 : null, arg3 : null]
g

gildor

10/02/2017, 1:07 AM
Parameter names not available in a separate module, Java erases parameter names (Kotlin keeps them in meta data, but Room works only with jvm class without direct support of Kotlin) So you have two choices: add default contstructor (if add default values to all the params or use compiler plugin) Move all data classes to the same module with database
m

MiSikora

10/02/2017, 5:58 AM
I guess I could also use pure Java classes for Room pojos. Will think about it. Thanks
g

gildor

10/02/2017, 6:07 AM
But this will not help
Java has exactly the same behaviour. You still need default constructor + setters, you cannot use constructor with parameters, just because names of parameters will be erased
m

MiSikora

10/02/2017, 8:50 AM
Hmm. I was quite sure it worked with Java when I checked last time but it seems to not be the case. 🤔 I'll stick to one module as I'd rather make my objects immutable. Thank you.
g

gildor

10/02/2017, 8:54 AM
Maybe in case of java you just had default constructor