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

我想添加Realm到一个Android项目,我已经添加了项目级别的classpath依赖项,但在我的模块的build.gradle文件中的apply plugin: 'realm-android'行导致下面的生成错误:

Error:Execution failed for task ':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写道为我工作是:

添加apply plugin: 'kotlin-kapt'到app / build.gradle。 我apply plugin: 'kotlin-android'后立即添加apply plugin: 'kotlin-android'

这个问题通过https://github.com/realm/realm-java/issues/4087进行跟踪