HomeAndroidadd opt-in compiler argument in construct.gradle?

add opt-in compiler argument in construct.gradle?


After I tried to make use of androidx.compose.ui.platform.LocalSoftwareKeyboardController in my RSS Feed Reader app, it turned out it’s an @ExperimentalComposeUiApi which has this @RequiresOptIn() annotation.

@RequiresOptIn("This API is experimental and is prone to change sooner or later.")
annotation class ExperimentalComposeUiApi

Which means, so as to use this LocalSoftwareKeyboardController, I would like so as to add @OptIn() annotation within the class or else you will notice a warning/error like under which is outlined by the RequiresOptIn() annotation above.

This API is experimental and is prone to change sooner or later.

So, I added this @OptIn(ExperimentalComposeUiApi::class).

@OptIn(ExperimentalComposeUiApi::class)
@Composable
enjoyable ArticlesTopBar(
    navHostController: NavHostController,
    viewModel: MainViewModel,
    onArticlesSearch: () -> Unit,
) {
    val keyboardController = 
    val keyboardController = LocalSoftwareKeyboardController.present
    
}

After that, I obtained this warning.

Warning:(21, 6) This class can solely be used with the compiler argument ‘-opt-in=kotlin.RequiresOptIn’

To repair this warning, I added this compiler argument within the construct.gradle (module degree) primarily based on the official documentation right here.

android {

    

    duties.withType(org.jetbrains.kotlin.gradle.duties.KotlinCompile).configureEach {
        kotlinOptions {
            freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
        }
    }
}

[Updated: July 01, 2022] – It appears to be like like this -Xopt-in=kotlin.RequiresOptIn compiler argument shouldn’t be wanted anymore in Kotlin 1.7.0. See right here.

Whereas creating identical Easy REST API app, I additionally encountered the identical difficulty as a result of the APIs from com.jakewharton.retrofit2.converter.kotlinx.serialization packages are marked with @ExperimentalSerializationApi annotation, which requires me to specify the@OptIn() annotation.

@RequiresOptIn(degree = RequiresOptIn.Degree.WARNING)
public annotation class ExperimentalSerializationApi

So I did the identical as above to eliminate the warnings.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments