Skip to content

Generating a Unique UUID in Xojo for Android

In the world of mobile app development, it’s often necessary to generate a universally unique identifier (UUID) to identify data or devices uniquely. If you’re a Xojo developer building apps for Android, you can easily generate a UUID using the function below. This function leverages the built-in UUID generation from Android’s Java class library.

What is a UUID?

A UUID (Universally Unique Identifier) is a 128-bit value that is unique worldwide. UUIDs are commonly used in software development to identify objects and have the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, where each “x” is a hexadecimal digit.

The Code: GenerateUUID

The following code shows a simple Xojo function that uses the built-in Java method on Android to generate a UUID. The function allows you to create the UUID either with or without hyphens, depending on your project requirements.

The Code:

Function GenerateUUID(useHyphens As Boolean = True) As String
' https://developer.android.com/reference/java/util/UUID
  Declare Function generateUUID Lib "java.util.UUID" Alias "randomUUID().toString" As CString

  Var uuid As String = generateUUID

  If useHyphens Then
    Return uuid
  Else
    Return uuid.ReplaceAll("-", "")
  End If
End Function

Code Explanation:

Declare Function generateUUID: This Declare statement gives us access to Android’s native Java library. We use the shared randomUUID method from the java.util.UUID class to generate a random UUID. This method returns the UUID as a string.

Var uuid As String = generateUUID: The generated UUID is stored as a string.

●  useHyphens: This optional parameter controls whether the UUID is returned with or without hyphens. The default value is True, meaning the UUID will be returned in its usual format with hyphens.

●  If useHyphens Then … Else: Depending on the value of useHyphens, either the standard UUID is returned or a hyphen-free version, where the ReplaceAll method is used to replace all hyphens with an empty string.

Use in Your Android Project:

1. UUID with Hyphens: If you want to get the UUID in its standard format with hyphens, you can call the function without any arguments:

Var myUUID As String = GenerateUUID 
' Example result: 123e4567-e89b-12d3-a456-426614174000 

2. UUID without Hyphens: If you prefer a UUID without hyphens, you can pass the argument False to the function:

Var myUUID As String = GenerateUUID(False) 
' Example result: 123e4567e89b12d3a456426614174000 

Why Use UUIDs?

UUIDs are ideal for cases where you need to ensure that every generated identifier is unique, even if they are generated by different devices or systems. Some common use cases in Android development with Xojo include:

●  Uniquely identifying users or devices

●  Creating keys for database entries

●  Tagging objects in a distributed application where no identifier conflicts should occur

With the GenerateUUID function, you can easily generate a unique UUID on Android devices using Xojo, without worrying about the complexities of native Android APIs. This method integrates seamlessly into your Android application and offers the flexibility to generate UUIDs with or without hyphens. If you’re working on a project where uniqueness is crucial, using UUIDs should be at the top of your list.

Good luck with your Android project in Xojo!

Martin T. is a Xojo MVP and has been very involved in testing Android support.