4

I'm trying to build a test project in Kotlin with Realm. Here's my model:

open class Book : RealmObject() {

    // Standard getters & setters generated by your IDE…
    @PrimaryKey
    open var id: Long = 0

    open var title = ""

    open var description = ""

    open var author = ""

    open var imageUrl = ""
}

and here's exception I get:

FATAL EXCEPTION: main
                                                                        Process: app.androidhive.info.realm, PID: 18782
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{app.androidhive.info.realm/app.androidhive.info.realm.activity.MainActivity}: java.lang.IllegalArgumentException: Book is not part of the schema for this Realm
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.IllegalArgumentException: Book is not part of the schema for this Realm

I searched around for the solution so and added classpath "io.realm:realm-gradle-plugin:2.2.2" in build.gradle and apply plugin: 'realm-android' and

dependencies {
    ...
    // Realm
    compile 'io.realm:realm-android:0.87.4'
    kapt "io.realm:realm-annotations:0.87.4"
    kapt "io.realm:realm-annotations-processor:0.87.4"
}

into build script in module app. It gives me another problem:

Error:(15, 48) Cannot access '<init>': it is public/*package*/ in 

'Builder'
/Volumes/Toshiba/Users/macuser/Development/Android/Exersises/MyApplication/app/src/main/java/realmtest/realm/RealmController.kt
Error:(27, 15) Unresolved reference: refresh
Error:(34, 15) Unresolved reference: clear
Error:(53, 23) Unresolved reference: allObjects

[KOTLIN] deleting /Volumes/Toshiba/Users/macuser/Development/Android/Exersises/MyApplication/app/build/tmp/kotlin-classes/debug on error

I can only build my project successfully if Book is written in java. Any suggestion how to make Realm and Kotlin work together?

AlexKost
  • 2,792
  • 4
  • 22
  • 42
  • You shouldn't add version 0.87.4 along with version 2.2.3 – EpicPandaForce Jan 15 '17 at 20:09
  • @EpicPandaForce, do you mean verson 2.2.3 of io.realm:realm-gradle-plugin? I've tried this and I've got "Could not find io.realm:realm-gradle-plugin:2.2.3" gradle build Error – AlexKost Jan 16 '17 at 05:08

2 Answers2

2

Use RealmClass annotation + RealmModel interface

import io.realm.Realm
import io.realm.RealmModel
import io.realm.annotations.PrimaryKey
import io.realm.annotations.RealmClass

@RealmClass
open class Example(
    @PrimaryKey open var Id : Long = 0,
    open var Field : String = ""
) : RealmModel

In module gradle file add

apply plugin: 'realm-android'

In project gradle file add

classpath "io.realm:realm-gradle-plugin:2.2.2"

No additional compile/kapt needed

  • Thanks, @Эльнар Берхеев, it helped! Just in case it would help someone: I had to make few other changes to build the project. – AlexKost Jan 16 '17 at 06:37
  • 1
    Theres no need for args in RealmConfiguretion.Build() call and I had to call Realm.init(context) before it. Also refresh(), clean() and allObjects() method were replaced with waitForChange(), delete() and where().findAll() methods respectively, as stated here: https://github.com/realm/realm-java/issues/2864. Cheers! – AlexKost Jan 16 '17 at 06:51
  • waitForChange() works differently than refresh(), use this instead http://stackoverflow.com/a/38839808/2413303 – EpicPandaForce Jan 16 '17 at 08:15
2

Realm java 4.1.0 has been released and most problem of Realm with Kotlin has resolved! You don't need to use open var just use var. You can test my sample project. My sample is for testing module in Realm object Server with kotlin.

Saeed
  • 101
  • 1
  • 1
  • 12