Search code examples
iosflutterframeworksstatic-librariesdynamic-frameworks

How to export Flutter project as SDK (iOS dynamic framework)


We have built an awesome Flutter project, which has great functionality we want to export as a framework, just like native libraries do, so that the source code is hidden (convert to dynamic framework).

We have followed the instructions: https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps

which allows us to include Flutter project in a Host iOS app, initializing FlutterEngine and use of FlutterViewController.

The question is, how do we create a dynamic framework, let's say SomeProductSDK.framework, which will expose a public methods to create our SomeProductSDK related modal screens?

// In any app
import SomeProductSDK

let controller = TransactionViewController() // SomeProductSDK.framework with partial implementation with flutter
self.present(controller, animated: true)

Solution

  • I've partially achieved what you want. All of this is very experimental and overall a bad idea for production ready SDK. But... it's possible.

    1. Create flutter app as usual and run it once on iOS simulator.
    2. Open Xcode workspace and add new framework. For my purposes I will name it RunnerLib.
    3. Change deployment target of that framework to be the same as for Runner. Also disable bitcode.
    4. Change target membership of App.framework and Flutter.framework to RunnerLib.
    5. Create Launcher class with one static method: + (void)launchFrom:(UIViewController *)parent, this what should create a FlutterViewController and present it.
    6. Rewrite the Runner to use Launcher class. Replace FlutterAppDelegate with standard AppDelegate, make ViewController, etc. It should look like standard native iOS project, so you could create one and copy over AppDelegate, storyboard and ViewController.
    7. Call launchFrom method in your view controller, in viewDidAppear or as IBAction on a button.
    8. You should be able to build the Runner and see that flutter screen appears.
    9. Now, if you build the Runner app, you can open the crated Runner.app, and see that Frameworks directory contains 3 frameworks: App, Flutter and Runner.
    10. You will need to have two sets of frameworks: one set for simulator, compiled in debug mode, and another set for devices - archived. Getting debug frameworks is pretty easy, just compile from Xcode and inspect the product. Archived frameworks are harder, I recommend doing xcodebuild archive with disabled signing.
    11. Your users will have to configure their project to use correct frameworks depending on device. Possibly this step can be automated by Carthage and fat binaries, but I'm not sure. The problem is with App.framework which looks completely different on device than on simulator.

    Source code: https://github.com/szotp/runner_lib