I am upgrading from Realm 0.87.2 to 0.89.0, and am getting the error:
RealmClass annotated object must implement RealmModel or derive from RealmObject
for classes that do, in fact, extend RealmObject.
Based on the instructions on the website (for installing 0.89.0), I have added the plug-in dependency to the project level build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'io.realm:realm-gradle-plugin:0.89.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Added the 'apply plugin' to the application level build.gradle file (and removed the obsolete compile dependency):
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.winterberrysoftware.luthierlab"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
repositories {
maven {
// Stetho-Realm browser
url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo'
}
}
ext {
supportLibVersion = "23.3.0"
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile "com.android.support:support-annotations:$supportLibVersion"
// Android JUnit Runner
androidTestCompile 'com.android.support.test:runner:0.4.1'
// JUnit4 Rules
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Espresso core
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile "com.android.support:design:$supportLibVersion"
compile "com.android.support:support-v4:$supportLibVersion"
compile "com.android.support:recyclerview-v7:$supportLibVersion"
// Realm-RecyclerView
// SuperSLiM should automatically load from github when realm-recyclerview
// loads. For some reason, the revision tag in the realm-recyclerciew
// package is ed0ba4b4d2 and github won't find it with that tag.
// If I lop off the trailing '2' and load it explicitly, everything works
// fine.
compile 'com.github.TonicArtos:SuperSLiM:ed0ba4b4'
compile 'com.github.thorbenprimke:realm-recyclerview:0.9.12'
// Stetho-Realm browser (for debug only)
debugCompile 'com.facebook.stetho:stetho:1.2.0'
debugCompile 'com.uphyca:stetho_realm:0.8.0'
}
Here is one of my classes that extends RealmObject (and triggers the spurious error):
package com.winterberrysoftware.luthierlab.model;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
/**
* The RealmProject class manages a Realm object with the details
* of a particular project.
*/
public class RealmProject extends RealmObject {
@PrimaryKey
private String name;
private RealmShape realmShape;
public RealmProject() {
// required no-argument public constructor
}
public RealmProject(String name, RealmShape realmShape) {
this.name = name;
this.realmShape = realmShape;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public RealmShape getRealmShape() {
return realmShape;
}
public void setRealmShape(RealmShape realmShape) {
this.realmShape = realmShape;
}
}
Here is the gradle log, showing the error:
Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources, :app:assembleDebug]
:app:clean
... lines omitted ...
:app:generateDebugAndroidTestSources
:app:compileDebugJavaWithJavac
C:\Users\Clo\Documents\Java Projects\AndroidStudioProjects\LuthierLab\app\src\main\java\com\winterberrysoftware\luthierlab\model\RealmProject.java
Error:(15, 8) error: A RealmClass annotated object must implement RealmModel or derive from RealmObject
Note: Processing class RealmProject
C:\Users\Clo\Documents\Java Projects\AndroidStudioProjects\LuthierLab\app\src\main\java\com\winterberrysoftware\luthierlab\model\RealmShape.java
Error:(14, 8) error: A RealmClass annotated object must implement RealmModel or derive from RealmObject
Note: Processing class RealmShape
Note: Creating DefaultRealmModule
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 5.15 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console
Things I have tried:
I figured it out. I am using realm-recycleview ( github for realm-recyclerview ), and it was pulling in the earlier version of Realm (0.87.2). So my project effectively had two versions of Realm: 0.87.2 and 0.89.0, and the older version was being loaded first.
I was referencing realm-recyclerviewer 0.9.12, there is a newer version of realm-recyclerview (0.9.14) at the github site. Switching to the newer version fixed the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.