Skip to main content
The Kotlin bindings provide native XMTP functionality for Android applications using Mozilla’s UniFFI. These bindings expose the core LibXMTP Rust library to Kotlin through automatically generated FFI code and native libraries.
These bindings are low-level FFI interfaces. For most Android development, use the XMTP Android SDK instead, which provides a Kotlin-native API built on top of these bindings.

Installation

The bindings are distributed as part of the XMTP Android SDK. If you need to use the bindings directly:

Gradle

Add to your build.gradle.kts:

Requirements

  • Android SDK 23+ (Android 6.0 Marshmallow)
  • Kotlin 1.9+
  • Java 17
  • Gradle 8.0+

Architecture

The Kotlin bindings use UniFFI to generate Kotlin code from Rust, with native .so libraries for all Android ABIs.

Key Technologies

  • UniFFI: Mozilla’s tool for generating foreign-language bindings from Rust
  • JNI: Java Native Interface for calling native code
  • Native Libraries: .so files for arm64-v8a, armeabi-v7a, x86_64, x86
  • Tokio Integration: Rust async runtime with Kotlin coroutine bridging

Binary Artifacts

The bindings include native libraries for all Android ABIs:
Plus generated Kotlin bindings:
  • xmtpv3.kt - Generated UniFFI bindings

Object Lifetimes

UniFFI manages object lifetimes using Arc<> pointers:
  • Objects crossing the FFI boundary are wrapped in Arc<>
  • Kotlin’s garbage collector automatically releases Rust objects
  • No manual memory management required
  • Objects implement AutoCloseable for explicit cleanup

Async and Concurrency

The bindings use Tokio’s multi-threaded runtime:
  • Kotlin suspend functions map to Rust async functions
  • Rust operations may resume on different threads after await
  • All exposed objects are Send + Sync for thread safety
  • Supports concurrent operations across multiple threads

Basic Usage

Here’s a basic example of creating a client and sending messages:

Development

Prerequisites

For development, you need:
  • Android Studio
  • Android SDK and NDK
  • Rust toolchain with Android targets
  • Cross-compilation tools
The easiest way is using Nix:

Build Commands

Linting and Formatting

Configuration:
  • Spotless is configured in build.gradle.kts
  • Follows Kotlin official style guide

Testing

Running Tests

The Android bindings have two types of tests:
Located in library/src/test/

Test Structure

Instrumented tests in library/src/androidTest/:
  • ClientTest.kt - Client creation and management
  • ConversationsTest.kt - Conversation operations
  • GroupTest.kt - Group messaging
  • DmTest.kt - Direct messages
  • CodecTest.kt - Content type codecs

Example Test

Key Dependencies

  • uniffi-xmtpv3 - Generated FFI bindings
  • Native .so libraries - Rust code compiled for Android
  • kotlinx-coroutines - Async/await support
  • androidx.lifecycle - Android lifecycle integration

Advanced Patterns

Database Encryption

All local data is encrypted using a key you provide:

Environment Configuration

Inbox State Management

Streaming with Coroutines

Lifecycle Integration

Performance Considerations

Memory Management

  • UniFFI uses Arc for shared ownership between Kotlin and Rust
  • Kotlin GC automatically deallocates objects
  • Implement AutoCloseable for explicit cleanup

Threading

  • Tokio runtime uses multiple threads for Rust operations
  • Kotlin coroutines integrate seamlessly
  • All callbacks are thread-safe

Database Performance

  • SQLite with encryption (SQLCipher)
  • Write-ahead logging (WAL) enabled
  • Connection pooling for concurrent access

ABI Filtering

To reduce APK size, filter ABIs in build.gradle.kts:

Troubleshooting

Native Library Not Found

If you see “UnsatisfiedLinkError: dlopen failed”:

Database Errors

If you encounter database errors:

Coroutine Context Required

Many methods are suspend functions:

Debugging

Debugging FFI can be challenging:

Enable Logging

Examine Database

Resources

UniFFI Documentation

Learn about the UniFFI framework

Source Code

View the bindings source code

XMTP Android SDK

Use the high-level Kotlin SDK

Example Tests

See real usage examples