flutterflutter-dependenciesflutter-packages

Is it possible to compile a flutter package to import only the resulting binary in the project?


I'm new to flutter, I've been creating a few packages in flutter that would be imported in several other (flutter) project. This packages are independently versioned.

My question is 'Is it possible to "compile" a flutter package (only dart code and some dependencies of flutter plugins) in order to obtain a binary file that I can add to my flutter project?'

I know I can add it as a dependency by path or by git, but my goal would be having several of this possible binary files in a dependency repository of my own...

So, is it possible?


Solution

  • Yes, this is possible but required some more complex implementations. it is possible to compile a Flutter package into a binary format that can be used as a dependency in other Flutter projects, although this is not a common practice in Flutter development - typical way would be to share Flutter packages is via source code through Git or pub.dev (or even a private pub.dev).

    These are a few solutions I can think off, but haven't tried them fully in latest versions:

    dart --snapshot=<your package>.snapshot <your package>.dart
    

    Then include the snapshot in your project (add the generated snapshot to your flutter project). In your project you would load and execute the snapshot, this might require some low-level Dart APIs and migt not be practical for typical Flutter development. (If anyone has more details on how to do this, please contribute).

    dependencies:
      your_package:
        hosted:
          name: <your package>
          url: https://<your repo server>.com
        version: ^1.0.0
    

    This is all fine for dart code packages, if you want to include platform specific code, then you would need something like the next example. Please notice that I don't have experience for all types of targets Flutter supports, but at least for Android/iOS this might be possible.

    
    flutter:
      plugin:
        platforms:
          android:
            pluginClass: com.example.YourPlugin
            dependencies:
              - group: com.example
                name: your_plugin
                version: 1.0.0
                url: https://your-repo-server.com/maven
          ios:
            pluginClass: YourPlugin
            podspec: https://your-repo-server.com/cocoapods/YourPlugin.podspec
    
    

    Again, I haven't tried to use these for the same purpose you seem to have, but they should work. I took these ideas from multiple existing plugins and some projects I've built.