I am trying to get Realm to work in my project. I have Kotlin with version 1.2.51 and Instant Run disabled.
In my project build.gradle
file I added the following dependency:
classpath "io.realm:realm-gradle-plugin:5.4.0"
In my App build.gradle
file I applied the Realm plugin as explained in the tutorial:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
When I am trying to build the project, I get the following error:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'
Caused by: org.gradle.api.InvalidUserDataException: Cannot change dependencies of configuration ':app:api' after it has been included in dependency resolution.
When I change the order of apply plugin
so that realm is directly after com.android.application
the project builds.
The problem then is, that Realm tells me on my first insert of a RealmObject, that the object is not contained in the object scheme.
RealmCustomer.kt
open class RealmCustomer(
@PrimaryKey
var id: String = "",
var firstname: String = "",
var lastname: String = "",
var addresses: RealmList<RealmAddress> = RealmList()
) : RealmObject() {}
RealmAddress.kt
open class RealmAddress(
var street: String = "",
var streetNr: String = "",
var city: String = "",
var countryCode: String = ""
) : RealmObject() {}
As far as I can see, there should not be a problem with it. In my Applications onCreate I call the following code:
Realm.init(this)
RealmConfiguration.Builder().name("my.realm").build().apply { Realm.setDefaultConfiguration(this) }
At some point later, I retrieve my Realm like this:
private val realm: Realm by lazy { Realm.getDefaultInstance() }
fun save(items: List<...>) {
realm.executeTransaction {
items.map {
val addressList = it.addresses?.map {
realm.createObject(RealmAddress::class.java).apply {
street = it.street
streetNr = it.streetNr
city = it.city
countryCode = it.countryCode
}
}
val cx = realm.createObject(RealmCustomer::class.java, it.id).apply {
firstname = it.firstname
lastname = it.lastname
}
addressList?.forEach { cx.addresses.add(it) }
}
}
}
This code with apply plugin: 'realm-android'
in the second place crashed with the Schema error: RealmException: RealmAddress is not part of the schema for this Realm
Thank you in advance.
Seems to be a bug in the Realm-Transformer 5.4.0, which happens only if
experimental = true
is enabled for Kotlin Android Extensions.EDIT: Using 5.4.1+ should solve this problem.
PREVIOUS ANSWER: You can use manually defined versions in the build.gradle file in the meantime:
As per docs.