gradlekotlin

How to pass compiler arguments to Kotlin Compiler with Gradle


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.


Solution

  • 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