【问题标题】:Realm Kotlin not in schema领域 Kotlin 不在架构中
【发布时间】:2015-12-05 10:02:23
【问题描述】:

我尝试使用 Realm.io 将数据存储在我有RealmClass的android 中

@RealmClass
public open class Alarm : RealmObject() {
   @Required
   public open var hourOfDay: Int? = null
   @Required
   public open var minute: Int? = null
   @Required
   public open var days: BooleanArray? = null

   public open var name: String? = null
}

onCreate 我尝试添加一些测试数据

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mRealm = Realm.getInstance(activity);

    mRealm.beginTransaction()

    var a = mRealm.createObject(Alarm::class.java)
    a.hourOfDay = 12
    a.minute = 1;
    a.days = booleanArrayOf(true, true, true, true, true, false, false);
    a.name = "Test${System.currentTimeMillis()}"

    mRealm.commitTransaction()
}

但我得到了例外。

> java.lang.IllegalArgumentException: Alarm is not part of the schema for this Realm

var a = mRealm.createObject(Alarm::class.java)

我已经设置了领域规则

-keep class io.realm.annotations.RealmModule
-keep @io.realm.annotations.RealmModule class *
-keep class io.realm.internal.Keep
-keep @io.realm.internal.Keep class *
-dontwarn javax.**
-dontwarn io.realm.**

> proguard-rules.pro

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
    realmFlavor {
        proguardFile 'proguard-rules.pro'
    }
}

但这并没有带来解决方案

结论

到目前为止,看起来我们必须用 Java、Realm 和任何 ORM 库编写模型,发布在 GitHub 上的 Kotlin 示例都有用纯 Java 编写的模型

【问题讨论】:

  • 您已设置 proguard 但未启用它 release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
  • 仍然抛出异常
  • 在 pro-guard 文件中添加领域规则
  • 我以前做过
  • 领域文档已经说过要添加这一行,我发布了我的 proguard 文件

标签: android realm kotlin


【解决方案1】:

“java.lang.NoClassDefFoundError: io/realm/annotations/RealmClass”

如果您遇到上述错误,您很可能需要在 build.gradle 中添加类似的内容。 kapt 的底部两行是关键。

//Realm
compile 'io.realm:realm-android:0.87.1'
kapt "io.realm:realm-annotations:0.87.1"
kapt "io.realm:realm-annotations-processor:0.87.1"

根据本期底部的评论:https://github.com/realm/realm-java/issues/509

【讨论】:

    【解决方案2】:

    要让 Kotlin 与 Realm 的注释处理器一起工作,您需要在 build.gradle 中进行以下设置

    compile "io.realm:realm-android-library:0.86.0@aar"
    compile "io.realm:realm-annotations:0.86.0"
    kapt "io.realm:realm-annotations-processor:0.86.0"
    

    【讨论】:

    • gradle throw Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > java.lang.NoClassDefFoundError: io/realm/annotations/RealmClass
    • 是否还需要使用kapt "io.realm:realm-annotations-processor:$realm_version" kapt "io.realm:realm-annotations:$realm_version" 才能使 Realm 与 Kotlin 一起使用?
    • 不,我们会在您使用 Realm 插件时自动添加。
    【解决方案3】:

    对我来说,我将@RealmClass 添加到所有模型并修复了it is not required

    我认为它强制使用注释器正确运行 Realm 处理器。

    要检查 RealmObject 模型是否被处理,您的 Gradle 控制台输出(您可以在 Android Studio 的右下角切换面板中打开它)必须显示 Red-colored model processing 日志,如下所示。

    :app:compileDebugJavaWithJavac
    Full recompilation is required because at least one of the classes of removed jar 'kotlin-stdlib-1.0.0.jar' requires it. Analysis took 1.572 secs.
    
    Note: Processing class ModelSomething
    Note: Processing class ModelAnotherThing
    Note: Creating DefaultRealmModule
    Note: Recompile with -Xlint:deprecation for details.
    Note: Some input files use unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    
    :app:transformClassesWithRealmTransformerForDebug
    :app:transformClassesWithInstantRunVerifierForDebug
    :app:transformClassesWithJavaResourcesVerifierForDebug
    

    Note: 行是红色的,您可以看到 Processing class ModelSomething 消息。

    您的所有模型都应列为此消息。

    此问题已作为可疑错误提交:https://github.com/realm/realm-java/issues/2822

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      相关资源
      最近更新 更多