Android Jetpack compose material 3 why Card need this annotation @OptIn(ExperimentalMaterial3Api::class)

2.8k Views Asked by At

I am new in Jetpack Compose(JC). I started JC and used card but it shows me an error and recommend me to add this annotation @OptIn(ExperimentalMaterial3Api::class).

Also showing this message This material API is experimental and is likely to change or to be removed in the future. My Question that if this is experimental and might have changes in future what is the normal and stable way to use card in jetpack compose Material3?

What does this annotation actually means and what if I don't want to use experimental or thing which is not stable.

As I also find that Material3 is stable.

https://material.io/blog/material-3-compose-stable

Clarify me if you think that I don't understand this correctly or provide the appropriate fix.

Thx!

3

There are 3 best solutions below

3
lijiawangjun 王 On
buildFeatures {
    compose true
    kotlinOptions {
        freeCompilerArgs += [
                "-opt-in=kotlin.ExperimentalUnsignedTypes",
                "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
                "-opt-in=kotlinx.coroutines.InternalCoroutinesApi",
                "-opt-in=androidx.compose.animation.ExperimentalAnimationApi",
                "-opt-in=androidx.compose.ExperimentalComposeApi",
                "-opt-in=androidx.compose.material.ExperimentalMaterialApi",
                "-opt-in=androidx.compose.runtime.ExperimentalComposeApi",
                "-opt-in=androidx.compose.ui.ExperimentalComposeUiApi",
                "-opt-in=androidx.compose.material3.ExperimentalMaterial3Api",
                "-opt-in=coil.annotation.ExperimentalCoilApi",
                "-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
                "-opt-in=com.google.accompanist.pager.ExperimentalPagerApi",
                "-opt-in=com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi"
        ]
    }
}
1
Ri Elopre On

Same here! i didn't understand it too so what i do is i check if my android studio is updated and it seems like it is not, so i update it and then create a new project. it works in me

1
Javad Vatan On

The OptIn annotation in Jetpack Compose indicates that a class or function is experimental and should only be used with caution. It allows developers to use features still in development that may change or be removed in future releases.

When using an OptIn-annotated class or function, you must explicitly opt into using it by adding the @OptIn annotation to the class or function that calls it. This helps to ensure that you are aware of the potential risks of using experimental features and taking appropriate precautions.