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 yourbuild.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:
.sofiles 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:xmtpv3.kt- Generated UniFFI bindings
Object Lifetimes
UniFFI manages object lifetimes usingArc<> 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
AutoCloseablefor explicit cleanup
Async and Concurrency
The bindings use Tokio’s multi-threaded runtime:- Kotlin
suspendfunctions map to Rust async functions - Rust operations may resume on different threads after
await - All exposed objects are
Send + Syncfor 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
Build Commands
Linting and Formatting
- Spotless is configured in
build.gradle.kts - Follows Kotlin official style guide
Testing
Running Tests
The Android bindings have two types of tests:- Unit Tests
- Instrumented Tests
library/src/test/Test Structure
Instrumented tests inlibrary/src/androidTest/:
ClientTest.kt- Client creation and managementConversationsTest.kt- Conversation operationsGroupTest.kt- Group messagingDmTest.kt- Direct messagesCodecTest.kt- Content type codecs
Example Test
Key Dependencies
- Runtime
- Development
uniffi-xmtpv3- Generated FFI bindings- Native
.solibraries - Rust code compiled for Android kotlinx-coroutines- Async/await supportandroidx.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
AutoCloseablefor 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 inbuild.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
