Search code examples
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:

    • Precompile Dart code by using Dart snapshots - Dart snapshots are a way of compiling Dart code into a format that can be executed more quickly. However, this approach may not be straightforward for Flutter packages, especially those that rely on Flutter framework internals. In this case you would do something like compiling the Dart Code into a snapshot with something like:
    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).

    • Creating a dart package archive - Another option is to create a .tar.gz archive of your package, which can be hosted on your own repository server. For this you would do pub publish --dry-run. This will create the .tar.gz file of your package. Then you can host the archive file on your own repository server. And update your pubspec.yaml file to include the hosted package:
    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.

    • Use binary artifacts: First you would need to compile the native code into binary artifacts (e.g. .aar for Android, .framework for iOS). Then you distribute those artifacts somehow (your repository server or binary server like Maven for Android or Cocoapods for iOS). Finally you update your pubspec.yaml to reference these artifacts. It would be something like:
    
    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.