SlideShare a Scribd company logo
6
Most read
16
Most read
20
Most read
Ian Wagner, July 27, 2023
Building a Cross-Platform
Mobile SDK in Rust
Seoul Rust Meetup
About Me
• iOS developer since 2008
• Shipping Rust in prod since 2018
• Currently role: Cofounder of Stadia Maps
Background
• Building a mobile navigation
SDK
• Hexagonal architecture:
• Everything communicates
across clear API boundaries
• Core business logic in Rust
• Native Swift and Kotlin
wrappers call into Rust via FFI
Interesting Problems
• Idiomatic bindings for Swift and Kotlin
• Compiling and linking for multiple targets
• Shipping the library with a high-level native wrapper
UniFFI
Idiomatic binding generator
• Open-source idiomatic binding generator from Mozilla
• Supports Kotlin, Swift, Python, and Ruby
• Interface De
fi
nition Language (IDL)
fi
le speci
fi
es what’s exported
UniFFI
Feels like magic
• Generated APIs generally idiomatic as promised
• Support for a wide variety of system types, incl. timestamps(!)
• Generates protocols / interfaces as well as concrete types
• Support for native callbacks and even full interface implementations(!!)
• Bridge in platform-native APIs (ex: keychain)
• Allow users to bring their own implementation (ex: custom o
ffl
ine routing)
uniffi-bindgen on iOS
Currently using a build script (better automation TBD)
uniffi-bindgen on Android
Gradle invokes cargo
Calling from Swift and Kotlin
UniFFI
Challenges
• Need to have all exported types visible from the top level(?)
• Not very much documentation on the
fi
ner points of the UDL
fi
le
• Unable to split into multiple modules / use multiple UDL
fi
les at once
• Fairly useless errors when your UDL is wrong
• Only generates bindings; doesn’t actually help you ship a lib (we’ll revisit this)
UniFFI
Tips
• Leverage generated protocol/interface types
• UniFFI generates protocols/interfaces for you!
• Rule of thumb: pass the protocol/interface; construct the concrete type
• Key bene
fi
t: easier mocking
• Third-party plugin for syntax highlighting UDL
fi
les available for CLion
• Use interior mutability patterns (must be both Send and Sync!); you can’t use
mutating struct methods; we currently use std
:
:
sync
:
:
Mutex<T>
Cross compilation
Surprisingly easy on its
face for iOS!
Cross-Platform Compilation
Android quirks
• In contrast to Apple, with deep
support for C ABI libs, the
Android NDK is a nightmare
• Need to set up ~/.cargo/
config.toml with the exact
linker paths for your NDK
Shipping the library
• Cross-compilation is the start, but there’s more!
• High-level wrapper (pretty straightforward but some quirks!)
• Binary issues
• Packaging
• You have to deal with a number of quirks on each platform; we’ll only look at
iOS today
Shipping the library
Swift quirks
• The Swift language has no (sanctioned) way to mark types for re-export
• Common pattern: Rust core -> UniFFI lower-level wrapper -> high level Swift package that exposes a
more ergonomic interface and hides FFI
• Types returned by methods in your package are visible and inspectable…
• But you can’t construct these types or even reference the type anywhere!
• No variables
• No return types
• No function args
• No enum variant unpacking(?!)
• SPM has some strange rules, like needing to have Package.swift in the repo root
Shipping the library
iOS quirks
• Building an XCFramework
• Apple’s native format for
distributing multi-platform binary
frameworks
• Easiest distribution format for
downstream consumers
• Quirk: iOS Simulator is one
platform but needs multiple archs
(Intel and ARM)
• Creating a fat simulator lib: lipo
Shipping the library
SPM quirks
• Local development hides challenges
• .binaryTarget(name:path:) works great for local development, but…
• If you want to add a local dependency on your package from another, SPM
chokes since this isn’t meant to go more than one level deep (design
fl
aw)
• So you have to push artifacts to GitHub or something
• Silver lining: can still make Swift changes and reference master + your old
release framework artifact and track main; just can’t touch the Rust lib
Bringing it all
together
Running in SwiftUI
Open Questions
• Repo structure: monorepo seems best for dev
• Build system: use native build systems or something like Bazel?
• Android: haven’t explored the edges as far so probably some unknown
unknowns
Resources
• UniFFI User Guide - https://mozilla.github.io/uni
ffi
-rs/
• uni
ffi
-dl (Uno
ffi
cial IntelliJ plugin) - https://github.com/Lonami/uni
ffi
-dl
• Compiling Rust libraries for Android apps: a deep dive - https://
gendignoux.com/blog/2022/10/24/rust-library-android.html
• Ferrostar - https://github.com/stadiamaps/ferrostar

