使用kapt

时间:2017-07-26 14:26:57

标签: java android kotlin apt kapt

我正在尝试将域集成到Java / Kotlin项目中,但kapt甚至不为.java文件生成RealmProxy类

我因为this而更新了领域,但仍无法正常工作

Gradle app file:

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() {

}

4 个答案:

答案 0 :(得分:1)

我相信你应该延长RealmObject

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

use the @RealmClass annotation and implement RealmModel

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

也许与子类一起使用的注释是崛起的领域。

答案 1 :(得分:1)

我通过将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}"

答案 2 :(得分:0)

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

答案 3 :(得分:0)

//构建gradle(应用)

  

应用插件:“ com.android.application”    应用插件:“ kotlin-kapt”

     

应用插件:“ kotlin-android”   Apply插件:“ realm-android”   Apply插件:'kotlin-android-extensions'

     

领域{       kotlinExtensionsEnabled = true   }

// build gradle(项目)  类路径“ io.realm:realm-gradle-plugin:5.9.0”