【问题标题】:Updating to Realm 0.89.0 (from 0.87.2)更新到 Realm 0.89.0(从 0.87.2)
【发布时间】:2016-04-23 20:15:59
【问题描述】:

我正在从 Realm 0.87.2 升级到 0.89.0,并收到错误消息:

RealmClass 注解对象必须实现 RealmModel 或派生自 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 构建命令:Make Project、Clean Project、Rebuild Project
  • .gradlew clean(根据网站说明)

【问题讨论】:

    标签: java android realm


    【解决方案1】:

    我想通了。我正在使用 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)。切换到较新的版本解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多