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

标签 android realm kotlin

我使用 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
}

等级错误:

Error:Execution failed for task javassist.
NotFoundException: com.theappsolution.conectric.model.SelfGeneration

最佳答案

Android Studio Instant Run 功能和 Realm 不兼容。并且使用此功能可能会导致许多不明显的错误以及编译时或运行时的错误。包括您正在举报的那个。

在 Android 中,当使用即时运行功能时,一些执行字节码操作的插件可能无法正常运行。在 documentation for Instant Run它说:

Certain third-party plugins that perform bytecode enhancement may cause issues with how Instant Run instruments your app. If you experience these issues, but want to continue using Instant Run, you should disable those plugins for your debug build variant. You can also help improve compatibility with third-party plugins by filing a bug

Realm talks about their change to use bytecode weaving在编译时,因此这种插件可能会在编译时或运行时与即时运行中断。果然在Realm issue 1832他们谈论了 Instant Run 的问题(Realm 问题跟踪器中有超过 28 issues with the phrase "Instant Run")。其他 Stack Overflow 问题也讨论了这些问题,例如:Realm causes my app to crash when trying to build a RealmConfiguration

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

关于android - 添加 Realm 类后无法构建 Android 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39472561/

相关文章:

android - 使用 LiveData 在 SeekBar 和 EditText 中取得进展

java - 我的Android程序有内存泄漏吗?

android - 如何将数据从 android 服务(从 onStart 或 OnCreate)传递到 android Activity

ios - 如何同时写入Realm的不同对象?

android - 使用Google NearBy Messages API时“<App> is having trouble with Google Play Services. Please try again”

android - Location.distanceTo 以米为单位返回值,但我需要英尺

android - Error :Could not find com. android.tools.build :gradle:3. 3.升级splunk:mint-android-sdk的gradle版本后出现问题

android - React Native 应用程序崩溃 : Unable to load script from assets 'index.android.bundle'

java - Realm android, RealmTransformer 好像没有应用

swift - 在 Realm、Swift 中按天从 NSDate 过滤