添加Realm类后无法构建Android项目



我已经用Kotlin创建了SelfGeneration Realm类,并添加了该项目不再构建之后。我该如何解决这个问题?

@RealmClass open class SelfGeneration() : BaseRealmObject {
    @PrimaryKey override var id: Int? = null
    open var type: ItemType? = null
    open var model: String? = null
    open var watt: Int? = null
    companion object {
        fun getById(id: Int): SelfGeneration {
            val realm = Realm.getDefaultInstance()
            val selfGeneration = realm.where(SelfGeneration::class.java)
                    .equalTo(BaseRealmObject.Field.ID, id)
                    .findFirst()
            return realm.copyFromRealm(selfGeneration)
        }
    }
}

依赖性:

dependencies {
    classpath com.android.tools.build:gradle:2.1.3
    classpath io.realm:realm-gradle-plugin:1.2.0
    classpath com.neenbedankt.gradle.plugins:android-apt:1.8
    classpath "com.fernandocejas.frodo:frodo-plugin:0.8.1
}

Gradle错误:

错误:执行javassist任务失败。
NotFoundException: com.theappsolution.conectric.model.SelfGeneration

Android Studio Instant Run功能与Realm不兼容。使用此特性可能会导致许多不明显的错误,并增加编译时间或运行时。包括你正在报道的那个。

在Android中,当使用Instant Run功能时,一些做字节码操作的插件可能无法正常工作。在Instant Run的文档中说:

某些执行字节码增强的第三方插件可能会导致即时运行如何检测您的应用程序的问题。如果您遇到这些问题,但希望继续使用即时运行,您应该在调试构建变体中禁用这些插件。您还可以通过提交bug

来帮助改进与第三方插件的兼容性。

Realm谈到了他们在编译时使用字节码编织的改变,所以这是一种可能在编译时或运行时与Instant Run发生冲突的插件类型。在1832年的realms issue中,他们讨论了即时运行的问题(在realms issue跟踪器中有超过28个问题是关于"即时运行"的)。其他Stack Overflow问题也讨论了这些问题,例如:Realm导致我的应用程序在试图构建RealmConfiguration时崩溃。

当前唯一的解决方案是在Android Studio首选项中禁用即时运行功能,清理项目,然后重新构建/运行。

最新更新