• Stars
    star
    697
  • Rank 63,433 (Top 2 %)
  • Language
    Dart
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Flutter + Rust = ❤️

Flutterust

Flutter + Rust = ❤️

Lints CI (Android, iOS)

Using Rust libs from Flutter using dart:ffi

It provides out-of-the box support for cross-compiling native Rust code for all available iOS and Android architectures and call it from plain Dart using Foreign Function Interface.

This template provides first class FFI support, the clean way.

  • No Swift/Kotlin wrappers
  • No message passing
  • No async/await on Dart
  • Write once, use everywhere
  • No garbage collection
  • Mostly automated development
  • No need to export aar bundles or .framework's
  • If you are lookin on how to use this with async Rust, see scrap example (simple web scrapper).

Project Structure

.
├── android
├── ios
├── lib                     <- The Flutter App Code
├── native                  <- Containes all the Rust Code
│   ├── adder
│   └── adder-ffi
├── packages                <- Containes all the Dart Packages that bind to the Rust Code
│   └── adder
├── target                  <- The compiled rust code for every arch
│   ├── aarch64-apple-ios
│   ├── aarch64-linux-android
│   ├── armv7-linux-androideabi
│   ├── debug
│   ├── i686-linux-android
│   ├── universal
│   ├── x86_64-apple-ios
│   └── x86_64-linux-android
└── test

Setup and Tools

  1. Add Rust build targets

For Android

rustup target add aarch64-linux-android armv7-linux-androideabi x86_64-linux-android i686-linux-android

For iOS

rustup target add aarch64-apple-ios x86_64-apple-ios
  1. Cargo Plugins
cargo install cargo-make

we also use dart-bindgen which requires LLVM/Clang. Install LLVM (10+) in the following way:

ubuntu/linux

  1. Install libclangdev - sudo apt-get install libclang-dev.

Windows

  1. Install Visual Studio with C++ development support.
  2. Install LLVM or winget install -e --id LLVM.LLVM.

MacOS

  1. Install Xcode.
  2. Install LLVM - brew install llvm.

Build and Test

In the Root of the project simply run:

cargo make

Then run flutter app normally

flutter run --no-sound-null-safety

How it works?

The simple idea here is that we build our rust code for all supported targets then build a Flutter Package that uses these targets.

In iOS

we build our rust package using cargo-lipo to build a universal iOS static lib from our rust code after that, we symbol link the built library to our package ios directory, copy the generated bindgen.h file to the ios/Classes the Makefile.toml do these steps for us.

Next we need to add these lines to our package podspec file:

  s.public_header_files = 'Classes**/*.h'
  s.static_framework = true
  s.vendored_libraries = "**/*.a"

but Xcode dose some tree shaking and we currently not using our static lib anywhere in the code, so we open our package's ios/Classes/Swift{PACKAGE_NAME}Plugin.swift and add a dummy method there:

 public static func dummyMethodToEnforceBundling() {
    // call some function from our static lib
    add(40, 2)
  }
In Android

In android it is a bit simpler than iOS, we just need to symbol link some libs in the right place and that is it. our build script creates this folder structure for every package we have:

packages/{PACKAGE_NAME}/android/src/main
├── jniLibs
│   ├── arm64-v8a
│   ├── armeabi-v7a
│   └── x86

Make sure that the Android NDK is installed (From SDK Manager in Android Studio), also ensure that the env variable $ANDROID_NDK_HOME points to the NDK base folder and after that, the build script build our rust crate for all of these targets using cargo-ndk and symbol link our rust lib to the right place, and it just works :)

See also

More Repositories

1

allo-isolate

Run Multithreaded Rust along with Dart VM (in isolate) 🌀
Rust
112
star
2

jsonrpc-ts

Strongly 💪 Typed Fast and lightweight JSON RPC 2.0 Client for Nodejs
TypeScript
65
star
3

impulse

🎇 send a push notification when your long build command finish
Rust
62
star
4

rust-wasm-hotreload

PoC Using WASM Modules as Hot-Reloading Logic from Rust.
Rust
39
star
5

