'How to set '-Xuse-experimental=kotlin.experimental' in IntelliJ

while trying to build a Kotlin/Ktor application in IntelliJ, multiple warnings of the form

Warning:(276, 6) Kotlin: This class can only be used with the compiler argument '-Xuse-experimental=kotlin.Experimental'

are output. The warnings refer to

@UseExperimental(KtorExperimentalLocationsAPI::class)

so I expected to satisfy the warning by setting Settings -> Build -> Compiler -> Kotlin Compiler -> Additional command line parameters to -version -Xuse-experimental=kotlin.Experimental. (-version was already there). But the warning is still generated. How do I satisfy it? Thanks in expectation.



Solution 1:[1]

Are you using Maven or Gradle for your project? I had the same issue with Gradle, but I was able to remove the warnings by putting the -Xuse-experimental=kotlin.Experimental in my build.gradle file, inside a tasks.withType.

For KtorExperimentalLocationsAPI you could try:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions.freeCompilerArgs += ["-Xuse-experimental=io.ktor.locations.KtorExperimentalLocationsAPI"]
}

Solution 2:[2]

In the current Kotlin version (1.5), you can use any experimental library by adding @OptIn annotation

@OptIn(ExperimentalCoroutinesApi::class)

However, the opt-in mechanism is experimental itself, and to use it you will need to add a compiler argument. The most idiomatic way to do that in Kotlin Multiplatform is:

kotlin.sourceSets.all {
    languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
}

Solution 3:[3]

Edit for Graddle 6.5.1 and Ktor 1.3.2. Answer by @jay-janez looks like that:

tasks.withType(KotlinCompile::class).all {
    kotlinOptions.freeCompilerArgs += "-Xuse-experimental=io.ktor.util.KtorExperimentalAPI"
}

Solution 4:[4]

Old question, but there has been some changes.

-Xuse-experimental has been replaced by -Xopt-in. See the docs.

Also for Grade Kotlin DSL you can use this syntax:

tasks.withType<KotlinCompile> {
    kotlinOptions.freeCompilerArgs = listOf(
        "-Xopt-in=io.ktor.util.KtorExperimentalAPI"
    )
}

Solution 5:[5]

Go to your app/build.gradle file and add the below task on the bottom part

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
    kotlinOptions {
        freeCompilerArgs += "-Xopt-in=io.ktor.util.KtorExperimentalAPI"
    }
}

Solution 6:[6]

In build.gradle.kts

Mostly on multiplatform projects

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions{
        kotlin.sourceSets.all {
            languageSettings.optIn("kotlin.RequiresOptIn")
        }
    }
}

Single platform

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        // ...
        freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Max Cruz
Solution 3 Innokenty
Solution 4 GoranSH
Solution 5 Jim Ovejera
Solution 6