All projects
Open Source2025Author

Rust Multiplatform Template

A UniFFI-based template for embedding a shared Rust core into iOS, Android, and JVM apps.

1ShellUpdated November 13, 2025
Rust Multiplatform Template

About

A starter template that wraps a Rust core library with UniFFI bindings so the same logic can be shared across Android, iOS, and JVM applications from a single source of truth.

Rust Multiplatform Template Library

A template project for creating Rust libraries that can be embedded in multiple platforms using UniFFI for automatic binding generation.

๐Ÿ“ Using this as a template? Run ./scripts/run_me_first.sh to customize all project names!

Table of Contents

What is This?

This template demonstrates how to write Rust code once and use it across:

  • iOS (arm64 devices)
  • iOS Simulator (arm64 M1+ and x86_64 Intel)
  • macOS (arm64 Apple Silicon)
  • Android (arm64-v8a, armeabi-v7a, x86, x86_64)
  • JVM (Desktop applications in Java/Kotlin)

The template uses UniFFI to automatically generate Swift and Kotlin bindings from your Rust code.

Template Functions

This template includes two async example functions:

  • echo(input, token) โ†’ Returns an EchoResult with text, length, and timestamp, or null/nil if empty (demonstrates optional returns, structured data, and cancellation)
  • random() โ†’ Returns a random number between 0.0 and 1.0 (demonstrates async functions and working with dependencies)

Quick Start

For Template Users (First Time)

If you're using this repository as a GitHub template, start here:

# 1. Clone the template
git clone https://github.com/yourusername/rust-multiplatform-template-lib.git my-project
cd my-project

# 2. Run the initialization script (interactive)
./scripts/run_me_first.sh

# This will guide you through:
# - Setting your Rust crate name
# - Setting your Swift module name
# - Setting your Java/Kotlin package name
# - Updating all ~50+ files in the project
# - Updating demo applications
# - Cleaning build artifacts

That's it! Your project is now customized with your names.

Prerequisites

  • Rust - Install from rustup.rs
  • Xcode (for iOS/macOS) - Required for Apple platforms
  • Android SDK & NDK (for Android/JVM) - Required for Kotlin multiplatform builds
    • Set ANDROID_HOME environment variable, or
    • Create platforms/kotlin/local.properties with sdk.dir path
    • See platforms/kotlin/README.md for setup details

Setup (After Initialization)

# Install required Rust targets
./scripts/setup.sh

# Build for all platforms
./scripts/build-all.sh

That's it! The library is now built for iOS, macOS, Android, and JVM.

Demo Applications

This template includes three complete demo applications that show how to use the Rust library:

iOS/macOS App

A SwiftUI app for iOS 14+ and macOS 11+ with interactive UI.

Location: apps/apple/

Run:

# 1. Build the library
./scripts/build-apple.sh

# 2. Open in Xcode
cd apps/apple
open DemoApp.xcodeproj

# 3. Select iPhone simulator or My Mac, then press Cmd+R

Features:

  • Color-coded sections for each function
  • Text input field for echo testing
  • Real-time result display with EchoResult metadata
  • Error handling with alerts
  • Async/await with Swift concurrency

Android App

A Jetpack Compose app for Android 5.0+ (API 21+).

Location: apps/android/

Run:

# 1. Build the Kotlin library
./scripts/build-kotlin.sh

# 2. Build the AAR (Android Archive)
cd platforms/kotlin
./gradlew assembleRelease
cd ../..

# 3. Open in Android Studio
# File -> Open -> Select apps/android/

# 4. Run on emulator or device

Features:

  • Material Design 3 with Jetpack Compose
  • Color-coded cards for each function
  • Interactive text input
  • Modern Android UI patterns
  • Coroutines for async operations

Desktop CLI App

A command-line JVM application that runs on macOS, Linux, and Windows.

Location: apps/desktop/

Run:

# 1. Build the Kotlin library and publish to Maven Local
cd platforms/kotlin
./gradlew publishToMavenLocal
cd ../..

# 2. Run the desktop app
cd apps/desktop
./gradlew run

# Or build a standalone JAR
./gradlew jar
java -jar build/libs/template-demo-desktop-1.0.0.jar

Features:

  • Automated testing of both functions
  • Detailed output with statistics
  • Portable JAR file
  • No GUI dependencies
  • Async support with Kotlin coroutines

Building

Build All Platforms

./scripts/build-all.sh

