官方文档
将 Flutter module 集成到 iOS 项目
https://flutter.cn/docs/development/add-to-app/ios/project-setup
- 1、flutter pub get
->Flutter.framework - 2、pod install
podhelper.rb->.ios下:Flutter.framework、 App.framework - 3、xcodebuild
xcode_backend.sh->Debug-iphoneos目录下:App.framework、
.ios下Flutter.framework
1、flutter pub get
(1)生成.ios
(1)这时候还没有App.framework , podspec文件是有了
(2)有engine,Flutter.framework。
(3)有插件列表 podspec FlutterPluginRegistrant.podspec,这时没有symlinks/plugins目录软链接
(4)导出当前的环境变量 flutter_export_environment.sh
(2)生成依赖列表
flutter-plugins-dependencies
2、pod install
执行 podHelper.rb 脚本
做2件事情:
(1)、 把这3个pod 引进native
Flutter.framework Flutter.podspec
插件注册 FlutterPluginRegistrant.podspec
plugins下面的所有用到的插件
App.framework flutter_commercial.podspec
安装Flutter.framework
拷贝ios-release版本
/Volumes/huc/opt/fvm/versions/2.2.0/bin/cache/artifacts/engine/ios-release安装插件
从flutter-plugins-dependencies列表,把依赖的插件从
/opt/fvm/versions/2.2.0/.pub-cache
拷贝一个快捷链接目录出来。
然后pod依赖用本地path依赖。
plugins_file = File.expand_path('.flutter-plugins-dependencies', flutter_application_path)
plugin_pods = flutter_parse_dependencies_file_for_ios_plugin(plugins_file)
plugin_pods.each do |plugin_hash|
plugin_name = plugin_hash['name']
plugin_path = plugin_hash['path']
if (plugin_name && plugin_path)
symlink = File.join(symlinks_dir, plugin_name)
FileUtils.rm_f(symlink)
File.symlink(plugin_path, symlink)
pod plugin_name, :path => File.join(symlink, 'ios'), :inhibit_warnings => true
end
end
- 安装App.framework
生成一个默认的App.framework 16Kb 一行代码用来占位置
(2)、把一段脚本写进xcode 工程配置.xcodeproj
/Volumes/huc/opt/fvm/versions/2.2.0/packages/flutter_tools/bin/xcode_backend.sh build
flutter_export_environment_path = File.join('${SRCROOT}', relative, 'flutter_export_environment.sh');
script_phase :name => 'Run Flutter Build flutter_commercial Script',
:script => "set -e\nset -u\nsource \"#{flutter_export_environment_path}\"\nexport VERBOSE_SCRIPT_LOGGING=1 && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/xcode_backend.sh build",
:execution_position => :before_compile
end
多了.symlinks 和App.framework,重新拷贝了Flutter.xcframework
(3) pod install生成
Pods-HouseCommercialCube-frameworks.sh
install_framework "${PODS_ROOT}/../../flutter_commercial/.ios/Flutter/App.framework"
install_framework "${PODS_XCFRAMEWORKS_BUILD_DIR}/Flutter/Flutter.framework"
3、run app
执行 xcode_backend.sh
/Volumes/huc/opt/fvm/versions/2.2.0/packages/flutter_tools/bin/xcode_backend.sh
做3件事情:
(1) 不太懂
AppFrameworkInfo.plist
if FLTAssetsPath=$(/usr/libexec/PlistBuddy -c "Print :FLTAssetsPath" "${derived_dir}/AppFrameworkInfo.plist" 2>/dev/null); then
if [[ -n "$FLTAssetsPath" ]]; then
assets_path="${FLTAssetsPath}"
fi
assets_path 这个后面也没有人用啊!
(2)通过环境 CONFIGURATION 重新 拿到对应 debug/release/profile版本的 engine: Flutter.framework
放到.ios下面
local derived_dir="${SOURCE_ROOT}/Flutter"
if [[ -e "${project_path}/.ios" ]]; then
derived_dir="${project_path}/.ios/Flutter"
fi
local framework_path="${FLUTTER_ROOT}/bin/cache/artifacts/engine/${artifact_variant}"
local flutter_engine_flag=""
local local_engine_flag=""
local flutter_framework="${framework_path}/Flutter.xcframework"
# TODO(jmagman): use assemble copied engine in add-to-app.
if [[ -e "${project_path}/.ios" ]]; then
RunCommand rm -rf -- "${derived_dir}/engine/Flutter.framework"
RunCommand cp -r -- "${flutter_framework}" "${derived_dir}/engine"
fi
.ios/Flutter/engine/Flutter.xcframework
编译前, 把这个下面添加一个空文件, engine被覆盖了,那么那个空文件就没了?
对比了一下大小。 release
debug 版本的Flutter.xcframework 255M
ios-release 1.03 GB
一开始是1.03G, run之后, 变成了255M
说明确实拷贝到这里了
(2) 编译App.framework 放到 BUILT_PRODUCTS_DIR 下面
.ios下面App.framework 没有变
RunCommand "${FLUTTER_ROOT}/bin/flutter" \
${verbose_flag} \
${flutter_engine_flag} \
${local_engine_flag} \
assemble \
--no-version-check \
--output="${BUILT_PRODUCTS_DIR}/" \
${performance_measurement_option} \
-dTargetPlatform=ios \
-dTargetFile="${target_path}" \
-dBuildMode=${build_mode} \
-dIosArchs="${ARCHS}" \
-dSdkRoot="${SDKROOT}" \
-dSplitDebugInfo="${SPLIT_DEBUG_INFO}" \
-dTreeShakeIcons="${TREE_SHAKE_ICONS}" \
-dTrackWidgetCreation="${TRACK_WIDGET_CREATION}" \
-dDartObfuscation="${DART_OBFUSCAT
App.framework 61Mb
Flutter.framework 35Mb
♦ rm
-rf --
/Volumes/huc/houseCommercial/flutter_commercial/
.ios/Flutter/engine/Flutter.framework
♦ cp -r -- /opt/fvm/versions/2.2.0
/bin/cache/artifacts/engine/ios/Flutter.xcframework /Volumes/huc/houseCommercial/flutter_commercial/
.ios/Flutter/engine
♦ /opt/fvm/versions/2.2.0/bin/flutter
--verbose assemble
--no-version-check
--output=/Users/huchu/Library/Developer/Xcode/DerivedData/HouseCommercialCube-haouxiieutwwizfrzseumgibdjxb/Build/Products/Debug-iphoneos/
-dTargetPlatform=ios
-dTargetFile=lib/main.dart
-dBuildMode=debug
-dIosArchs=arm64
-dSdkRoot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.0.sdk
-dSplitDebugInfo=
-dTreeShakeIcons=false
-dTrackWidgetCreation=false
-dDartObfuscation=false
-dEnableBitcode=
-dCodesignIdentity=FD1A800911570C03E014A5628E4B5F87C416FC06
--ExtraGenSnapshotOptions=
--DartDefines=
--ExtraFrontEndOptions=
debug_ios_bundle_flutter_assets
(3)嵌入式库
"${PODS_ROOT}/Target Support Files/Pods-HouseCommercialCube/Pods-HouseCommercialCube-frameworks.sh"
if [[ "$CONFIGURATION" == "Debug" ]]; then
///其他动态库
install_framework "${PODS_ROOT}/../../flutter_commercial/.ios/Flutter/App.framework"
install_framework "${PODS_XCFRAMEWORKS_BUILD_DIR}/Flutter/Flutter.framework"
fi
if [[ "$CONFIGURATION" == "Release" ]]; then
///其他动态库
install_framework "${PODS_ROOT}/../../flutter_commercial/.ios/Flutter/App.framework"
install_framework "${PODS_XCFRAMEWORKS_BUILD_DIR}/Flutter/Flutter.framework"
fi