Kotlin can use the JVM UUID class directly, and recent Kotlin releases add a multiplatform Uuid type in the standard library.
Key takeaways
- On the JVM, java.util.UUID.randomUUID() works directly.
- Kotlin 2.0.20+ adds a multiplatform kotlin.uuid.Uuid API.
- Both produce standard version 4 UUID strings.
JVM interop (works everywhere on the JVM)
The simplest option on the JVM is the familiar Java UUID class, called straight from Kotlin.
val id = java.util.UUID.randomUUID().toString() // version 4Multiplatform kotlin.uuid
Newer Kotlin versions include a standard-library Uuid that works across platforms, not just the JVM.
import kotlin.uuid.Uuid
val id = Uuid.random() // version 4
val parsed = Uuid.parse("018f2f7a-07b2-7d6e-9c37-43e1577b8b44")