How to Generate a UUID in Kotlin
The most straightforward way to generate a UUID in Kotlin is UUID.randomUUID() from java.util.UUID — since Kotlin compiles to JVM bytecode, this API is available with zero extra setup and works identically on Android. The rust uuid reference shows how to use uuid::Uuid::new_v4() with zero-cost abstractions.
import java.util.UUID
fun main() {
val myUuid: UUID = UUID.randomUUID()
println(myUuid)
// Output: 550e8400-e29b-41d4-a716-446655440000
}
Explanation
import java.util.UUIDpulls in the JVM's built-in UUID class — no extra Gradle dependency needed, and it works identically on Android.UUID.randomUUID()generates a random UUID (version 4) using a cryptographically strong pseudo-random number generator under the hood.println(myUuid)callsUUID'stoString()implicitly, producing the standard 36-character hyphenated format.- The result is a
UUIDobject, not aString— call.toString()explicitly when you need a string for concatenation or JSON serialization.
Convert a String to a UUID in Kotlin
When a UUID arrives as a string — from a network response, a config file, or a database column — parse it back into a type-safe UUID with UUID.fromString(). The call throws IllegalArgumentException on malformed input, so wrap it safely using Kotlin's null-safety idioms. The bash uuid guide covers uuidgen, /proc/sys/kernel/random/uuid, and Python one-liners for shell scripting.
import java.util.UUID
fun String.toUuidOrNull(): UUID? =
runCatching { UUID.fromString(this) }.getOrNull()
fun main() {
val valid = "550e8400-e29b-41d4-a716-446655440000".toUuidOrNull()
val invalid = "not-a-uuid".toUuidOrNull()
println(valid) // 550e8400-e29b-41d4-a716-446655440000
println(invalid) // null
}
Explanation
UUID.fromString()parses any properly formatted 36-character UUID string, in either uppercase or lowercase, into aUUIDobject.- It throws
IllegalArgumentExceptionif the string isn't a valid UUID, so never call it directly on untrusted input. - The
toUuidOrNull()extension wraps the call inrunCatching { }.getOrNull(), an idiomatic Kotlin pattern that turns the exception into anullresult instead of crashing. - Two
UUIDobjects compare equal with==when they represent the same identifier, regardless of how each one was created.
Other Options for Generating UUIDs in Kotlin
UUID.randomUUID() covers most use cases, but Kotlin's standard library also has its own native multiplatform Uuid API, added in Kotlin 2.0.20 as an alternative to java.util.UUID for projects that don't target the JVM.
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
@OptIn(ExperimentalUuidApi::class)
fun main() {
// Works on JVM, Native, JS, and Wasm targets alike
val myUuid = Uuid.random()
println(myUuid)
}
Explanation
kotlin.uuid.Uuidis Kotlin's newer multiplatform Uuid API, useful when a Kotlin Multiplatform project needs UUIDs on targets wherejava.util.UUIDisn't available, such as iOS, JS, or Wasm.Uuid.random()is the multiplatform equivalent ofUUID.randomUUID(), returning aUuidinstance rather than a JVMUUID.- The
@OptIn(ExperimentalUuidApi::class)annotation is required because this is a newer API — check the current Kotlin release notes for its stability status before relying on it in production. - For JVM-only and Android projects,
java.util.UUIDremains the simpler, more broadly compatible choice.