4

I have one open class like this

open class NewsResponse(

@field:SerializedName("news")
val news: List<NewsItem?>? = null
):RealmObject()

And NewsItem class like this

open class NewsItem(

@field:SerializedName("created")
val created: String? = null,

@field:SerializedName("link")
val link: String? = null,

@field:SerializedName("description")
val description: String? = null,

@field:SerializedName("title")
val title: String? = null
):RealmObject()

I have also added

apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android-extensions'

these plugins in app gradle

I have classpath "io.realm:realm-gradle-plugin:5.1.0" in project level gradle. So when I run the app, I get an error saying

Caused by: io.realm.exceptions.RealmException: NewsItem is not part of the schema for this Realm
    at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:180)

How to solve this problem?

theanilpaudel
  • 2,840
  • 8
  • 28
  • 55

2 Answers2

8

Use this order:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
EpicPandaForce
  • 71,034
  • 25
  • 221
  • 371
  • I have the same issue using the latest kotlin version and android studio. Even though the above solution was implemented the app still crash miserably – AouledIssa Oct 03 '18 at 09:55
  • 1
    I've heard it also happens if you still have `annotationProcessor` scope somewhere instead of `kapt` – EpicPandaForce Oct 03 '18 at 10:16
  • I only have implementation and androidTestImplementation. There is no annotationProcessor. Maybe the Realm plugin is self uses this statement. If so we have no control over it as its gradle applied dependency. – AouledIssa Oct 03 '18 at 10:31
  • 2
    If `kotlin-kapt` is applied before `realm-android`, then Realm will apply `kapt` scope since `2.2.0` or so – EpicPandaForce Oct 03 '18 at 10:46
  • 1
    I figured it out. `apply plugin: 'kotlin-kapt` was missing and yes `apply plugin: 'realm-android'` is the last applied. Thank you so much! – AouledIssa Oct 03 '18 at 11:13
1

for me, i have to change my order to:

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'

and also add an empty constructor in my model class.

Ho Luong
  • 526
  • 7
  • 10