44

I'm compiling a Kotlin library jar with Gradle using the Kotlin gradle plugin:

apply plugin: 'kotlin'

I'm trying to find a way to pass a simple -include-runtime compiler arguments to the kotlin compiler. I can't seem to find any documentation on this at all. I tried mimicking the java plugin, but that didn't seem to work. Here is some documentation about working with the command line compiler, but the gradle documentation doesn't mention anything about passing compiler arguments.

spierce7
  • 14,797
  • 13
  • 65
  • 106
  • Hi! How did you end up solving this problem? :D – Jeel Shah Nov 15 '17 at 23:21
  • I asked this a few years ago. My need to get this working went away. Looking at the gradle plugin documentation http://kotlinlang.org/docs/reference/using-gradle.html I see that there is a new `freeCompilerArgs` that I might use to pass the necessary arguments. If I couldn't get that working I'd look into building a fat jar or something similar. – spierce7 Nov 16 '17 at 02:38

3 Answers3

55

You can specify compiler args inside kotlinOptions closure on tasks of KotlinCompile type. For all of them, for instance:

allprojects {
    ...

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            jvmTarget = '1.6'
            freeCompilerArgs += '-include-runtime'
        }
    }
}

Kotlin docs: using Gradle

Miha_x64
  • 5,973
  • 1
  • 41
  • 63
2

Try this:

compileKotlin {
    kotlinOptions.includeRuntime = true
}

UPD btw this exact option includeRuntime couldn't work because it is not Gradle way. There are many options to build jar with dependencies in Gradle: Gradle – Create a Jar file with dependencies, Gradle Shadow

Sergey Mashkov
  • 4,630
  • 1
  • 27
  • 24
  • You can see it in kotlin's plugin tests for example: https://github.com/JetBrains/kotlin/search?l=gradle&q=compileKotlin&utf8=%E2%9C%93 – Sergey Mashkov Aug 07 '15 at 14:27
  • @SergeyMashkov This appears not to work for the command `./gradlew clean assemble`. Is there a command that it does work for? On the above comment you left a link to show examples of this being used. I've gone through all the links, and there are no examples of anyone using the code you posted. – spierce7 Aug 07 '15 at 15:42
0

If anyone is using the kotlin DSL you can try this too:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions.includeRuntime = true
user3681304
  • 629
  • 6
  • 23