realm-java: [Kotlin] annotation processor throws if the realm plugin is applied after kotlin

Goal

Use a RealmList<String> in Kotlin

Expected Results

I’m able to use a (Kotlin) String in a RealmList

Actual Results

Realm can’t use Kotlin strings, saying:

Error:Element type of RealmList must be a class implementing ‘RealmModel’ or one of the ‘java.lang.String’, ‘byte[]’, ‘java.lang.Boolean’, ‘java.lang.Long’, ‘java.lang.Integer’, ‘java.lang.Short’, ‘java.lang.Byte’, ‘java.lang.Double’, ‘java.lang.Float’, ‘java.util.Date’.

Also, using java.lang.String in a Kotlin file doesn’t fix the error.

Steps & Code to Reproduce

  1. Setup new project
  2. Create realm object
  3. Add var list : RealmList<String> = RealmList()

See the error shown above

Version of Realm and tooling

Realm version(s): 4.0.0

Realm sync feature enabled: no

Android Studio version: 3.0.0RC1

Which Android version and device: n/a

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

@Marcel50506

The problem is caused by the order of applied plugins:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'

Change it to:

apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

Will solve the issue.

But it feels something is not right there since i got:

app: Original kapt is deprecated. Please add "apply plugin: 'kotlin-kapt'" to your build.gradle.
Processor path was modified by kapt. Previous value = configuration ':app:debugAnnotationProcessorClasspath'
Destination for generated sources was modified by kapt. Previous value = /tmp/5434/MyApplication3/app/build/generated/source/apt/debug
Note: Processing class SimpleObject
error: Element type of RealmList must be a class implementing 'RealmModel' or one of the 'java.lang.String', 'byte[]', 'java.lang.Boolean', 'java.lang.Long', 'java.lang.Integer', 'java.lang.Short', 'java.lang.Byte', 'java.lang.Double', 'java.lang.Float', 'java.util.Date'.
1 error

when the problem happens

I guess something needs to be updated in the realm plugin.

using classpath "io.realm:realm-gradle-plugin:4.0.0"

and plugins

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

And following class

open class Person (
        var name: String? = null,
        var age: Int = 0,
        var birthDate: Date? = null,
        var parents: RealmList<Person>? = null,
        @field:LinkingObjects("parents") val childOf: RealmResults<Person>? = null,
        var phoneNumbers: RealmList<String>? = null
): RealmObject()

My code compiles just fine with Kotlin? 😕

We got the same issue while using kapt3 ("apply “kotlin-kapt”), and it’s gone using kapt1 (which should be deprecated) Forget what I said, i messed up, the issue we got was when we tried with List<String> instead of RealmList<String>

@Zhuinden

This doesn’t make a difference