Realm for Android with Kotlin - Cannot change Depe

2020-03-24 04:21发布

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.

1条回答
不美不萌又怎样
2楼-- · 2020-03-24 04:58

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:

 buildscript {
    ext.kotlin_version = '1.2.51'
    ext.realm_version = '5.4.0'

    repositories {
        jcenter()
        mavenCentral()
    }

   dependencies {
       classpath "io.realm:realm-transformer:5.1.0"
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
   }
 }

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

 import io.realm.transformer.RealmTransformer
 android.registerTransform(new RealmTransformer(project))

 dependencies {
   implementation "io.realm:realm-annotations:$realm_version"
   implementation "io.realm:realm-android-library:$realm_version"
   implementation "io.realm:realm-android-kotlin-extensions:$realm_version" {
       exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk7"
   }
   kapt "io.realm:realm-annotations-processor:$realm_version"
 }

As per docs.

查看更多
登录 后发表回答