Griill
Griill

Reputation: 51

Cannot build Android project after adding Realm class

I've created SelfGeneration Realm class with Kotlin and after adding that the project is no longer building. How do I fix this?

@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:

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 error:

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

Upvotes: 2

Views: 340

Answers (1)

Jayson Minard
Jayson Minard

Reputation: 86016

Android Studio Instant Run feature and Realm are not compatible. And using this feature can cause many non-obvious errors and compile time or runtime. Including the one you are reporting.

In Android, when using Instant Run feature some plugins that do byte code manipulation may not function correctly. In the documentation for Instant Run it says:

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 at compile time so this is the type of plugin that might break with Instant Run either at compile time or at runtime. And sure enough in the Realm issue 1832 they talk about problems with Instant Run (there are over 28 issues with the phrase "Instant Run" in the Realm issue tracker). Also other Stack Overflow questions talks about these problems, such as: Realm causes my app to crash when trying to build a RealmConfiguration.

The only current solution is to disable the Instant Run feature in Android Studio preferences, clean your project, and then build/run again.

Upvotes: 2

Related Questions