7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | # Uniffi based bindings for the Rust SDK crypto crate |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | This crate contains Uniffi based bindings for the `matrix-sdk-crypto` crate. The |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | README mainly describes how to build and integrate the bindings into a Kotlin |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | based Android project, but the Android specific bits can be skipped if you are |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | targeting an x86 Linux project. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | To build and distribute bindings for iOS projects, see a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | [dedicated page](../apple/README.md) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | ## Prerequisites |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | ### Rust |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | To build the bindings [Rust] will be needed it can be either installed using an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | OS specific package manager or directly with the provided |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | [installer](https://rustup.rs/). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | ### Android NDK |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | The Android NDK will be required as well, it can be installed either through |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | Android Studio or directly using an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | [installer](https://developer.android.com/ndk/downloads). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | ### Configuring Rust for cross compilation |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | First we'll need to install the Rust target for our desired Android |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | architecture, for example: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | ```text |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | # rustup target add aarch64-linux-android |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | This will add support to cross-compile for the aarch64-linux-android target, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | Rust supports many different [targets], you'll have to make sure to pick the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | right one for your device or emulator. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | After this is done, we'll have to configure [Cargo] to use the correct linker |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | for our target, by providing the Cargo setting of |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | [target.<triple>.linker](https://doc.rust-lang.org/cargo/reference/config.html#targettriplelinker) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | with a value of the path to an appropriate linker in your NDK installation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | This may be set through an environment variable: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | ```text |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | $ export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="<path-to-ndk-installation>/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang" |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | Alternatively, it may be set in the `.cargo/config.toml` file in the current |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | directory, any parent directory, or your home directory: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | ```text |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | [target.aarch64-linux-android] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | linker = "<path-to-ndk-installation>/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang" |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | ## Building |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | To enable cross compilation for `olm-sys` which builds our `libolm` C library |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | we'll need to set the `ANDROID_NDK` environment variable to the location of our |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | Android NDK installation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | ```text |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | $ export ANDROID_NDK=$HOME/Android/Sdk/ndk/<some-installed-version> |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | Also, include the NDK tools directory in your `PATH`: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | ```text |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | $ export PATH="$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH" |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | ### Building for a target |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | The bindings can built for the `aarch64` target with: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | ```text |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | $ cargo build --target aarch64-linux-android |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | After that, a dynamic library can be found in the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | `target/aarch64-linux-android/debug` directory, under the repository root |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | directory. The library will be called `libmatrix_sdk_crypto_ffi.so` and needs to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | be renamed and copied into the `jniLibs` directory of your Android project, for |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | Element Android: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | ```text |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | $ cp ../../target/aarch64-linux-android/debug/libmatrix_sdk_crypto_ffi.so \ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | /home/example/matrix-sdk-android/src/main/jniLibs/aarch64/libuniffi_olm.so |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | ## License |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | [Apache-2.0](https://www.apache.org/licenses/LICENSE-2.0) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | [Rust]: https://www.rust-lang.org/ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | [installer]: https://rustup.rs/ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | [targets]: https://doc.rust-lang.org/nightly/rustc/platform-support.html |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | [Cargo]: https://doc.rust-lang.org/cargo/ |