开源软件名称(OpenSource Name):brickpop/flutter-rust-ffi开源软件地址(OpenSource Url):https://github.com/brickpop/flutter-rust-ffi开源编程语言(OpenSource Language):Ruby 29.1%开源软件介绍(OpenSource Introduction):Flutter Rust FFI TemplateThis project is a Flutter Plugin template. 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.
Getting startedWrite your native codeEdit your code within Make sure to annotate your exported functions with Returning strings or structs may require using Compile the library
Generated artifacts:
Reference the shared objectsiOSEnsure that ...
s.source = { :path => '.' }
+ s.public_header_files = 'Classes**/*.h'
s.source_files = 'Classes/**/*'
+ s.static_framework = true
+ s.vendored_libraries = "**/*.a"
s.dependency 'Flutter'
s.platform = :ios, '8.0'
... On $ cd flutter/ios
$ ln -s ../rust/target/universal/release/libexample.a . Append the generated function signatures from $ cd flutter/ios
$ cat ../rust/target/bindings.h >> Classes/MylibPlugin.h In our case, it will append NOTE: By default, XCode will skip bundling the ...
public func dummyMethodToEnforceBundling() {
rust_greeting("...");
compress_jpeg_file("...");
compress_png_file("...");
// ...
// This code will force the bundler to use these functions, but will never be called
}
} If you won't be using Flutter channels, the rest of methods can be left empty.
AndroidSimilarly as we did on iOS with You should have the following structure on
As before, if you are not using Flutter channels, the methods within Exposing a Dart API to use the bindingsTo invoke the native code: load the library, locate the symbols and Automatic binding generationTo use ffigen, add the dependency in dev_dependencies:
flutter_test:
sdk: flutter
+ ffigen: ^1.2.0 Also, add the following lines at the end of ffigen:
output: lib/bindings.dart
headers:
entry-points:
- rust/target/bindings.h
name: GreeterBindings
description: Dart bindings to call mylib functions On MacOS: brew install llvm
flutter pub run ffigen:setup -I/usr/local/opt/llvm/include -L/usr/local/opt/llvm/lib On Linux: sudo apt-get install -y clang libclang-dev
flutter pub run ffigen:setup Generate flutter pub run ffigen Finally, use the generated Manual bindingsLoad the library: final DynamicLibrary nativeExampleLib = Platform.isAndroid
? DynamicLibrary.open("libexample.so") // Load the dynamic library on Android
: DynamicLibrary.process(); // Load the static library on iOS Find the symbols we want to use, with the appropriate Dart signatures: final Pointer<Utf8> Function(Pointer<Utf8>) rustGreeting = nativeExampleLib
.lookup<NativeFunction<Pointer<Utf8> Function(Pointer<Utf8>)>>("rust_greeting")
.asFunction();
final void Function(Pointer<Utf8>) freeGreeting = nativeExampleLib
.lookup<NativeFunction<Void Function(Pointer<Utf8>)>>("rust_cstr_free")
.asFunction(); Call them: // Prepare the parameters
final name = "John Smith";
final Pointer<Utf8> namePtr = Utf8.toUtf8(name);
print("- Calling rust_greeting with argument: $namePtr");
// Call rust_greeting
final Pointer<Utf8> resultPtr = rustGreeting(namePtr);
print("- Result pointer: $resultPtr");
final String greetingStr = Utf8.fromUtf8(resultPtr);
print("- Response string: $greetingStr"); When we are done using freeGreeting(resultPtr); More information
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论