Kotlin Multiplatform (KMP) enables sharing business logic across JVM, Android, iOS, macOS, and JavaScript targets from a single codebase. UUID generation in KMP requires a library that works across all targets — the system clock and CSPRNG access differ per platform. This page covers UUID v7 in KMP projects using the benasher44/uuid library, Ktor server integration, SQLDelight storage, and the expect/actual pattern for platform-specific UUID v7 generation.
Adding UUID support to a KMP project
// build.gradle.kts (shared module)
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.benasher44:uuid:0.8.4") // UUID v4, all targets
}
}
val jvmMain by getting {
dependencies {
implementation("com.github.f4b6a3:uuid-creator:6.0.0") // UUID v7, JVM only
}
}
}
}
The benasher44/uuid library provides uuid4() across all targets. UUID v7 requires platform-specific handling — on JVM use uuid-creator from Maven Central; on iOS use an expect/actual bridge.
expect/actual pattern for UUID v7
// commonMain/kotlin/ids.kt
expect fun generateId(): String
// jvmMain/kotlin/ids.kt
import com.github.f4b6a3.uuid.UuidCreator
actual fun generateId(): String = UuidCreator.getTimeOrderedEpoch().toString()
// iosMain/kotlin/ids.kt
import platform.Foundation.NSUUID
actual fun generateId(): String {
// iOS: UUID v4 from Foundation — upgrade to UUID v7 via Swift package if needed
return NSUUID().UUIDString().lowercase()
}
This pattern lets shared commonMain business logic call generateId() without knowing the platform. The Kotlin Multiplatform documentation covers the expect/actual mechanism in detail.
Ktor server integration
In a Ktor backend (JVM), use UUID v7 for all resource IDs:
import io.ktor.server.application.*
import io.ktor.server.routing.*
import io.ktor.server.response.*
import com.github.f4b6a3.uuid.UuidCreator
fun Application.configureRouting() {
routing {
post("/orders") {
val order = Order(
id = UuidCreator.getTimeOrderedEpoch().toString(), // UUID v7
customerId = call.receive<CreateOrderRequest>().customerId,
createdAt = System.currentTimeMillis()
)
// persist order...
call.respond(HttpStatusCode.Created, order)
}
}
}
Ktor’s REST API documentation covers routing, content negotiation, and serialisation with kotlinx.serialization.
SQLDelight storage schema
-- Order.sq
CREATE TABLE Order (
id TEXT NOT NULL PRIMARY KEY, -- UUID v7 string
customerId TEXT NOT NULL,
total REAL NOT NULL,
createdAt INTEGER NOT NULL
);
selectAll:
SELECT * FROM Order ORDER BY id ASC; -- Lexicographic = chronological for UUID v7
selectByCustomer:
SELECT * FROM Order WHERE customerId = ? ORDER BY id DESC LIMIT :limit;
// In repository
val orders = database.orderQueries.selectAll().executeAsList()
// Results are in creation order — UUID v7 lexicographic sort = time sort
UUID v7 string sort matches creation order as defined in RFC 9562 §5.7, so ORDER BY id ASC in SQLDelight returns records in the order they were inserted.
Shared domain model
// commonMain — shared across all platforms
@Serializable
data class Order(
val id: String, // UUID v7 string
val customerId: String, // UUID v4 (no timestamp exposure)
val total: Double,
val createdAt: Long // Unix ms (can also extract from id for v7)
)
// Timestamp extraction utility (commonMain)
fun extractTimestampMs(uuidV7: String): Long {
val hex = uuidV7.replace("-", "").substring(0, 12)
return hex.toLong(16)
}
Android integration with Room
On Android-only projects or KMP with an Android-specific database module:
// Android: Room with UUID v7
@Entity(tableName = "orders")
data class OrderEntity(
@PrimaryKey
val id: String = UuidCreator.getTimeOrderedEpoch().toString(),
val customerId: String,
val total: Double
)
@Dao
interface OrderDao {
@Query("SELECT * FROM orders ORDER BY id ASC")
fun getAllOrders(): Flow<List<OrderEntity>>
}
The Android Room documentation covers entity annotations, DAOs, and Flow-based observation.
Further reading
- UUID in Java — JVM UUID generation patterns
- UUID in Mobile — iOS and Android UUID patterns
- UUID in Databases — storage and index performance
External references
- benasher44/uuid on GitHub
- uuid-creator on Maven Central
- SQLDelight documentation
- RFC 9562 — UUID standard
- Kotlin Multiplatform documentation
Frequently asked questions
Which library provides UUID v7 in Kotlin Multiplatform?
The benasher44/uuid library is the most widely used UUID library for Kotlin Multiplatform — it supports all KMP targets (JVM, Android, iOS, macOS, Linux, JS) and implements UUID v4. For UUID v7, use the uuid-creator library on Maven Central on JVM/Android targets, and implement a custom expect/actual declaration for iOS that calls Swift's UUID v7 generation. RFC 9562 defines the UUID v7 bit layout used by both libraries.
How do I store UUID v7 in SQLDelight for Kotlin Multiplatform?
Declare the column as TEXT NOT NULL in your .sq schema file and add a custom column adapter mapping String to your UUID type. SQLDelight generates type-safe queries — the UUID v7 string sorts lexicographically, giving creation-order results from ORDER BY id without a separate created_at column. The SQLDelight documentation covers column adapters and custom types.
Does UUID v7 work on iOS targets in Kotlin Multiplatform?
KMP iOS targets run on the Kotlin/Native runtime. UUID generation requires platform-specific entropy — use an expect/actual function where the iOS actual calls NSUUID().uuidString (UUID v4) or a Swift UUID v7 package via Objective-C interop. For purely JVM/Android KMP projects, uuid-creator works directly. The Kotlin Multiplatform documentation covers expect/actual declarations for platform-specific implementations.