Since Kotlin is a JVM-based language, we can use the same libraries and methods we would have used in Java to generate UUIDs, as long as we do so in Kotlin syntax. The following code snipped produces a valid version 4 UUID:
import java.util.UUID
println(UUID/randomUUID())
If you need to generate a specific UUID version, you need to use a third-party library such as uuid-creator which offers more flexible options:
import com.github.f4b6a3.uuid.UuidCreator
//generate UUID version 1
val uuid = UuidCreator.getTimeBased()
//generate UUID version 2
val uuid = UuidCreator.getDceSecurity(UuidLocalDomain.LOCAL_DOMAIN_PERSON, 1234)
//generate UUID version 3
val uuid = UuidCreator.getNameBasedMd5(UuidNamespace.NAMESPACE_URL, "https://github.com/")
//generate UUID version 4
val uuid = UuidCreator.getRandomBased()
//generate UUID version 5
val uuid = UuidCreator.getNameBasedSha1(UuidNamespace.NAMESPACE_URL, "https://github.com/")
//generate UUID version 6
val uuid = UuidCreator.getTimeOrdered()
//generate UUID version 7
val uuid = UuidCreator.getTimeOrderedEpoch()
Find out how to generate UUIDs in your favorite language here: Bash, Groovy, Python, Scala, Clojure, ClojureScript, Java, JavaScript, or return to Online UUID Generator