How to supress OptIn warnings in Gradle and during Git commit code analysis

I’m using OptIn features quite liberally in my code base. Because of this, I get a lot of warnings of the following form while compiling:

w: /path/to/file.kt: (73, 2): This class can only be used with the compiler argument '-Xopt-in=kotlin.RequiresOptIn'

Is there a way to disable these warnings when compiling with Gradle? They tend to obscure relevant warnings when looking at the compile output.

The same goes for the code analysis warnings when I do a git commit. I like this feature when doing a commit, but since I have the @OptIn clause sin plenty of my files, it keeps telling me that there a warnings, which requires me to look at them individually and then reissue the commit.

I’d like to have a way to tell the compiler and code analyser to never warn me about the fact that I’m using the OptIn features.

tasks.withType<KotlinCompile> {
	kotlinOptions {
		freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
	}
}
1 Like

Thank you. As this is a multiplatform project, I added the following to the JVM and Linux targets (those are the only ones I use):

jvm() {
    compilations.main.kotlinOptions {
        jvmTarget = "11"
        freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
    }
    compilations.test.kotlinOptions {
        jvmTarget = "11"
        freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
    }
}

And the following section for the Linux target:

linuxX64("linux") {
    compilations.main.kotlinOptions {
        freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
    }
    compilations.test.kotlinOptions {
        freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
    }
}

After doing this, I seem to get less warnings, but I still get a set of them from the following target:

> Task :array:compileKotlinMetadata
w: ATTENTION!
This build uses unsafe internal compiler arguments:

-XXLanguage:+InlineClasses

This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!

w: /home/elias/prog/array_kotlin/array/src/commonMain/kotlin/array/common.kt: (49, 2): This class can only be used with the compiler argument '-Xopt-in=kotlin.RequiresOptIn'
w: /home/elias/prog/array_kotlin/array/src/commonMain/kotlin/array/common.kt: (128, 2): This class can only be used with the compiler argument '-Xopt-in=kotlin.RequiresOptIn'
w: /home/elias/prog/array_kotlin/array/src/commonMain/kotlin/array/io.kt: (41, 2): This class can only be used with the compiler argument '-Xopt-in=kotlin.RequiresOptIn'
w: /home/elias/prog/array_kotlin/array/src/commonMain/kotlin/array/parser.kt: (43, 6): This class can only be used with the compiler argument '-Xopt-in=kotlin.RequiresOptIn'

Do I need to add the flag to some other section too?

In addition to this, I also have a set of these warnings that I would like to disable:

> Task :array:compileKotlinJvm
w: ATTENTION!
This build uses unsafe internal compiler arguments:

-XXLanguage:+InlineClasses

This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!

Is there a way to disable this as well?

1 Like

Seems like you missed adding it for the common code.

w: ATTENTION!
This build uses unsafe internal compiler arguments:

-XXLanguage:+InlineClasses

This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!

Sadly I don’t know any way of removing this warning. I’m not sure it’s possible, but I can live with it since it is only displayed once per build/module and not for every inline class.

1 Like

Thank you again for your help. I didn’t understand what you meant by compiler options for common code, as I would have expected the ones for each target to be applied.

Your suggestion did lead me to search for other targets for which I can add compiler options, and while “common” was not one that worked I did find another one that stops the warnings:

metadata() {
    compilations.main.kotlinOptions {
        freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
    }
}

I have no idea what this one actually does though. It’s not documented in the Kotlin Gradle documentation.

3 Likes

If you are using kotlin multiplatform, a simpler/more idiomatic way to enable the RequiresOptIn annotation is:

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