Skip to content

Template project for integrating Rust via UniFFI into an Expo (React Native) module for iOS and Android.

Notifications You must be signed in to change notification settings

basedafdev/expo-rust-ffi-template

Repository files navigation

Expo Rust FFI Template

A template for building React Native applications with Expo that integrate native Rust code via FFI (Foreign Function Interface). This template demonstrates how to bridge Rust with Swift (iOS) and Kotlin (Android) in an Expo-managed workflow.

Features

  • 🦀 Rust integration with iOS and Android
  • 📱 Expo managed workflow
  • 🔄 Hot reloading support
  • 🛠 Pre-configured build scripts
  • 📚 Example arithmetic module included
  • 🎯 TypeScript support

Prerequisites

Before you begin, ensure you have installed:

Getting Started

  1. Create a new project using this template:

    npx create-expo-app my-app --template expo-rust-ffi-template
  2. Install dependencies:

    cd my-app
    npm install
  3. Build native modules:

    # For iOS
    npm run build:ios
    
    # For Android
    npm run build:android
  4. Start the development server:

    npm start

Project Structure

my-app/
├── modules/
│   └── rust_ffi/           # Shared Rust FFI implementation
│       ├── ios/            # iOS FFI bindings and libraries
│       │   └── arithmetic/
│       └── android/        # Android FFI bindings and libraries
│           └── arithmetic/
├── rust_core/              # Rust implementation
│   ├── modules/            # Individual Rust modules
│   │   └── arithmetic/     # Example arithmetic module
│   ├── uniffi-bindgen/     # UniFFI bindings generator
│   ├── generated/          # Generated bindings
│   └── Cargo.toml         # Workspace configuration
├── scripts/               # Build scripts
│   ├── build_ios.sh
│   └── build_android.sh
├── ios/                  # iOS project files
├── android/             # Android project files
└── App.tsx             # Main application entry point

Development Workflow

Creating a New Rust Module

  1. Create a new module in the Rust workspace:

    cd rust_core/modules
    mkdir my_module
    cd my_module
    cargo init --lib
  2. Add the module to rust_core/Cargo.toml:

    [workspace]
    members = [
      "modules/arithmetic",
      "modules/my_module",
      "uniffi-bindgen"
    ]
  3. Implement your Rust code:

    // rust_core/modules/my_module/src/lib.rs
    #[uniffi::export]
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
  4. Generate bindings:

    npm run build:ios    # For iOS
    npm run build:android # For Android

Using Native Code in React Native

import { NativeModules } from 'react-native';

const { Arithmetic } = NativeModules;
const result = await Arithmetic.add(5, 3); // Returns 8

Best Practices

  • Use Android Studio for editing Kotlin files
  • Use Xcode for editing Swift files
  • Rebuild native code after making changes
  • Keep native and JavaScript interfaces in sync
  • Test changes in the app before committing

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • UniFFI - The FFI binding generator
  • Expo - The React Native development platform

About

Template project for integrating Rust via UniFFI into an Expo (React Native) module for iOS and Android.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published