flutter_rust_bridge icon indicating copy to clipboard operation
flutter_rust_bridge copied to clipboard

Distribute dynamically linked binaries on iOS/MacOS

Open brookman opened this issue 1 year ago • 2 comments

Hi @fzyzcjy,

Thank you for creating this excellent Flutter/Rust integration solution. It's a remarkable contribution!

I am currently developing a package for pub.dev, which is a wrapper around a Rust crypto crate using flutter_rust_bridge. My goal is to ensure users of my library do not need to install the Rust toolchain.

Using the mimir package as a template, I've managed to set up GitHub actions to build my package for Windows, iOS, and macOS. These actions also configure the binaries to be downloaded during the Flutter build process. For iOS and macOS, I build an "xcframework" that contains multiple "lib...a" files. On the Flutter/Dart side, I have the following code:

static Future<void> _ensureInitialized() async {
    if (_initialized) {
      return;
    }

    if (io.Platform.isIOS || io.Platform.isMacOS) {
      // TODO(brookman): Use dynamic linking for iOS and MacOS?
      final lib = ExternalLibrary.process(iKnowHowToUseIt: true);
      await RustLib.init(externalLibrary: lib);
    } else {
      await RustLib.init();
    }
    
    _initialized = true;
  }

This approach works as expected as long as there is only one package using flutter_rust_bridge in the project (if I understand the docs correctly).

Is there a way to enhance this solution to use dynamic linking on iOS and macOS? I'm not very familiar with the Apple ecosystem. My initial attempt involved modifying the build to generate .dylib files instead of .a files, but this resulted in an error stating that .dylib files are not allowed in xcframeworks. Should I move away from using xcframeworks, or is there some signing process I need to follow?

Thank you in advance for your help!

brookman

brookman avatar May 26 '24 14:05 brookman

Hi! Thanks for opening your first issue here! :smile:

welcome[bot] avatar May 26 '24 14:05 welcome[bot]

Is there a way to enhance this solution to use dynamic linking on iOS and macOS?

I am also not an expert of ios/macos, but here is how flutter_rust_bridge (and flutter's default template IIRC) does it: https://github.com/fzyzcjy/flutter_rust_bridge/blob/d46844b8855891385509d4a4726f8ac0ec8aabf5/frb_dart/lib/src/loader/_io.dart#L65. Seems that you can directly open the framework as a dynamic library

fzyzcjy avatar May 27 '24 00:05 fzyzcjy

Close since inactivity. Feel free to reopen if you have any questions!

fzyzcjy avatar Jun 18 '24 00:06 fzyzcjy

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.

github-actions[bot] avatar Jul 02 '24 01:07 github-actions[bot]