This will build for all supported platforms (Apple, Android, JVM).

Build Specific Platforms

Apple platforms (iOS + macOS):

./scripts/build-apple.sh

Android + JVM:

./scripts/build-kotlin.sh

Using the Generated Libraries

iOS/macOS (Swift)

  1. In Xcode: File โ†’ Add Package Dependencies
  2. Click Add Local and select the platforms/apple directory
  3. Add the Template package to your target
  4. Import and use in Swift:
import Template

// Echo function (async)
Task {
    do {
        let result = try await echo(input: "Hello!", token: nil)
        if let echoResult = result {
            print("Text: \(echoResult.text)")
            print("Length: \(echoResult.length)")
        }
    } catch {
        print("Error: \(error)")
    }
}

// Random function (async)
Task {
    let number = await random()
    print("Random: \(number)")
}

Android (Kotlin/Java)

  1. Build and publish to Maven Local:

    cd platforms/kotlin
    ./gradlew publishToMavenLocal
  2. Add to your app's build.gradle.kts:

    dependencies {
        implementation("com.template:template:0.1.0")
    }
  3. Use in Kotlin:

    import kotlinx.coroutines.launch
    import uniffi.rust_multiplatform_template_lib.*
    
    // Echo function (async)
    scope.launch {
        val result = templateEcho("Hello!", null)
        if (result != null) {
            println("Text: ${result.text}")
            println("Length: ${result.length}")
        }
    }
    
    // Random function (async)
    scope.launch {
        val number = templateRandom()
        println("Random: $number")
    }

JVM Desktop (Kotlin/Java)

Same as Android - the Kotlin Multiplatform setup supports both Android and JVM targets.

Project Structure

rust-multiplatform-template-lib/
โ”œโ”€โ”€ src/                          # Rust source code
โ”‚   โ”œโ”€โ”€ lib.rs                    # Library entry point
โ”‚   โ”œโ”€โ”€ template.rs               # Core Rust functions
โ”‚   โ””โ”€โ”€ template.udl              # UniFFI interface definition
โ”œโ”€โ”€ tests/                        # Rust integration tests
โ”‚   โ””โ”€โ”€ template_tests.rs         # Test suite
โ”œโ”€โ”€ platforms/                    # Platform-specific bindings
โ”‚   โ”œโ”€โ”€ apple/                    # iOS/macOS Swift package
โ”‚   โ”‚   โ”œโ”€โ”€ Package.swift         # Swift Package Manager manifest
โ”‚   โ”‚   โ”œโ”€โ”€ Sources/Template/     # Generated Swift bindings
โ”‚   โ”‚   โ”œโ”€โ”€ Tests/                # Swift tests
โ”‚   โ”‚   โ””โ”€โ”€ xcframework/          # Built XCFramework
โ”‚   โ””โ”€โ”€ kotlin/                   # Android/JVM Kotlin package
โ”‚       โ”œโ”€โ”€ build.gradle.kts      # Gradle build configuration
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”‚   โ”œโ”€โ”€ commonMain/kotlin/    # Generated Kotlin bindings
โ”‚       โ”‚   โ”œโ”€โ”€ commonTest/kotlin/    # Kotlin tests
โ”‚       โ”‚   โ”œโ”€โ”€ jniLibs/              # Android native libraries
โ”‚       โ”‚   โ””โ”€โ”€ jvmMain/kotlin/       # JVM native libraries
โ”‚       โ””โ”€โ”€ settings.gradle.kts
โ”œโ”€โ”€ apps/                         # Demo applications
โ”‚   โ”œโ”€โ”€ apple/                    # iOS/macOS SwiftUI app
โ”‚   โ”‚   โ”œโ”€โ”€ DemoApp.xcodeproj/        # Xcode project
โ”‚   โ”‚   โ”œโ”€โ”€ TemplateDemo/             # Source files
โ”‚   โ”‚   โ””โ”€โ”€ README.md
โ”‚   โ”œโ”€โ”€ android/                  # Android Compose app
โ”‚   โ”‚   โ”œโ”€โ”€ app/                      # App module
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ src/main/             # Source files
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ build.gradle.kts
โ”‚   โ”‚   โ”œโ”€โ”€ build.gradle.kts
โ”‚   โ”‚   โ””โ”€โ”€ settings.gradle.kts
โ”‚   โ””โ”€โ”€ desktop/                  # Desktop CLI JVM app
โ”‚       โ”œโ”€โ”€ src/main/kotlin/          # Kotlin source
โ”‚       โ”œโ”€โ”€ build.gradle.kts
โ”‚       โ””โ”€โ”€ settings.gradle.kts
โ”œโ”€โ”€ scripts/                      # Build, test, and documentation scripts
โ”‚   โ”œโ”€โ”€ build-all.sh              # Build all platforms
โ”‚   โ”œโ”€โ”€ build-apple.sh            # Build script for iOS/macOS
โ”‚   โ”œโ”€โ”€ build-kotlin.sh           # Build script for Android/JVM
โ”‚   โ”œโ”€โ”€ test-all.sh               # Test all platforms
โ”‚   โ”œโ”€โ”€ test-apple.sh             # Test script for iOS/macOS
โ”‚   โ”œโ”€โ”€ test-kotlin.sh            # Test script for Android/JVM
โ”‚   โ”œโ”€โ”€ doc-all.sh                # Generate documentation
โ”‚   โ””โ”€โ”€ setup.sh                  # Install Rust targets
โ”œโ”€โ”€ docs_src/                     # Documentation source (markdown)
โ”‚   โ”œโ”€โ”€ UDL_GUIDE.md              # UniFFI interface guide
โ”‚   โ”œโ”€โ”€ SWIFT_EXAMPLES.md         # Swift usage examples
โ”‚   โ”œโ”€โ”€ KOTLIN_EXAMPLES.md        # Kotlin usage examples
โ”‚   โ””โ”€โ”€ TESTING.md                # Testing guide
โ”œโ”€โ”€ docs/                         # Generated documentation (HTML)
โ”‚   โ”œโ”€โ”€ *.html                    # HTML from markdown (auto-generated)
โ”‚   โ””โ”€โ”€ lib/                      # Rust API docs (from cargo doc)
โ”œโ”€โ”€ Cargo.toml                    # Rust package manifest
โ”œโ”€โ”€ DEVELOPMENT.md                # Development guide
โ”œโ”€โ”€ CONTRIBUTING.md               # Contributing guidelines
โ””โ”€โ”€ README.md                     # This file

