Search code examples
androidandroid-gradle-pluginrealmkotlin

Applying Realm plugin in Kotlin project results in compilation error


I'm trying to add Realm to an Android project, I've added the project level classpath dependency, but putting the apply plugin: 'realm-android' line in my module's build.gradle file results in the following build error:

Error:Execution failed for task ':data:compileDebugAndroidTestJavaWithJavac'. java.lang.NoClassDefFoundError: org/jetbrains/kotlin/annotation/AnnotationProcessorWrapper

Without that line, the application builds and runs fine, there is no Realm code in it yet.

Project level 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
}

Module 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'
}

Solution

  • To close this question: The comment @zaki50 wrote worked for me which is:

    Adding apply plugin: 'kotlin-kapt' to app/build.gradle. I added it right after apply plugin: 'kotlin-android'.

    This issue is tracked by https://github.com/realm/realm-java/issues/4087