带有Kotlin的Android领域-包含在依赖项解析中后,无法更改配置的依赖项
我正在尝试让Realm在我的项目中工作.我的Kotlin版本为1.2.51,并且禁用了Instant Run.
I am trying to get Realm to work in my project. I have Kotlin with version 1.2.51 and Instant Run disabled.
在我的项目build.gradle
文件中,添加了以下依赖项:
In my project build.gradle
file I added the following dependency:
classpath "io.realm:realm-gradle-plugin:5.4.0"
在我的App build.gradle
文件中,我按照教程中的说明应用了Realm插件:
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.
当我更改apply plugin
的顺序时,以使领域直接位于com.android.application
之后,即会生成项目.
问题是,Realm在我第一次插入RealmObject时告诉我,该对象方案中不包含该对象.
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
RealmCustomer.kt
open class RealmCustomer(
@PrimaryKey
var id: String = "",
var firstname: String = "",
var lastname: String = "",
var addresses: RealmList<RealmAddress> = RealmList()
) : RealmObject() {}
RealmAddress.kt
RealmAddress.kt
open class RealmAddress(
var street: String = "",
var streetNr: String = "",
var city: String = "",
var countryCode: String = ""
) : RealmObject() {}
据我所知,它应该没有问题. 在我的Applications onCreate中,我调用以下代码:
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) }
}
}
}
此第二个带有apply plugin: 'realm-android'
的代码因架构错误而崩溃:RealmException: RealmAddress is not part of the schema for this Realm
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
谢谢.
似乎是Realm-Transformer 5.4.0中的错误,只有在为Kotlin Android扩展启用experimental = true
时才会发生.
Seems to be a bug in the Realm-Transformer 5.4.0, which happens only if experimental = true
is enabled for Kotlin Android Extensions.
编辑:使用5.4.1+应该可以解决此问题.
Using 5.4.1+ should solve this problem.
上一个答案:同时,您可以在build.gradle文件中使用手动定义的版本:
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"
}
按照 查看更多