Testing

Test All Platforms

./scripts/test-all.sh

This will run tests for:

  • Rust core library (unit, integration, and doc tests)
  • Apple platforms (iOS + macOS Swift tests)
  • Kotlin platforms (Android + JVM tests)

Test Specific Platforms

Test Rust code:

cargo test

Test Apple platforms (iOS + macOS):

./scripts/test-apple.sh

Test Kotlin platforms (Android + JVM):

./scripts/test-kotlin.sh

Manual testing (if needed):

For Swift:

cd platforms/apple
swift test

For Kotlin:

cd platforms/kotlin
./gradlew test

Documentation

The project documentation is organized into two directories:

  • docs_src/ - Source markdown files (edit these)
  • docs/ - Generated HTML documentation (auto-generated, published to GitHub Pages)

Generate Documentation for All Platforms

./scripts/doc-all.sh

This will generate API documentation for:

  • Rust โ†’ docs/lib/index.html

View Documentation Locally

After running ./scripts/doc-all.sh, open the generated HTML files in your browser:

open docs/lib/index.html

Or start a local web server:

cd docs && python3 -m http.server 8000
# Then open: http://localhost:8000/

Adding Documentation

  1. Add markdown files to docs_src/ for platform-specific guides
  2. Root-level documentation (README.md, DEVELOPMENT.md, CONTRIBUTING.md) stays in project root
  3. CI/CD automatically converts markdown to HTML and publishes to GitHub Pages

Publishing Documentation

Documentation is automatically published to GitHub Pages when you push to main:

git add docs/ docs_src/
git commit -m "Update documentation"
git push

The GitHub Actions workflow handles:

  • Converting markdown โ†’ HTML
  • Generating Rust API docs
  • Deploying to gh-pages branch

Development

For contributors and maintainers, see DEVELOPMENT.md for detailed instructions on:

  • Setting up your development environment
  • Building and testing
  • Adding new features
  • Customizing the template
  • Troubleshooting common issues
  • Contributing guidelines

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines on:

  • Reporting bugs
  • Suggesting enhancements
  • Submitting pull requests
  • Development workflow

Contact

Ezequiel (Kimi) Aceto

For questions, issues, or discussions about this project:

License

MIT License - see LICENSE for details.

Copyright (c) 2025 Ezequiel (Kimi) Aceto

Resources