Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass compiler arguments to Kotlin Compiler with Gradle

Tags:

gradle

kotlin

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.

like image 251
spierce7 Avatar asked Aug 06 '15 05:08

spierce7


People also ask

Does gradle work with Kotlin?

Gradle is a build system that is very commonly used in the Java, Android, and other ecosystems. It is the default choice for Kotlin/Native and Multiplatform when it comes to build systems.

How do I change my Kotlin compiler version?

Go to Intellij Preferences -> Build, Execution, Deployment -> Kotlin Compiler. Update Language version and Api version to the one you wish. This should be the accepted answer.

Which compiler is used for Kotlin?

The Kotlin compiler for JVM compiles Kotlin source files into Java class files. The command-line tools for Kotlin to JVM compilation are kotlinc and kotlinc-jvm . You can also use them for executing Kotlin script files.

How to build a Kotlin project with Gradle?

In order to build a Kotlin project with Gradle, you should apply the Kotlin Gradle plugin to your project and configure the dependencies. Apply the Kotlin Gradle plugin by using the Gradle plugins DSL.

How do I enable compiler plugins for Kotlin?

Pass an option to a Kotlin compiler plugin. Available plugins and their options are listed in the Tools > Compiler plugins section of the documentation. Provide source compatibility with the specified version of Kotlin. Allow using declarations only from the specified version of Kotlin bundled libraries.

How do I pass compiler arguments with delimiter characters in Kotlin?

On Windows, when you pass compiler arguments that contain delimiter characters (whitespace, =, ;, , ), surround these arguments with double quotes ( " ). $ kotlinc.bat hello.kt -include-runtime -d "My Folder\hello.jar" The following options are common for all Kotlin compilers. Display the compiler version.

How do I add compiler arguments in IntelliJ IDEA?

In IntelliJ IDEA, write in the compiler arguments in the Additional command line parameters text box in Settings/Preferences | Build, Execution, Deployment | Compiler | Kotlin Compiler. If you're using Gradle, specify the compiler arguments in the kotlinOptions property of the Kotlin compilation task. For details, see Gradle.


2 Answers

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

like image 136
Miha_x64 Avatar answered Oct 20 '22 18:10

Miha_x64


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

like image 2
Sergey Mashkov Avatar answered Oct 20 '22 17:10

Sergey Mashkov