Realm Kotlin ไม่ได้อยู่ในสคีมา

ฉันพยายามใช้ Realm.io เพื่อจัดเก็บข้อมูลใน Android ที่ฉันมี RealmClass

@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

at 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 ใด ๆ ตัวอย่าง Kotlin ที่โพสต์บน GitHub มีโมเดลที่เขียนด้วย Java ล้วนๆ


person Mariusz    schedule 05.12.2015    source แหล่งที่มา
comment
คุณได้ตั้งค่า proguard แต่คุณไม่ได้เปิดใช้งานมัน release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }   -  person Maheshwar Ligade    schedule 05.12.2015
comment
ยังคงส่งข้อยกเว้น   -  person Mariusz    schedule 05.12.2015
comment
เพิ่มกฎขอบเขตในไฟล์ pro-guard   -  person Maheshwar Ligade    schedule 05.12.2015
comment
ฉันเคยทำมันมาก่อน   -  person Mariusz    schedule 05.12.2015
comment
เพิ่มหนึ่งบรรทัด -เก็บคลาส io.realm.annotations.RealmModule   -  person Maheshwar Ligade    schedule 05.12.2015
comment
realm doc บอกว่าจะเพิ่มบรรทัดนี้ ฉันโพสต์ไฟล์ proguard ของฉันแล้ว   -  person Mariusz    schedule 05.12.2015
comment
คุณแน่ใจหรือไม่ว่าได้เปิดใช้งานการประมวลผลคำอธิบายประกอบแล้ว   -  person Maheshwar Ligade    schedule 05.12.2015
comment
ฉันไม่พบการประมวลผลคำอธิบายประกอบในการตั้งค่า android studio   -  person Mariusz    schedule 05.12.2015


คำตอบ (3)


"java.lang.NoClassDefFoundError: io/realm/คำอธิบายประกอบ/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

person ShadowRogue7    schedule 07.01.2016

เพื่อให้ 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"
person Christian Melchior    schedule 07.12.2015
comment
ไล่ระดับ Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > java.lang.NoClassDefFoundError: io/realm/annotations/RealmClass - person Mariusz; 07.12.2015
comment
ยังจำเป็นต้องใช้ kapt "io.realm:realm-annotations-processor:$realm_version" kapt "io.realm:realm-annotations:$realm_version" เพื่อให้ Realm ทำงานร่วมกับ Kotlin ได้หรือไม่ - person lagos; 03.06.2016
comment
ไม่ เราจะเพิ่มสิ่งนั้นโดยอัตโนมัติเมื่อคุณใช้ปลั๊กอิน Realm - person Christian Melchior; 03.06.2016

สำหรับฉัน ฉันเพิ่ม @RealmClass ให้กับทุกรุ่นและแก้ไขแม้ว่าจะ ก็ไม่จำเป็น.

ฉันคิดว่ามันบังคับให้รันโปรเซสเซอร์ Realm อย่างถูกต้องโดยใช้คำอธิบายประกอบ

หากต้องการตรวจสอบว่าโมเดล RealmObject ได้รับการประมวลผลหรือไม่ เอาต์พุตคอนโซล Gradle ของคุณ (คุณสามารถเปิดได้ใน Android Studio ที่แผงสลับด้านล่างขวา) จะต้องแสดงบันทึก การประมวลผลโมเดลสีแดง ดังนี้

: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

person Youngjae    schedule 16.05.2016