Importing a static library (Rust .a) to Flutter project in iOS

Viewed 1514

I have followed the instructions from here and here

And although the .a library and the functions work as expected on Debug (simulator and real device), when I Archive and test through TestFlight the Flutter App begins with a grey background (which I read that it means some kind of error).

If I go ahead and remove all calls of the Rust lib then the Testflight opens normally.

Note: I have also added the .a library as a Linked framework from the XCode and I have included the .h file to the bridging-header.h of the project.

I have also

  • run flutter build --release
  • cleared the derived data
  • pods cache
  • even re-made the iOS folder from scratch.

Is there something else that I'm missing here?

1 Answers

I managed to make it work by creating a Flutter plugin flutter create -t plugin then I imported the .a file on the /iOS folder and included all the rust functions on the .h file inside /iOS/Classes (these are automatically created).

Then add a sample function for each of the rust functions inside the .Swift file in /iOS/Classes And make sure to include

flutter:
  plugin:
    pluginClass: ApproverRustPlugin

On your .yaml file of the plugin.

Also include these on the plugin .podspec file

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

Once all these are done. Make sure to include your plugin (assuming it exists on the same folder as the main App) on the main app .yaml file.

dependencies:
  flutter:
    sdk: flutter
  approver_rust:
    path: "./approver_rust"

For reference:

Pod::Spec.new do |s|
  s.name             = 'approver_rust'
  s.version          = '0.0.1'
  s.summary          = 'A new flutter plugin project.'
  s.description      = <<-DESC
A new flutter plugin project.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'email@example.com' }
  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, '9.0'

  # Flutter.framework does not contain a i386 slice.
  s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
  s.swift_version = '5.0'
end
Related