I am working on a flutter project that runs fine in development. However I do not know how to get the build to include dll's referenced using FFI.
I can't find clear instructions on how to do it.
I tried following the steps to build an msix here, which worked but didn't seem to include the dll (it fails in the same way as the regular build)
what is the procedure to have the build process consider the dll's?
other dll's show up in the build directory from 3rd party packages so there must be a way right?
That's really hard to discover by your own, but indeed you can bind those libraries to you MSIX. In my case I just made a package for label printers using Dart FFI and DLL`s provided by manufacturer and this is how I did it.
You need to add these DLL's to your assets setting on pubspec.yaml from your package. This is my case:
[...]
flutter:
  [...]
  assets:
    - assets/WinPort.dll
    - assets/Winppla.dll
    - assets/Winpplb.dll
    - assets/Winpplz.dll
With this setting you will embed your DLL files in your final MSIX, but this was the easy part. Now you have make sure to load the proper load these files in code.
Based on my own tests, I still dealing with two ways to develop and test code, the first one is when I am running a project in my machine via flutter run I must set the target for current.path, when I get it done and start building for deploy I change this to resolvedExecutable.parent.path. Where is what you need to do.
Loading you DLL in development environment (flutter run):
final String _packageAssetsDirPath = normalize(join(Directory.current.path,'assets'));
On production environment (running from .exe or MSIX installed):
final String _assetsPackageDir = normalize(
      join('data', 'flutter_assets', 'packages', 'YOUR_PACKAGE_NAME', 'assets'));
  final String _exeDirPath = File(Platform.resolvedExecutable).parent.path;
  final String _packageAssetsDirPath =
      normalize(join(_exeDirPath, _assetsPackageDir));
After with this var called _packageAssetsDirPath will be easy to load your DLL's, now you invoke a DynamicLibrary constructor:
// Path for DLL file
final String _libDllSourceFullPath =
      normalize(join(_packageAssetsDirPath, 'Winppla.dll'));
// Target for copy, place DLL in same place the .exe you are running
final String _libDllDestFullPath =
      normalize(join(_packageAssetsDirPath, 'YOUROWN.dll'));
// Try to copy for running exe path
File(_libDllSourceFullPath).copySync(_libDllDestFullPath);
// With this copy, would be simple to load, and if it fails, try in full path
// LOAD DLL
  try {
    String _packageAssetsDirPath =
        normalize(join(Directory.current.path, 'assets'));
    String _printerLibraryPath =
        normalize(join(_packageAssetsDirPath, 'Winppla.dll'));
    DynamicLibrary _library = DynamicLibrary.open(_printerLibraryPath);
    return _library;
  } catch (e) {
    try {
      DynamicLibrary _library = DynamicLibrary.open('Winppla.dll');
      return _library;
    } catch (e) {
      // Avoing errors creating a fake DLL, but you could deal with an exception
      return DynamicLibrary.process();
    }
  }
At this point you can load a DLL and use it, you can check my package full code at https://github.com/saviobatista/argox_printer check for lib/src/ppla.dart at function _setupDll() and you will see that loading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With