More Related Content

PPTX
Auto-Generating Language-Specific Wrappers for Rust Libraries
PDF
Building static libraries for iOS with CocoaPods
PDF
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
PDF
Gigigo Workshop - Create an iOS Framework, document it and not die trying
PDF
Dependent things dependency management for apple sw - slideshare
PDF
MPD2011 | Александр Додатко "Процесс непрерывной интеграции для iOS проектов"
PDF
RubyMotion Inspect Conference - 2013. (With speaker notes.)
PPTX
Top 6 iOS Libraries Every iOS Developer Should Use In The Project.pptx
Auto-Generating Language-Specific Wrappers for Rust Libraries
Building static libraries for iOS with CocoaPods
Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and...
Gigigo Workshop - Create an iOS Framework, document it and not die trying
Dependent things dependency management for apple sw - slideshare
MPD2011 | Александр Додатко "Процесс непрерывной интеграции для iOS проектов"
RubyMotion Inspect Conference - 2013. (With speaker notes.)
Top 6 iOS Libraries Every iOS Developer Should Use In The Project.pptx

Similar to Building a Cross-Platform Mobile SDK in Rust.pdf (20)

PDF
Rust With async / .await
PDF
JS Fest 2019/Autumn. Алексей Орленко. Node.js N-API for Rust
PDF
Introduction to Swift
KEY
iPhone OS: The Next Killer Platform
PDF
Mobile Fest 2018. Алексей Лизенко. Make your project great again
PDF
Formacion en movilidad: Conceptos de desarrollo en iOS (I)
PDF
Андрей Володин — Как подружиться с роботом
PDF
Orthogonality: A Strategy for Reusable Code
PDF
Interface Oxidation
PDF
The Rust Programming Language: an Overview
PPTX
Developing a mobile cross-platform library
PPTX
iphone application development
PPTX
Making Swift Native Modules in React Native
PDF
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
PPTX
iOS Development at Scale @Chegg
PDF
Welcome to New Swift: Library Evolution & LSP Support
PDF
A Framework Driven Development
PDF
Programming iOS in Lua - A bridge story
PDF
Writing native bindings to node.js in C++
PDF
Bridging the gap – plugin for unity and i os
Rust With async / .await
JS Fest 2019/Autumn. Алексей Орленко. Node.js N-API for Rust
Introduction to Swift
iPhone OS: The Next Killer Platform
Mobile Fest 2018. Алексей Лизенко. Make your project great again
Formacion en movilidad: Conceptos de desarrollo en iOS (I)
Андрей Володин — Как подружиться с роботом
Orthogonality: A Strategy for Reusable Code
Interface Oxidation
The Rust Programming Language: an Overview
Developing a mobile cross-platform library
iphone application development
Making Swift Native Modules in React Native
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
iOS Development at Scale @Chegg
Welcome to New Swift: Library Evolution & LSP Support
A Framework Driven Development
Programming iOS in Lua - A bridge story
Writing native bindings to node.js in C++
Bridging the gap – plugin for unity and i os
Ad

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
history of c programming in notes for students .pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Introduction to Artificial Intelligence
PPTX
Essential Infomation Tech presentation.pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Nekopoi APK 2025 free lastest update
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Digital Strategies for Manufacturing Companies
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
L1 - Introduction to python Backend.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
AI in Product Development-omnex systems
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Transform Your Business with a Software ERP System
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
history of c programming in notes for students .pptx
Odoo POS Development Services by CandidRoot Solutions
Introduction to Artificial Intelligence
Essential Infomation Tech presentation.pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Nekopoi APK 2025 free lastest update
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Wondershare Filmora 15 Crack With Activation Key [2025
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Digital Strategies for Manufacturing Companies
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
L1 - Introduction to python Backend.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
AI in Product Development-omnex systems
VVF-Customer-Presentation2025-Ver1.9.pptx
Transform Your Business with a Software ERP System
Ad

Building a Cross-Platform Mobile SDK in Rust.pdf

  • 1. Ian Wagner, July 27, 2023 Building a Cross-Platform Mobile SDK in Rust Seoul Rust Meetup
  • 2. About Me • iOS developer since 2008 • Shipping Rust in prod since 2018 • Currently role: Cofounder of Stadia Maps
  • 3. Background • Building a mobile navigation SDK • Hexagonal architecture: • Everything communicates across clear API boundaries • Core business logic in Rust • Native Swift and Kotlin wrappers call into Rust via FFI
  • 4. Interesting Problems • Idiomatic bindings for Swift and Kotlin • Compiling and linking for multiple targets • Shipping the library with a high-level native wrapper
  • 5. UniFFI Idiomatic binding generator • Open-source idiomatic binding generator from Mozilla • Supports Kotlin, Swift, Python, and Ruby • Interface De fi nition Language (IDL) fi le speci fi es what’s exported
  • 6. UniFFI Feels like magic • Generated APIs generally idiomatic as promised • Support for a wide variety of system types, incl. timestamps(!) • Generates protocols / interfaces as well as concrete types • Support for native callbacks and even full interface implementations(!!) • Bridge in platform-native APIs (ex: keychain) • Allow users to bring their own implementation (ex: custom o ffl ine routing)
  • 7. uniffi-bindgen on iOS Currently using a build script (better automation TBD)
  • 9. Calling from Swift and Kotlin
  • 10. UniFFI Challenges • Need to have all exported types visible from the top level(?) • Not very much documentation on the fi ner points of the UDL fi le • Unable to split into multiple modules / use multiple UDL fi les at once • Fairly useless errors when your UDL is wrong • Only generates bindings; doesn’t actually help you ship a lib (we’ll revisit this)
  • 11. UniFFI Tips • Leverage generated protocol/interface types • UniFFI generates protocols/interfaces for you! • Rule of thumb: pass the protocol/interface; construct the concrete type • Key bene fi t: easier mocking • Third-party plugin for syntax highlighting UDL fi les available for CLion • Use interior mutability patterns (must be both Send and Sync!); you can’t use mutating struct methods; we currently use std : : sync : : Mutex<T>
  • 12. Cross compilation Surprisingly easy on its face for iOS!
  • 13. Cross-Platform Compilation Android quirks • In contrast to Apple, with deep support for C ABI libs, the Android NDK is a nightmare • Need to set up ~/.cargo/ config.toml with the exact linker paths for your NDK
  • 14. Shipping the library • Cross-compilation is the start, but there’s more! • High-level wrapper (pretty straightforward but some quirks!) • Binary issues • Packaging • You have to deal with a number of quirks on each platform; we’ll only look at iOS today
  • 15. Shipping the library Swift quirks • The Swift language has no (sanctioned) way to mark types for re-export • Common pattern: Rust core -> UniFFI lower-level wrapper -> high level Swift package that exposes a more ergonomic interface and hides FFI • Types returned by methods in your package are visible and inspectable… • But you can’t construct these types or even reference the type anywhere! • No variables • No return types • No function args • No enum variant unpacking(?!) • SPM has some strange rules, like needing to have Package.swift in the repo root
  • 16. Shipping the library iOS quirks • Building an XCFramework • Apple’s native format for distributing multi-platform binary frameworks • Easiest distribution format for downstream consumers • Quirk: iOS Simulator is one platform but needs multiple archs (Intel and ARM) • Creating a fat simulator lib: lipo
  • 17. Shipping the library SPM quirks • Local development hides challenges • .binaryTarget(name:path:) works great for local development, but… • If you want to add a local dependency on your package from another, SPM chokes since this isn’t meant to go more than one level deep (design fl aw) • So you have to push artifacts to GitHub or something • Silver lining: can still make Swift changes and reference master + your old release framework artifact and track main; just can’t touch the Rust lib
  • 19. Open Questions • Repo structure: monorepo seems best for dev • Build system: use native build systems or something like Bazel? • Android: haven’t explored the edges as far so probably some unknown unknowns
  • 20. Resources • UniFFI User Guide - https://mozilla.github.io/uni ffi -rs/ • uni ffi -dl (Uno ffi cial IntelliJ plugin) - https://github.com/Lonami/uni ffi -dl • Compiling Rust libraries for Android apps: a deep dive - https:// gendignoux.com/blog/2022/10/24/rust-library-android.html • Ferrostar - https://github.com/stadiamaps/ferrostar