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

1,525

Solution 1

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

Solution 2

In order to export the framework as a native library you'll have to ensure that the person who'll use your framework (user) has Flutter SDK installed onto his computer as your framework's code will surely be dependent on Flutter SDK by default. So what your trying to do is equivalent of having your own package on pub.dev.

If you find some way to make the framework you want to export - independent of Flutter's framework, only then exporting it as an framework would make sense. (Doing that should be possible beyond doubt, but see the amount of work and time you'll put in just to get your framework there. You could have built your own Flutter framework in those same efforts and time)

Since you have already made that awesome Flutter Project, consider uploading it as an package on https://pub.dev/ if you wish to.

Suggestion: You can ask the users who want to try your package to install Flutter. This way your hardwork won't go in vains.

Conclusion: It is not practically feasible to achieve what your trying to do.

Share:
1,525
Almas Adilbek
Author by

Almas Adilbek

Geek designer. I'm focused on quality products, where every single pixel is indispensable., Founder of Sajda.mobi & Kaz.News

Updated on December 01, 2022

Comments

  • Almas Adilbek
    Almas Adilbek over 1 year

    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)
    
  • Almas Adilbek
    Almas Adilbek over 4 years
    Thank you for answer. Actually, we are trying to distribute a commercial product as a SDK, which was built with flutter. SDK consumers are native apps or cross-platform apps does not matter. And, we want hide the source code. So making it public in pub.dev is not an options.
  • Almas Adilbek
    Almas Adilbek over 4 years
    Thanks, finally I got time to answer you. Most of the time flutter apps will have plugin dependencies, those plugins must be build as well. Flutter module uses pbhelper.rb sciprt file to do that. I did it, so I could build all deps, app.framework, flutter.framework and we get also FlutterPluginRegistrant.framework which refers to all dep pods. I've put all those frameworks into umbrella framework, which works fine for Simulator in another iOS project (just embedding it), then created aggregate target and put umbrella into it, as a result I could build for devices as well.
  • Almas Adilbek
    Almas Adilbek over 4 years
    I have another problem. In flutter module project if we have a swift based plugin and static framework plugin (for ex: firebase_messaging) we get build errors, that is because we can't build those static frameworks as dynamic (specifying !use_frameworks in Podfile). If i remove !use_frameworks, then we have header paths problem for swift based plugins. This is only the case for Flutter module project. Building in Flutter app project has no problem.
  • szotp
    szotp over 4 years
    Be careful with umbrella frameworks. They are not allowed in appstore. As for dependencies, they will be a problem for customer if they want to use the same library in their app. I would keep them minimal.
  • Almas Adilbek
    Almas Adilbek over 4 years
    Yeah, we are aware of umbrella framework drawbacks. Do you think the native Pod dependency and the plugin pod dependency can conflict with each other? Umbrella contains only flutter related plugin pods, not the one used in native apps. Or, do we miss something?
  • Almas Adilbek
    Almas Adilbek over 4 years
    hi there. Did I understand correct that using umbrella framework in project will not pass iTunes connect validation?
  • szotp
    szotp over 4 years
    Yes my app got rejected because of this.
  • DAMM108
    DAMM108 about 2 years
    Hey Guys , Awesome thread so were you were able to achieve native sdk using flutter and does it got approved ? @szotp