如何在IntelliJ中设置'-Xuse-experimental = kotlin.experimental'

Mic*_*ael 11 intellij-idea compiler-warnings kotlin ktor

尝试在IntelliJ中构建Kotlin / Ktor应用程序时,出现以下形式的多个警告

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

输出。警告是指

@UseExperimental(KtorExperimentalLocationsAPI::class)
Run Code Online (Sandbox Code Playgroud)

所以预期通过设定以满足警告设置- >构建- >编译器- >科特林编译器- >附加的命令行参数-version -Xuse-实验= kotlin.Experimental。(-version已经在那里)。但是仍会生成警告。我如何满足呢?谢谢您的期待。

Max*_*ruz 24

在当前的 Kotlin 版本(1.5)中,您可以通过添加 @OptIn 注解来使用任何实验库

@OptIn(ExperimentalCoroutinesApi::class)
Run Code Online (Sandbox Code Playgroud)

但是,选择加入机制本身是实验性的,要使用它,您需要添加编译器参数。在 Kotlin Multiplatform 中最惯用的方法是:

kotlin.sourceSets.all {
    languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
}
Run Code Online (Sandbox Code Playgroud)

  • `languageSettings.useExperimentalAnnotation()` 最近已被 `languageSettings.optIn()` 取代 (16认同)

Jay*_*nez 15

您正在为项目使用Maven或Gradle吗?我有同样的问题与摇篮,但我可以通过将删除的警告-Xuse-实验值= kotlin.Experimental在我的build.gradle文件,里面tasks.withType

对于KtorExperimentalLocationsAPI,您可以尝试:

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


Bre*_*mer 7

build.gradle.kts

主要用于多平台项目

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

单一平台

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        // ...
        freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
    }
}
Run Code Online (Sandbox Code Playgroud)