UUID in Kotlin
Kotlin UUID Generator
On the JVM and Android, Kotlin gets UUIDs from java.util.UUID — the same class Java uses, with no wrapper needed.
JVM and Androidjava.util.UUIDMultiplatform wrapper
Generated locally with the Web Crypto API — nothing leaves your browser.
Generating UUIDs in Kotlin
UUID.randomUUID().toString()is the idiom, and it produces a lowercase v4 value. PrefertoString()at the boundary and keep theUUIDobject inside the model: it is comparable and hashable, so it works as a map key and in data classes without custom equality.- Kotlin/Native and Kotlin/JS have no
java.util, so a shared multiplatform module needs anexpect fun randomUuid(): Stringwith oneactualper target — the JVM one delegating toUUID.randomUUID(), the others drawing from the platform's random source. That indirection is the price of one identifier API across targets.
How to generate
- Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
- Copy one value with Copy, or set How many and use Copy all for a fixture list.
- Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.
Use Cases
- Android Room entities — a string primary key assigned before insert, so the row has its identity offline.
- Correlation IDs in structured logs across suspend functions.
- Test fixtures for Kotlin and Android instrumentation tests.
How Kotlin UUID Generator Compares
| UUID.randomUUID() | JVM and Android, v4, CSPRNG-backed |
| uuid-creator | Library; adds time-ordered v7 for JVM projects |
| expect/actual wrapper | One API across JVM, Native and JS targets |
Code Examples
Kotlin
import java.util.UUID
val id: String = UUID.randomUUID().toString() // v4
// "8f2c7b41-0d3e-4a55-b6c1-8e7d9f0a1b2c"
// Keep the UUID object in the model; render to string at the edges
data class Order(val id: UUID = UUID.randomUUID(), val total: Long)Multiplatform
// commonMain
expect fun randomUuid(): String
// androidMain / jvmMain
actual fun randomUuid(): String = java.util.UUID.randomUUID().toString()
// nativeMain (Kotlin/Native has no java.util)
actual fun randomUuid(): String = platform.Foundation.NSUUID().UUIDStringFrequently Asked Questions
Does Kotlin have its own UUID type?
No. On the JVM and Android you use
java.util.UUID; for shared multiplatform code you wrap it behind an expect/actual function.How do I generate a UUID v7 in Kotlin?
The JDK does not include it. Use the
uuid-creator library (UuidCreator.getTimeOrderedEpoch()), or generate the value in the database.Is UUID.randomUUID() safe on Android?
Yes — it is backed by the platform's secure random generator and is the standard choice for Room or SQLDelight primary keys.
Related tools
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterJavaScript UUID GeneratorTypeScript UUID GeneratorNode.js UUID GeneratorReact UUID GeneratorPython UUID GeneratorJava UUID GeneratorC# UUID GeneratorGo UUID GeneratorPHP UUID GeneratorRuby UUID GeneratorRust UUID GeneratorKotlin UUID GeneratorSwift UUID GeneratorSQL UUID Generator