使用 kapt 时,Realm 未在 Java/Kotlin 项目中生成 RealmProxy 类

Realm not generating RealmProxy classes in Java/Kotlin project when using kapt

我正在尝试将领域集成到 Java/Kotlin 项目中,但 kapt 没有生成 RealmProxy 类,即使是 .java 文件

由于 this,我已经更新了领域,但仍然无法正常工作

Gradle 应用程序文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

repositories {
    maven {
        url 'https://maven.fabric.io/public'
    }
}

kapt {
    generateStubs = true
}

project.ext {
    daggerVersion = '2.2'
}

dependencies {

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    kapt "com.android.databinding:compiler:2.3.3"

    annotationProcessor group: 'com.google.dagger', name: 'dagger-compiler', version: "${daggerVersion}"
    compile group: 'com.google.dagger', name: 'dagger', version: "${daggerVersion}"
    kapt "com.google.dagger:dagger-compiler:${daggerVersion}"
    provided group: 'org.glassfish', name: 'javax.annotation', version: '10.0-b28'
    testAnnotationProcessor group: 'com.google.dagger', name: 'dagger-compiler', version: "${daggerVersion}"
    testCompile group: 'com.google.dagger', name: 'dagger', version: "${daggerVersion}"
    androidTestAnnotationProcessor group: 'com.google.dagger', name: 'dagger-compiler', version: "${daggerVersion}"
    androidTestCompile group: 'com.google.dagger', name: 'dagger', version: "${daggerVersion}"
}
...

项目文件:

buildscript {
    ext.kotlin_version = '1.1.3-2'

    repositories {
        jcenter()

        maven {
            url 'https://maven.fabric.io/public'
        }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.realm:realm-gradle-plugin:3.5.0"
    }
}
...

Java模型文件:

@RealmClass
public class NotificationEventsModel extends RealmObject {

    @PrimaryKey
    private long classPK;

    @SerializedName("userNotificationEventsCount")
    private int eventsCount;

}

Kotlin 模型文件:

@RealmClass open class PageModel(

    open var classPK: Long = 0,
    open var content: String? = null,
    @PrimaryKey
    open var id: Long = 0,

) : RealmObject() {

}

我认为您应该扩展 RealmObject:

open class PageModel(...) : RealmObject()

use the @RealmClass annotation and implement RealmModel:

@RealmClass 
open class PageModel(...) : RealmModel

也许与子类化一起使用的注解是 Realm 的绊脚石。

kotlin-kapt 插件指示 gradle 使用 kapt3。也许这是相关的,而 Realm 插件有一些问题吗?不应该是这种情况,因为从 开始似乎有效,但也适用于旧版本的插件(这使得它更加奇怪)。

我通过将 realm-android 插件声明为插件列表中的最后一个插件并从 annotationProcessor 切换到 Kapt 解决了这个问题,如下所示:

apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

kapt 'com.android.databinding:compiler:2.3.3'
compile group: 'com.google.dagger', name: 'dagger', version: "${daggerVersion}"
kapt group: 'com.google.dagger', name: 'dagger-compiler', version: "${daggerVersion}"
kaptTest group: 'com.google.dagger', name: 'dagger-compiler', version: "${daggerVersion}"
kaptAndroidTest group: 'com.google.dagger', name: 'dagger-compiler', version: "${daggerVersion}"

//构建 gradle(应用程序)

apply plugin: 'com.android.application' apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-android' apply plugin : 'realm-android' apply plugin :'kotlin-android-extensions'

realm { kotlinExtensionsEnabled = true }

//构建gradle(项目) 类路径 "io.realm:realm-gradle-plugin:5.9.0"