sigma

Sigma σ is a Simple, Safe and Fast Template language
Rust
32
star
6

ytdl

Get Direct [d]own[l]oad ⬇️⬇️ Links of any [y]ou[t]ube Video . Written in Rust, for personal use.
Rust
25
star
7

wadump

CLI Tool to dump and analyze WhatsApp Web Packets
Rust
19
star
8

coemu

Conquer Online 2.0 Server Emulator
Rust
13
star
9

tweetide

[WIP] A Simple Twitter 🐦 Clone Using Tide 🌊 and Rust 🦀
Rust
12
star
10

curve25519-rs

Curve25519 in safe Pure Rust with #[no_std]. ported from rust-crypto crate
Rust
11
star
11

dead-simple-feedback-app

just a dead simple feedback application
HTML
8
star
12

my-free-eBooks

My Collection of free E-Books, for everyone !
8
star
13

a-star-algorithm-js

a simple implementation of A* algorithm in javascript
JavaScript
7
star
14

jax

minimal C-interpreter to play with. for learning purpose
C
6
star
15

twt-about-me

a twitter bot that receives a DM and retweet about it
TypeScript
5
star
16

dotfiles

my swiss army knife
Nix
5
star
17

mocky

[WIP] ⚙️ Generate realistic-fake 👻 data on the fly
Rust
5
star
18

coc-bloc

CoC (NeoVim & Vim) support for the Bloc Library and provides tools for effectively creating Blocs and Cubits for both Flutter and AngularDart apps.
JavaScript
5
star
19

leetcode-rs

💡 Learning Rust 🦀 By Solving Leetcode Problems.
Rust
3
star
20

kontakt-server

Yet another Contact List Manager using gRPC and Rust ❤️
Rust
3
star
21

bitclient-ts

Strongly 💪 Typed Bitcoin JSON RPC Client for Nodejs
TypeScript
3
star
22

core101

TypeScript
3
star
23

cogg-hook-cpp

The Conquer Online Game Guard Hook Prototype in C++
C++
3
star
24

rfx

A tiny command-line utility 🛠 and pretty JSON viewer 🍿
Rust
3
star
25

wallet

ShekozWallet, my personal crypto wallet
Rust
3
star
26

libsignal-protocol-rs

[Archived] Signal Protocol Rust Library (unofficial) 🦀
Rust
3
star
27

rustystream

a simple demo of how to use Rust streams/channels with Dart
Dart
3
star
28

i-love-js

a play JS script with Github that shows Love ❤️
JavaScript
2
star
29

coshield

Simple Conquer Online C++ Anti-Cheat
C++
2
star
30

kontakt-gql

JavaScript
2
star
31

VTAC

Convert Video Files to MP3 with Audio compression
C#
2
star
32

cogg

Conquer Online Game Guard.
Rust
2
star
33

blockchain

Simple Block Chain Mechanism in Typescript for Learning Purpose
TypeScript
2
star
34

loading_switch

Simple Flutter UI Challange inspired by: https://dribbble.com/shots/9841408-Loading-switch-interaction
Dart
2
star
35

shekohex.github.io

My Personal Website
HTML
1
star
36

draw-io

For my draw.io
HTML
1
star
37

XtremeVoteHelper

C#
1
star
38

vue-offline

Vue 🎉 PWA template with webpack Offline Plugin and Typescript. 💥
JavaScript
1
star
39

facetract

Is that a Human? 🕵️‍♂️
Rust
1
star
40

tipbot-contract

smart-contract built to support Edgeware community by tipping.
Rust
1
star
41

shadykhalifa-website

My Website, it could be yours too.
JavaScript
1
star
42

koins

Koin 🪙 store your users' coins easily on blockchain smart contract.
Rust
1
star
43

blockk

Block Spotify Ads & Trackers with ease
Rust
1
star
44

curve25519-js

Curve25519 in WASM works in the browser and Nodejs
Rust
1
star
45

piggybank

Piggy Bank Smart contract for rainy days
Solidity
1
star