Kotlin gradle multi module example


#1

Is there an example of a kotlin multi-module gradle project? I am desperately striving to get all parts in place, so that:

  1. I don’t have to repeat the buildscript {} block in every gradle project
  2. to use kotlin 1.1 and jvm 1.8 for all projects, without repeating kotlinCompile{} and kotlinTestCompile{} blocks

Thanks.


#2

Here is my solution:

buildscript {
    ext.kotlin_version = '1.1.2-2'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}


allprojects{
    apply plugin: "kotlin"

    compileKotlin {
        kotlinOptions {
            jvmTarget = "1.8"
            javaParameters = true
        }
    }

    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    }

}

This way kotlin plugin is automatically applied for the root project and all subprojects. Another way is to use new plugin declaration from kotlin documentation:

plugins {
    id "org.jetbrains.kotlin.jvm" version "<version to use>"
}

It does not work from subprojects or allprojects blocks.


#3

Thank you, that is exactly what I came up with. Yet Intellij insists that it needs to configure the Kotlin plugin still, and adds the buildscript {} to every build script. Also, when using jvmTarget 1.8, Intellij randomly fails to compile the code, saying that I’m trying to call 1.8 code from something compiled with 1.6.
I tried the plugins{} approach, but Intellij still insists that I have Kotlin unconfigured in the modules. Do you also encounter this behaviour?


#4

Yes I do. I just ignored IDE warning. Everything works fine. I also mostly use gradle tasks instead of IDE.