Giter Site home page Giter Site logo

brickpop / flutter-rust-ffi Goto Github PK

View Code? Open in Web Editor NEW
587.0 587.0 52.0 184 KB

Starter project for Flutter plugins willing to access native and synchronous rust code using FFI

License: Other

Kotlin 9.78% Ruby 29.06% Swift 9.01% Objective-C 6.71% Dart 22.27% Makefile 20.93% Rust 2.25%
flutter mobile native rust

flutter-rust-ffi's People

Contributors

brickpop avatar olegnet avatar stoically avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flutter-rust-ffi's Issues

note: ld: error: unable to find library -lgcc

when i use make all
= note: ld: error: unable to find library -lgcc
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error

error: could not compile example-rs

To learn more, run the command again with --verbose.
make: *** [target/aarch64-linux-android/release/libexample.so] Error 101

Ensure the bundling of C symbols on iOS archive

Option 1

Follow https://flutter.dev/docs/development/platform-integration/c-interop#ios-symbols-stripped

Option 2

See from https://github.com/drewcrawford/Rust-iOS-Example#the-case-of-the-missing-symbols


What seems to happen is Xcode strips some symbols from a framework when it's built. So if you have libhello.a inside a framework, and the framework doesn't call anything inside libhello.a, all of libhello.a is striped. If you only call one function, the others are stripped etc.

So what you have to do is declare an exported_symbols.txt like

_hello
_benchmark

And then set that file as the framework's Exported Symbols File in the framework's build settings.

How to force Xcode to link unused

From the README:

NOTE: By default, XCode will skip bundling the libexample.a library if it detects that it is not being used. To force its inclusion, add a dummy method in SwiftMylibPlugin.swift that uses at least one of the native functions.

Do you know how to force Xcode to always bundle static libs? In my case, I'm not using a Flutter plugin, and just trying to use a single universal .a file.

My generated dart file is getting errors like missing_const_final_var_or_type and implicit_this_reference_in_initializer

Hi!

Copied this example. My pubspec:

dependencies:
  flutter:
    sdk: flutter
  ffi: ^1.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter
  ffigen: 4.1.3

ffigen:
  name: NativeLibrary
  description: Bindings to `headers/example.h`.
  output: 'lib/generated_bindings.dart'
  headers:
    entry-points:
      - 'rust/bindings.h'

My header is only 1 row and is: void start_application(void);.
So when I run flutter pub run ffigen it creates a dart file with errors like: Variables must be declared using the keywords 'const', 'final', 'var' or a type name. Try adding the name of the type of the variable or the keyword 'var'. and then The instance member '_start_applicationPtr' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression

Any clue why I get these errors?

Calling dart/flutter functions from Rust

Is it possible to call flutter functions from Rust? I know it's possible when using JNI-rust (by interacting with JniEnv methods), but I'm not sure if it's possible in the dart/flutter context

Web support?

Is it possible to call Rust from Flutter Web using wasm?

Works on debug, but not on release

Hello and thanks for the template and article.

I'm trying to use the produced .a file as a static library, I'm including the .h on the bridge header file and everything works on debug on a real-device and emulator.

However when I archive and test from Testflight I get a grey screen, if I comment out all the Rust invocations from Flutter, the App works as expected.
Is there something specific that needs to be done in order for the library to be included? I have it on embedded libraries and frameworks. Not sure what else to do.

Thanks in advance!

Thank you

Thank you for solving this ! I've been working hard on this too and I don't think I would have found the trick of adding a dummy function to my swift library. That's a weird behavior, don't you think there is something we can do ? Maybe the Dart team can have a look at this.

For the android part I prefer to use cargo-gradle because it rebuilds when my Rust code changes you should definitely check it out.

How to access database in rust flutter

Rust has few libraries that allows database access like sqlite and also native storat. Is it possible to do it with rust and access rust function via flutter?

Basically I want push as much as on Rust side as I'm not that happy with flexibility of dart language.

BTW thanks for awesome template. :-)

x86_64 arch target

Why do you left x86_64,
until now flutter release support arm64, arm arches but in latest they added x86_64 for latest target, so it is better to add x86_64 target also.

How to write test for the rust functions

I tried to write some test, under both ./test and ./example/test

but it comes with error:

Invalid argument(s): Failed to lookup symbol (dlsym(RTLD_DEFAULT, my_function_name): symbol not found)
  dart:ffi                                                          DynamicLibrary.lookup

Though it can be called in the application.

I tried to add some delay for loading the test, but still failed, probabaly some extra setting is required.

It should more related to ffi package, discussed it there: dart-archive/ffi#39

Error on flutter run: "Your app is using an unsupported Gradle project. To fix this problem, create a new project"

Steps to reproduce

  1. clone the repo.
  2. cd inside
  3. start an eligible x86 Android Emulator
  4. flutter run lib/mylib.dart

Result

$ flutter run lib/mylib.dart
Using hardware rendering with device Android SDK built for x86. If you notice graphics artifacts, consider enabling software rendering with
"--enable-software-rendering".
Launching lib/mylib.dart on Android SDK built for x86 in debug mode...
Exception: [!] Your app is using an unsupported Gradle project. To fix this problem, create a new project by running flutter create -t app <app-directory> and then move the dart code, assets and pubspec.yaml to the new project.

Notes

The same happens after renaming mylib.dart to main.dart and do flutter run.

flutter doctor

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.1, on Mac OS X 10.15.4 19E287 darwin-x64, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.55.2)
[✓] Connected device (2 available)

• No issues found!

Pipe Rust standard input/output to Dart

