Each release of Kotlin includes compilers for the supported targets: JVM, JavaScript, and native binaries for supported platforms.
These compilers are used by:
The IDE, when you click the Compile or Run button for your Kotlin project.
Gradle, when you call gradle build in a console or in the IDE.
Maven, when you call mvn compile or mvn test-compile in a console or in the IDE.
You can also run Kotlin compilers manually from the command line as described in the Working with command-line compiler tutorial.
Kotlin compilers have a number of options for tailoring the compiling process.
Using a build script, you can specify additional compilation options. Use the compilerOptions property of a Kotlin compilation task for it. For example:
tasks.named("compileKotlin", org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask::class.java) {
compilerOptions {
freeCompilerArgs.add("-Xexport-kdoc")
}
}
tasks.named('compileKotlin', org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask.class) {
compilerOptions {
freeCompilerArgs.add("-Xexport-kdoc")
}
}
JVM compilation tasks are called compileKotlin for production code and compileTestKotlin for test code. The tasks for custom source sets are named according to their compile<Name>Kotlin patterns.
The names of the tasks in Android Projects contain build variant names and follow the compile<BuildVariant>Kotlin pattern, for example, compileDebugKotlin or compileReleaseUnitTestKotlin.
For both the JVM and Android projects, it's possible to define options using the project Kotlin extension DSL:
kotlin {
compilerOptions {
apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9)
}
}
kotlin {
compilerOptions {
apiVersion = org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9
}
}
Some important details to be aware of:
The android.kotlinOptions and kotlin.compilerOptions configuration blocks override each other. The last (lowest) block takes effect.
kotlin.compilerOptions configures every Kotlin compilation task in the project.
You can override the configuration applied by kotlin.compilerOptions DSL using the tasks.named<KotlinJvmCompile>("compileKotlin") { } (or tasks.withType<KotlinJvmCompile>().configureEach { }) approach.
JavaScript compilation tasks are called compileKotlinJs for production code, compileTestKotlinJs for test code, and compile<Name>KotlinJs for custom source sets.
To configure a single task, use its name:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask // ... val compileKotlin: KotlinCompilationTask<*> by tasks compileKotlin.compilerOptions.suppressWarnings.set(true)
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
// ...
tasks.named('compileKotlin', KotlinCompilationTask) {
compilerOptions {
suppressWarnings.set(true)
}
}
Note that with the Gradle Kotlin DSL, you should get the task from the project's tasks first.
Use the Kotlin2JsCompile and KotlinCompileCommon types for JS and common targets, respectively.
It is also possible to configure all of the Kotlin compilation tasks in the project:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
// ...
tasks.named<KotlinCompilationTask<*>>("compileKotlin").configure {
compilerOptions { /*...*/ }
}
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
// ...
tasks.named('compileKotlin', KotlinCompilationTask) {
compilerOptions { /*...*/ }
}
Here is a complete list of options for Gradle tasks:
Name |
Description |
Possible values |
Default value |
|---|---|---|---|
|
A property for configuring a list of opt-in compiler arguments |
|
|
|
Enables the progressive compiler mode |
|
|
Name |
Description |
Possible values |
Default value |
|---|---|---|---|
|
Generate metadata for Java 1.8 reflection on method parameters |
false |
|
|
Target version of the generated JVM bytecode |
"1.8", "9", "10", ..., "19". Also, see Types for compiler options |
"1.8" |
|
Don't automatically include the Java runtime into the classpath |
false |
|
|
|
|
|
Name |
Description |
Possible values |
Default value |
|---|---|---|---|
|
Report an error if there are any warnings |
false |
|
|
Don't generate warnings |
false |
|
|
Enable verbose logging output. Works only when the Gradle debug log level enabled |
false |
|
|
A list of additional compiler arguments. You can use experimental |
[] |
Use the attribute freeCompilerArgs to supply additional (including experimental) compiler arguments. You can add a single argument to this attribute or a list of arguments:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
// ...
val compileKotlin: KotlinCompilationTask<*> by tasks
// Single experimental argument
compileKotlin.compilerOptions.freeCompilerArgs.add("-Xexport-kdoc")
// Single additional argument, can be a key-value pair
compileKotlin.compilerOptions.freeCompilerArgs.add("-Xno-param-assertions")
// List of arguments
compileKotlin.compilerOptions.freeCompilerArgs.addAll(listOf("-Xno-receiver-assertions", "-Xno-call-assertions"))
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
// ...
tasks.named('compileKotlin', KotlinCompilationTask) {
compilerOptions {
// Single experimental argument
freeCompilerArgs.add("-Xexport-kdoc")
// Single additional argument, can be a key-value pair
freeCompilerArgs.add("-Xno-param-assertions")
// List of arguments
freeCompilerArgs.addAll(["-Xno-receiver-assertions", "-Xno-call-assertions"])
}
}
Name |
Description |
Possible values |
Default value |
|---|---|---|---|
|
Restrict the use of declarations to those from the specified version of bundled libraries |
"1.3" (DEPRECATED), "1.4" (DEPRECATED), "1.5", "1.6", "1.7", "1.8", "1.9" (EXPERIMENTAL) |
|
|
Provide source compatibility with the specified version of Kotlin |
"1.3" (DEPRECATED), "1.4" (DEPRECATED), "1.5", "1.6", "1.7", "1.8", "1.9" (EXPERIMENTAL) |
To set a language version, use the following syntax:
tasks
.withType<org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile>()
.configureEach {
compilerOptions
.languageVersion
.set(
org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9
)
}
tasks
.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask.class)
.configureEach {
compilerOptions.languageVersion =
org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9
}
Also, see Types for compiler options.
Name |
Description |
Possible values |
Default value |
|---|---|---|---|
|
Disable internal declaration export |
false |
|
|
Define whether the |
"call", "noCall". Also, see Types for compiler options |
"call" |
|
Generate .meta.js and .kjsm files with metadata. Use to create a library |
true |
|
|
The kind of JS module generated by the compiler |
"umd", "commonjs", "amd", "plain", "es". Also, see Types for compiler options |
"umd" |
|
Destination *.js file for the compilation result |
"<buildDir>/js/packages/<project.name>/kotlin/<project.name>.js" |
|
|
Generate source map |
true |
|
|
Embed source files into the source map |
"never", "always", "inlining". Also, see Types for compiler options |
|
|
Add variable and function names that you declared in Kotlin code into the source map. For more information on the behavior, see our compiler reference. |
"simple-names", "fully-qualified-names", "no". Also, see Types for compiler options |
"simple-names" |
|
Add the specified prefix to paths in the source map |
||
|
Generate JS files for specific ECMA version |
"v5" |
"v5" |
|
Translate primitive arrays to JS typed arrays |
true |
Some of the compilerOptions use the new types instead of the String type:
Option |
Type |
Example |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Learn more about:
© 2010–2023 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/gradle-compiler-options.html