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.
To opt in to using an API in a module, compile it with the argument -opt-in , specifying the fully qualified name of the opt-in requirement annotation of the API you use: -opt-in=org. mylibrary.
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"] }
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") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With