Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 820
Almas Adilbek Avatar asked Aug 29 '19 12:08

Almas Adilbek


2 Answers

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

like image 158
szotp Avatar answered Sep 27 '22 19:09

szotp


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.

like image 31
Son of Stackoverflow Avatar answered Sep 27 '22 19:09

Son of Stackoverflow