带有Kotlin的Android领域-在依赖关系解析中包含配置后,无法更改其依赖关系

时间:2018-07-24 14:53:25

标签: android kotlin realm

我正在尝试让Realm在我的项目中工作。我的Kotlin版本为1.2.51,并且禁用了Instant Run。

在我的项目build.gradle文件中,添加了以下依赖项:

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

在我的应用build.gradle文件中,按照教程中的说明应用了Realm插件:

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

当我尝试构建项目时,出现以下错误:

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.

当我更改apply plugin的顺序时,以使领域直接位于com.android.application之后,即会生成项目。 问题是,Realm在我第一次插入RealmObject时告诉我,对象方案中不包含该对象。

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() {}

据我所知,应该没有问题。 在我的Applications onCreate中,我调用以下代码:

Realm.init(this)
RealmConfiguration.Builder().name("my.realm").build().apply { Realm.setDefaultConfiguration(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) }
        }
    }
}

此代码第二apply plugin: 'realm-android'崩溃,发生架构错误:RealmException: RealmAddress is not part of the schema for this Realm

谢谢。

1 个答案:

答案 0 :(得分:3)

似乎是Realm-Transformer 5.4.0中的错误,只有在为Kotlin Android扩展启用experimental = true时才会发生。

编辑:使用5.4.1+应该可以解决此问题。


上级答案:同时,您可以在build.gradle文件中使用手动定义的版本:

 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"
 }

按照docs