34

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.

7 Answers7

29

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"]
}
Jay Yanez
  • 529
  • 4
  • 8
  • 1
    how about if we using a lot of experimental features? e.g: `UnstableDefault`, and `ImplicitSerialization`. Does the argument would look like this? `kotlinOptions.freeCompilerArgs += ["-Xuse-experimental=io.ktor.locations.KtorExperimentalLocationsAPI, UnstableDefault, Implicit, etc"] ` – mochadwi May 26 '19 at 19:04
  • 1
    Updated my comment above: when using multiple args, you can do so: `kotlinOptions.freeCompilerArgs += [ "-Xuse-experimental=io.ktor.locations.KtorExperimentalLocationsAPI", "-Xuse-experimental=kotlinx.serialization.UnstableDefault", "-Xuse-experimental=kotlinx.serialization.ImplicitSerialization"]` – mochadwi May 26 '19 at 19:21
23

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")
}
Max Cruz
  • 1,215
  • 13
  • 17
  • 16
    `languageSettings.useExperimentalAnnotation()` has recently been replaced by `languageSettings.optIn()` – Joffrey Sep 08 '21 at 07:29
7

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"
    }
}
Breimer
  • 506
  • 8
  • 14
4

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"
}
Innokenty
  • 3,001
  • 1
  • 27
  • 30
4

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"
    }
}
Jim Ovejera
  • 739
  • 6
  • 10
3

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"
    )
}
GoranSH
  • 96
  • 5
  • Getting `Could not get unknown property 'withType' for task set of type`. I guess the place where I'm adding this is wrong or this has changed with the new version of Gradle. – acarlstein Sep 19 '21 at 18:14
1

You can add -opt-in=kotlin.RequiresOptIn compiler argument in the build

  kotlinOptions {
        freeCompilerArgs += [
                "-Xopt-in=kotlin.RequiresOptIn"
        ]
    }
Elie Danhash
  • 61
  • 1
  • 4