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

1,049

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 protected]' }
  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
Share:
1,049
MDompekidis
Author by

MDompekidis

I work at 7Linternational.com as an application engineer, while I try to create/learn how to make games mostly in HTML5. I generally like action films and tv series, I play most of Blizzards games and I'm an old-school rpg player/fan. You can view my code-helping blog here: nightlycoding blog

Updated on December 24, 2022

Comments

  • MDompekidis
    MDompekidis over 1 year

    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?

  • Yogendra Patel
    Yogendra Patel about 3 years
    can you please send whole code of .yaml and .podspec file, i need for reference.
  • MDompekidis
    MDompekidis about 3 years
    The podspec and yaml (Except for the changes above) are auto generated when you do flutter create -t plugin