Generate UUID on kotlin-multiplatform?

Are there any kotlin-multiplatform common functions to get a UUID/GUID?

 // ideally something like this
 val newUUID = UUID() // "1598044e-5259-11e9-8647-d663bd873d93"
 println("newUUID = $newUUID")

I'd prefer not to make separate Android and iOS versions using expect-actual.


  • As per the Kotlin multiplatform documentation, you can make an expect/actual function to use the Android (java) and iOS (NSUUID) specific implementations:

    // Common
    expect fun randomUUID(): String
    // Android
    import java.util.UUID
    actual fun randomUUID() = UUID.randomUUID().toString()
    // iOS
    import platform.Foundation.NSUUID
    actual fun randomUUID(): String = NSUUID().UUIDString()