之前写了一篇关于如何将 flutter 直接打包成 android aar 的文章, 本篇写一写如何将 flutter 打包成 framework 以便于直接让没有 flutter 环境的 iOS 开发者使用, 因为国内很多项目都有这样的要求
本篇并不会做完全的工程集成化, 只是做一下如何将 flutter 的 framework 打出来, 并且置入到 iOS 原生工程中, 因为各项目一定户会有自己的特殊性, 不可能完全一样
本篇打包脚本部分参考了 https://www.jianshu.com/p/700bd7d2122b 的内容,但是又有一些针对 flutter 版本的变化和 flutter type 不同的情况进行的修改, 不观看连接中的内容并不会影响观看
文章目录
开发环境
MacOS
XCode 10
git
flutter 及 flutter 的相关工具链
cocoapods
创建几个工程
iOS 原生工程
使用 xcode 创建
这个原生工程就是模拟你的原有工程
Flutter 工程
这里我只使用 flutter module 的方式, 如果你 flutter 是 app 的方式创建的,则打包脚本的内容需要根据应用结构有所调整
$ flutter create -t module flutter_module_for_ios
这次直接在里面添加一个带有原生功能的插件, 和 android 篇相同依然选择 shared_preferences 那个插件
修改 pubspec.yaml:
dependencies:
shared_preferences: ^0.5.3+1
创建脚本
观察结构
打包 iOS,然后观察 build 文件夹
flutter packages get
flutter build ios --release --no-codesign
tree build/ios/Release-iphoneos
build/ios/Release-iphoneos
├── FlutterPluginRegistrant
│ └── libFlutterPluginRegistrant.a
├── Runner.app
│ ├── AppIcon20x20@2x.png
│ ├── AppIcon20x20@2x~ipad.png
│ ├── AppIcon20x20@3x.png
│ ├── AppIcon20x20~ipad.png
│ ├── AppIcon29x29.png
│ ├── AppIcon29x29@2x.png
│ ├── AppIcon29x29@2x~ipad.png
│ ├── AppIcon29x29@3x.png
│ ├── AppIcon29x29~ipad.png
│ ├── AppIcon40x40@2x.png
│ ├── AppIcon40x40@2x~ipad.png
│ ├── AppIcon40x40@3x.png
│ ├── AppIcon40x40~ipad.png
│ ├── AppIcon60x60@2x.png
│ ├── AppIcon60x60@3x.png
│ ├── AppIcon76x76@2x~ipad.png
│ ├── AppIcon76x76~ipad.png
│ ├── AppIcon83.5x83.5@2x~ipad.png
│ ├── Assets.car
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboardc
│ │ │ ├── 01J-lp-oVM-view-Ze5-6b-2t3.nib
│ │ │ ├── Info.plist
│ │ │ └── UIViewController-01J-lp-oVM.nib
│ │ └── Main.storyboardc
│ │ ├── BYZ-38-t0r-view-8bC-Xf-vdC.nib
│ │ ├── Info.plist
│ │ └── UIViewController-BYZ-38-t0r.nib
│ ├── Debug.xcconfig
│ ├── Flutter.xcconfig
│ ├── Frameworks
│ │ ├── App.framework
│ │ │ ├── App
│ │ │ ├── Info.plist
│ │ │ └── flutter_assets
│ │ │ ├── AssetManifest.json
│ │ │ ├── FontManifest.json
│ │ │ ├── LICENSE
│ │ │ ├── fonts
│ │ │ │ └── MaterialIcons-Regular.ttf
│ │ │ └── packages
│ │ │ └── cupertino_icons
│ │ │ └── assets
│ │ │ └── CupertinoIcons.ttf
│ │ └── Flutter.framework
│ │ ├── Flutter
│ │ ├── Info.plist
│ │ └── icudtl.dat
│ ├── Info.plist
│ ├── PkgInfo
│ ├── Release.xcconfig
│ └── Runner
├── Runner.app.dSYM
│ └── Contents
│ ├── Info.plist
│ └── Resources
│ └── DWARF
│ └── Runner
├── libPods-Runner.a
└── shared_preferences
└── libshared_preferences.a
18 directories, 46 files
发现打包出来的是.a 文件, 这里我们需要修改一下 ios 目录下的文件, 以便于打包出来 framework 文件, 因为 framework 是 apple 提供的一种打包方案, 直接将所有需要的资源包括头文件,库文件都聚集到了一起,方便引用, 而.a 文件就不一样了, 还需要包含对应的头文件
修改.ios 下的 podfile
在第一行添加这个
use_frameworks!
然后先清除一下刚刚打包的内容 $ flutter clean
flutter build ios --release --no-codesign
tree build/ios/Release-iphoneos
build/ios/Release-iphoneos
├── FlutterPluginRegistrant
│ ├── FlutterPluginRegistrant.framework
│ │ ├── FlutterPluginRegistrant
│ │ ├── Headers
│ │ │ ├── FlutterPluginRegistrant-umbrella.h
│ │ │ └── GeneratedPluginRegistrant.h
│ │ ├── Info.plist
│ │ └── Modules
│ │ └── module.modulemap
│ └── FlutterPluginRegistrant.framework.dSYM
│ └── Contents
│ ├── Info.plist
│ └── Resources
│ └── DWARF
│ └── FlutterPluginRegistrant
├── Pods_Runner.framework
│ ├── Headers
│ │ └── Pods-Runner-umbrella.h
│ ├── Info.plist
│ ├── Modules
│ │ └── module.modulemap
│ └── Pods_Runner
├── Runner.app
│ ├── AppIcon20x20@2x.png
│ ├── AppIcon20x20@2x~ipad.png
│ ├── AppIcon20x20@3x.png
│ ├── AppIcon20x20~ipad.png
│ ├── AppIcon29x29.png
│ ├── AppIcon29x29@2x.png
│ ├── AppIcon29x29@2x~ipad.png
│ ├── AppIcon29x29@3x.png
│ ├── AppIcon29x29~ipad.png
│ ├── AppIcon40x40@2x.png
│ ├── AppIcon40x40@2x~ipad.png
│ ├── AppIcon40x40@3x.png
│ ├── AppIcon40x40~ipad.png
│ ├── AppIcon60x60@2x.png
│ ├── AppIcon60x60@3x.png
│ ├── AppIcon76x76@2x~ipad.png
│ ├── AppIcon76x76~ipad.png
│ ├── AppIcon83.5x83.5@2x~ipad.png
│ ├── Assets.car
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboardc
│ │ │ ├── 01J-lp-oVM-view-Ze5-6b-2t3.nib
│ │ │ ├── Info.plist
│ │ │ └── UIViewController-01J-lp-oVM.nib
│ │ └── Main.storyboardc
│ │ ├── BYZ-38-t0r-view-8bC-Xf-vdC.nib
│ │ ├── Info.plist
│ │ └── UIViewController-BYZ-38-t0r.nib
│ ├── Debug.xcconfig
│ ├── Flutter.xcconfig
│ ├── Frameworks
│ │ ├── App.framework
│ │ │ ├── App
│ │ │ ├── Info.plist
│ │ │ └── flutter_assets
│ │ │ ├── AssetManifest.json
│ │ │ ├── FontManifest.json
│ │ │ ├── LICENSE
│ │ │ ├── fonts
│ │ │ │ └── MaterialIcons-Regular.ttf
│ │ │ └── packages
│ │ │ └── cupertino_icons
│ │ │ └── assets
│ │ │ └── CupertinoIcons.ttf
│ │ ├── Flutter.framework
│ │ │ ├── Flutter
│ │ │ ├── Info.plist
│ │ │ └── icudtl.dat
│ │ ├── FlutterPluginRegistrant.framework
│ │ │ ├── FlutterPluginRegistrant
│ │ │ └── Info.plist
│ │ └── shared_preferences.framework
│ │ ├── Info.plist
│ │ └── shared_preferences
│ ├── Info.plist
│ ├── PkgInfo
│ ├── Release.xcconfig
│ └── Runner
├── Runner.app.dSYM
│ └── Contents
│ ├── Info.plist
│ └── Resources
│ └── DWARF
│ └── Runner
└── shared_preferences
├── shared_preferences.framework
│ ├── Headers
│ │ ├── SharedPreferencesPlugin.h
│ │ └── shared_preferences-umbrella.h
│ ├── Info.plist
│ ├── Modules
│ │ └── module.modulemap
│ └── shared_preferences
└── shared_preferences.framework.dSYM
└── Contents
├── Info.plist
└── Resources
└── DWARF
└── shared_preferences
37 directories, 65 files
Runner.app 这个熟悉的名字下虽然看似包含所有的 framework,但是仔细观察, 这些 framework 内不包含头文件, 所以其实是用不了的
可以使用的是如图所示的几个
根据上面的 tree 图可以看到,其中包含了全部的库文件, 但除此以外,