Foundation-Devices/tor as a multi-platform Flutter FFI plugin for starting and stopping the Tor daemon. Based on libtor-sys.
cargo install cargo-ndk
sudo apt install git build-essential cmake llvm clang pkg-config cargo rustc libssl-dev libc6-dev-i386
Run build script
cd scripts/linux
./build_all.sh
Run the NDK setup and build scripts
cd scripts/android
./install_ndk.sh
./build_all.sh
To generate tor-ffi.h
C bindings for Rust, cbindgen --config cbindgen.toml --crate tor-ffi --output target/tor-ffi.h
or cargo build
in native/tor-ffi
to produce headers according to build.rs
To generate tor_bindings_generated.dart
Dart bindings for C, flutter pub run ffigen --config ffigen.yaml
flutter run
in example
to run the example app
See example/lib/main.dart
for usage. Must run the build script for your platform first.
This project is a starting point for a Flutter FFI plugin, a specialized package that includes native code directly invoked with Dart FFI.
This template uses the following structure:
-
src
: Contains the native source code, and a CmakeFile.txt file for building that source code into a dynamic library. -
lib
: Contains the Dart code that defines the API of the plugin, and which calls into the native code usingdart:ffi
. -
platform folders (
android
,ios
,windows
, etc.): Contains the build files for building and bundling the native code library with the platform application.
The pubspec.yaml
specifies FFI plugins as follows:
plugin:
platforms:
some_platform:
ffiPlugin: true
This configuration invokes the native build for the various target platforms and bundles the binaries in Flutter applications using these FFI plugins.
This can be combined with dartPluginClass, such as when FFI is used for the implementation of one platform in a federated plugin:
plugin:
implements: some_other_plugin
platforms:
some_platform:
dartPluginClass: SomeClass
ffiPlugin: true
A plugin can have both FFI and method channels:
plugin:
platforms:
some_platform:
pluginClass: SomeName
ffiPlugin: true
The native build systems that are invoked by FFI (and method channel) plugins are:
- For Android: Gradle, which invokes the Android NDK for native builds.
- See the documentation in android/build.gradle.
- For iOS and MacOS: Xcode, via CocoaPods.
- See the documentation in ios/flutter_libtor.podspec.
- See the documentation in macos/flutter_libtor.podspec.
- For Linux and Windows: CMake.
- See the documentation in linux/CMakeLists.txt.
- See the documentation in windows/CMakeLists.txt.
To use the native code, bindings in Dart are needed.
To avoid writing these by hand, they are generated from the header file
(src/flutter_libtor.h
) by package:ffigen
.
Regenerate the bindings by running flutter pub run ffigen --config ffigen.yaml
.
Very short-running native functions can be directly invoked from any isolate.
For example, see sum
in lib/flutter_libtor.dart
.
Longer-running functions should be invoked on a helper isolate to avoid
dropping frames in Flutter applications.
For example, see sumAsync
in lib/flutter_libtor.dart
.
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.