我从 Realm 0.87.2 升级到 0.89.0,并收到错误:
RealmClass annotated object must implement RealmModel or derive from RealmObject
对于实际上扩展 RealmObject 的类。
根据网站上的说明(安装0.89.0),我已将插件依赖项添加到项目级别的build.gradle文件中:
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
}
将“应用插件”添加到应用程序级别 build.gradle 文件(并删除了过时的编译依赖项):
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'
}
这是我的一个扩展 RealmObject 的类(并触发虚假错误):
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;
}
}
这是 gradle 日志,显示错误:
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
我尝试过的事情:
- Android Studio 构建命令:创建项目、清理项目、重建项目
- .gradlew clean(按照网站说明)
最佳答案
我明白了。我正在使用 Realm-recycleview ( github for realm-recyclerview ),它拉入了早期版本的 Realm (0.87.2)。因此,我的项目实际上有两个版本的 Realm:0.87.2 和 0.89.0,并且首先加载旧版本。
我引用的是realm-recyclerviewer 0.9.12,github 站点上有更新版本的realm-recyclerview (0.9.14)。切换到新版本解决了问题。
关于java - 更新到 Realm 0.89.0(从 0.87.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36815909/