android-在Kotlin项目中应用Realm插件会导致编译错误

我正在尝试将Realm添加到Android项目中,已经添加了项目级别的类路径依赖项,但是
应用插件:“ realm-android”
模块的build.gradle文件中的一行导致以下构建错误:

错误:任务’:data:compileDebugAndroidTestJavaWithJavac’的执行失败.
 java.lang.NoClassDefFoundError:
org / jetbrains / kotlin / annotation / AnnotationProcessorWrapper

没有这一行,应用程序将构建并运行良好,尚无Realm代码.

项目级别build.gradle:

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

buildscript {
    ext.kotlin_version = '1.0.6'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.realm:realm-gradle-plugin:2.3.0"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

模块build.gradle:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'realm-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'

        test {
            java.srcDirs = ['src/test/kotlin']
        }
    }
}

buildscript {
    ext.kotlin_version = '1.0.6'
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

kapt {
    generateStubs = true
}

dependencies {  
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(path: ':domain')

    compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.6'

    compile 'io.reactivex:rxjava:1.1.6'
    compile 'io.reactivex:rxkotlin:0.60.0'

    compile 'javax.inject:javax.inject:1'

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:2.6.3'
    testCompile 'com.nhaarman:mockito-kotlin:1.1.0'
}

解决方法:

要结束这个问题:
@ zaki50写的评论对我有用,它是:

在app / build.gradle中添加Apply插件:“ kotlin-kapt”.
我在应用插件“ kotlin-android”之后立即添加了它.

https://github.com/realm/realm-java/issues/4087跟踪此问题

上一篇:java-更深入地了解Realm的工作原理?


下一篇:android-RealmDB-React Native-无法在路径’.management’打开领域