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.shto customize all project names!
- What is This?
- Template Functions
- Quick Start
- Demo Applications
- Building
- Using the Generated Libraries
- Project Structure
- Testing
- Documentation
- Development
- Contributing
- Contact
- License
- Resources
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.
This template includes two async example functions:
echo(input, token)โ Returns an EchoResult with text, length, and timestamp, ornull/nilif 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)
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 artifactsThat's it! Your project is now customized with your names.
- 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_HOMEenvironment variable, or - Create
platforms/kotlin/local.propertieswithsdk.dirpath - See platforms/kotlin/README.md for setup details
- Set
# Install required Rust targets
./scripts/setup.sh
# Build for all platforms
./scripts/build-all.shThat's it! The library is now built for iOS, macOS, Android, and JVM.
This template includes three complete demo applications that show how to use the Rust library:
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+RFeatures:
- 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
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 deviceFeatures:
- Material Design 3 with Jetpack Compose
- Color-coded cards for each function
- Interactive text input
- Modern Android UI patterns
- Coroutines for async operations
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.jarFeatures:
- Automated testing of both functions
- Detailed output with statistics
- Portable JAR file
- No GUI dependencies
- Async support with Kotlin coroutines
./scripts/build-all.shThis will build for all supported platforms (Apple, Android, JVM).
Apple platforms (iOS + macOS):
./scripts/build-apple.shAndroid + JVM:
./scripts/build-kotlin.sh- In Xcode: File โ Add Package Dependencies
- Click Add Local and select the
platforms/appledirectory - Add the
Templatepackage to your target - 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)")
}-
Build and publish to Maven Local:
cd platforms/kotlin ./gradlew publishToMavenLocal -
Add to your app's
build.gradle.kts:dependencies { implementation("com.template:template:0.1.0") } -
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") }
Same as Android - the Kotlin Multiplatform setup supports both Android and JVM targets.
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
./scripts/test-all.shThis will run tests for:
- Rust core library (unit, integration, and doc tests)
- Apple platforms (iOS + macOS Swift tests)
- Kotlin platforms (Android + JVM tests)
Test Rust code:
cargo testTest Apple platforms (iOS + macOS):
./scripts/test-apple.shTest Kotlin platforms (Android + JVM):
./scripts/test-kotlin.shManual testing (if needed):
For Swift:
cd platforms/apple
swift testFor Kotlin:
cd platforms/kotlin
./gradlew testThe project documentation is organized into two directories:
docs_src/- Source markdown files (edit these)docs/- Generated HTML documentation (auto-generated, published to GitHub Pages)
./scripts/doc-all.shThis will generate API documentation for:
- Rust โ
docs/lib/index.html
After running ./scripts/doc-all.sh, open the generated HTML files in your browser:
open docs/lib/index.htmlOr start a local web server:
cd docs && python3 -m http.server 8000
# Then open: http://localhost:8000/- Add markdown files to
docs_src/for platform-specific guides - Root-level documentation (
README.md,DEVELOPMENT.md,CONTRIBUTING.md) stays in project root - CI/CD automatically converts markdown to HTML and publishes to GitHub Pages
Documentation is automatically published to GitHub Pages when you push to main:
git add docs/ docs_src/
git commit -m "Update documentation"
git pushThe GitHub Actions workflow handles:
- Converting markdown โ HTML
- Generating Rust API docs
- Deploying to
gh-pagesbranch
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
We welcome contributions! Please see CONTRIBUTING.md for guidelines on:
- Reporting bugs
- Suggesting enhancements
- Submitting pull requests
- Development workflow
Ezequiel (Kimi) Aceto
- ๐ง Email: eaceto@pm.me
- ๐ Website: eaceto.dev
- ๐ผ LinkedIn: linkedin.com/in/ezequielaceto
- ๐ GitHub: @eaceto
For questions, issues, or discussions about this project:
- ๐ Report issues
- ๐ฌ Start a discussion
MIT License - see LICENSE for details.
Copyright (c) 2025 Ezequiel (Kimi) Aceto
