Problem with Kotlin Duration in Android Studio

Issue

I’m new with Android Studio and Kotlin and I have some trouble with Kotlin Duration. When I try to do following in my code:

val test: Duration = Duration.parse(value = timeClose)

I get this error message:

This declaration needs opt-in. Its usage must be marked with ‘@kotlin.time.ExperimentalTime’ or ‘@OptIn(kotlin.time.ExperimentalTime::class)’

I use Android Studio Chipmunk 2021.2.1 Patch 1 with Kotlin 212-1.7.10

How can I solve my problem?

Solution

Do what the error is telling you to do: mark it with one of the suggestions.

@kotlin.time.ExperimentalTime
val test: Duration = Duration.parse(value = timeClose)

or

@OptIn(kotlin.time.ExperimentalTime::class)
val test: Duration = Duration.parse(value = timeClose)

The reason you have to do this is that the Duration class is in the ExperimentalTime package, which means that this is an experimental preview, as the documentation states:

Note that this API is in a preview state and has a very high chance of being changed in the future. Do not use it if you develop a library since your library will become binary incompatible with the future versions of the standard library.

Answered By – Stephan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published