Hi,
I am asking myself if I can use this template to call native rust functions and then write to the standard input of the rust
library as well as read from its standard output. Do you know if this is possible? I am asking because I am trying
to integrate a UCI Chess Engine (Stockfish, more specifically its Rust port called Rustfish) into a Flutter App and therefore I need to be able to write to/ read from the standard input/ output to send UCI commands and receive their answers.
I would be very happy if you could help me here.

Add linux compatibility

Is there some way to generate linux bindings, I'm building for linux arm64 and I need to access my rust code. I'm building a dbus wrapper if I can't but I'd like to access it directly.

Not able to use the template for integrating a bigger rust library

First of all, thank you for this nice template. I am currently trying to integrate the Stockfish chess engine into Flutter using a Rust port of Stockfish (https://github.com/syzygy1/Rustfish) together with your template. However, I am not able to get it running at the moment and I don't know what the problem is.

The lib.rs file I use for this template is the Rustfish source code bundled into a single file (adjusting the function signature of the main function I wand to call as stated in the readme of your template). The bundled rust library works fine as it is but after integrating it into flutter using the template, it will only crash when calling the main function of the library. All I get is the following error stacktrace where I sadly can't make anything out of:

I/flutter (14269): - Mylib bindings found 👍
I/flutter (14269):   DynamicLibrary: handle=0xbea78c8197f77fe5
I/flutter (14269): - Calling native rustfish test
I/flutter (14269): - Executed
I/flutter (14269): - Calling native rustfish main
F/libc    (14269): Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 14318 (1.ui), pid 14269 (b.mylib_example)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: ****
Revision: '0'
ABI: 'arm64'
Happend: 'Sat Nov 28 18:46:14 2020
'
SYSVMTYPE: Art
APPVMTYPE: Art
pid: 14269, tid: 14318, name: 1.ui  >>> org.mylib.mylib_example <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
    x0  0000000000000000  x1  00000000000037ee  x2  0000000000000006  x3  0000000000000008
    x4  00000070e6f5f1c8  x5  00000070e6f5f1c8  x6  00000070e6f5f1c8  x7  00000070e6f5f000
    x8  0000000000000083  x9  539e262590de5120  x10 0000000000000000  x11 fffffffc7ffffbdf
    x12 0000000000000001  x13 0000000000002710  x14 0000000051eb851f  x15 0000000000000064
    x16 00000071942f12c0  x17 000000719422fe34  x18 0000000000000001  x19 00000000000037bd
    x20 00000000000037ee  x21 00000070e7019dc0  x22 00000070e70eda70  x23 00000070e6ff8208
    x24 0000000000000001  x25 0000000000000000  x26 0000000000011803  x27 0000000000015ac4
    x28 fffffffffffffffe  x29 00000070f3bfdc30
    sp  00000070f3bfdbf0  lr  0000007194224960  pc  0000007194224988
backtrace:
    #00 pc 0000000000022988  /system/lib64/libc.so (abort+116)
    #01 pc 000000000006a72c  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #02 pc 0000000000067dc4  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #03 pc 0000000000068d3c  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #04 pc 0000000000068cc4  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #05 pc 0000000000068840  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #06 pc 0000000000066fec  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #07 pc 0000000000068808  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #08 pc 000000000007b988  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #09 pc 000000000007b95c  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #10 pc 00000000000496b0  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #11 pc 00000000000492f0  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
    #12 pc 0000000000030784  /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so (main+188)
    #13 pc 0000000000005564  <anonymous:00000070f2a00000>

The dart file where I try to call the native rust code is the following:

import 'dart:ffi';
import 'dart:io';

///////////////////////////////////////////////////////////////////////////////
// Typedef's
///////////////////////////////////////////////////////////////////////////////

typedef RustMainFunc = void Function();
typedef RustMainFuncNative = Void Function();

typedef RustTestFunc = void Function();
typedef RustTestFuncNative = Void Function();

///////////////////////////////////////////////////////////////////////////////
// Load the library
///////////////////////////////////////////////////////////////////////////////

final DynamicLibrary nativeRustfishLib = Platform.isAndroid
    ? DynamicLibrary.open("librustfish.so")
    : DynamicLibrary.process();

///////////////////////////////////////////////////////////////////////////////
// Locate the symbols we want to use
///////////////////////////////////////////////////////////////////////////////

final RustMainFunc rustMain = nativeRustfishLib
  .lookup<NativeFunction<RustMainFuncNative>>("main")
  .asFunction();

final RustTestFunc rustTest = nativeRustfishLib
    .lookup<NativeFunction<RustTestFuncNative>>("test")
    .asFunction();

///////////////////////////////////////////////////////////////////////////////
// HANDLERS
///////////////////////////////////////////////////////////////////////////////

String nativeRustfishMain() {
  if (nativeRustfishLib == null)
    return "ERROR: The native rustfish library is not initialized 🙁";

  print("- Mylib bindings found 👍");
  print("  ${nativeRustfishLib.toString()}"); // Instance info

  print("- Calling native rustfish test");
  // The actual native call
  rustTest(); // working dummy method call
  print("- Executed");

  print("- Calling native rustfish main");
  // The actual native call
  rustMain(); // crashing
  print("- Executed");

  return "Success";
}

I would be very glad about any king of help on setting this up.

Could you write a similar article on how to use Golang and Flutter together for desktop?

I have been trying to setup a project to use Flutter and Golang together for macOS.

I followed two of your articles https://medium.com/stack-me-up/meet-the-coolest-tech-marriage-flutter-go-369cf11367c9 and https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2 but unfortunately I could not get the setup happen.

I got lost at macOS bundle security and dylib linking using xcode.

How do I expose the golang native functions into flutter app for desktop?
Could you write a similar article on how to use Golang and Flutter together for desktop or any pointers will be appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.