1. flutter create -t app
创建app
在xcode工程中,添加了脚本:
/bin/sh "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
/bin/sh "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed_and_thin
验证一下是不是在创建app的命令这一步 就已经给xcode 工程添加了脚本的。
flutter create --template=app my_flutter_app
此命令执行后创建了一个app工程,打开ios目录下Runner.xcworkspace
可以看到名字为Run Script和Thin Binary的两个脚本自动添加到ios 工程中了。
现在使用flutter_tools源代码调试一下create命令执行过程
打开AndroidStudio 添加dart commond line命令行,如下图配置所示:

启动Debug flutter_tools执行:
case FlutterProjectType.app:
generatedFileCount += await generateApp(
<String>['app', 'app_test_widget'],
relativeDir,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: !creatingNewProject,
projectType: template,
);
继续跟进:
generatedCount += await renderMerged(
<String>[...templateNames, 'app_shared'],
directory,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
);
继续跟进:
return template.render(
directory,
context,
overwriteExisting: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
);
继续跟进:
_templateFilePaths.forEach((String relativeDestinationPath, String absoluteSourcePath) {
finalDestinationFile.createSync(recursive: true);
}
这里flutter_tools下面有193个模版文件

通过这些模版文件,完成了一个flutter app项目的创建:

2. flutter pub get
通过以上命令发现,在创建flutter app内,会自动调用一次flutter pub get
Future<int> generateApp(
List<String> templateNames,
Directory directory,
Map<String, Object> templateContext, {
bool overwrite = false,
bool pluginExampleApp = false,
bool printStatusWhenWriting = true,
bool generateMetadata = true,
FlutterProjectType projectType,
}) async {
// 创建工程文件,模版文件拷贝
generatedCount += await renderMerged(
<String>[...templateNames, 'app_shared'],
directory,
templateContext,
overwrite: overwrite,
printStatusWhenWriting: printStatusWhenWriting,
);
// 执行了 pub get
await pub.get(
context: PubContext.create,
directory: directory.path,
offline: boolArg('offline'),
generateSyntheticPackage: true,
);
}
拉取flutter pub包
如果有dart 文件代码的添加,pub 库的添加删除,都需要重新执行flutter pub get完成更新
如果只是修改dart文件里面代码内容,在xcode侧 编译代码,会重新编译最新版本的dart代码
3. pod install
在Podfile文件中插入
插入podhelper.rb
在修改dart文件和pub包数量后,执行flutter pub get 之后,需要重新执行pod install
调用podhelper.rb方法:
flutter_install_all_ios_pods __dir__
podhelper.rb 是一个用于 Flutter 的辅助脚本,用于在 iOS 平台上自动安装和配置 CocoaPods 依赖项。flutter_install_all_ios_pods 是其中的一个方法,它主要完成以下几个步骤:
- 确定 Flutter 引擎的根目录,并将其添加到 CocoaPods 的搜索路径中。
- 确定应用程序项目的 Podfile 文件,并在其中添加必要的依赖项,例如 Flutter 引擎、Flutter 插件和其他依赖项。
- 运行 CocoaPods 安装命令,自动下载并安装所有依赖项。
- 在应用程序项目的 Info.plist 文件中添加必要的配置,例如 Flutter 引擎的版本号和应用程序的唯一标识符。
dir 是一个 Ruby 内置方法,用于获取当前文件所在的目录路径。在 flutter_install_all_ios_pods 方法中,dir 用于确定 podfile_path,即 Podfile 文件所在的路径,以便后续的依赖项配置和安装。
flutter_additional_ios_build_settings(target)
flutter_additional_ios_build_settings 方法是 Flutter 的辅助脚本,用于向 Xcode 项目中添加自定义的构建设置。
该方法接受一个参数 target,表示需要添加自定义构建设置的 Xcode target。它主要完成以下几个步骤:
- 确定 Flutter 引擎的根目录,并将其添加到 Xcode 项目的搜索路径中。
- 在 xcconfig 文件中设置一系列自定义的构建设置,例如启用 bitcode、关闭代码混淆等。
- 将 xcconfig 文件的路径添加到 Xcode target 的 buildSettings 中,以便 Xcode 在构建时自动加载这些设置。
通过使用 flutter_additional_ios_build_settings 方法,开发人员可以在不修改 Xcode 项目的情况下,向项目添加自定义构建设置。这对于需要对 Xcode 构建进行细粒度控制的项目非常有用。
4. 在ios xcode侧运行工程
会执行xcode_backend.sh 脚本:
$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh脚本是用于构建和打包Flutter应用程序的脚本,它支持两个参数:build和embed_and_thin,用于指定构建和打包的方式。
当使用build参数调用xcode_backend.sh脚本时,它会执行标准的构建和打包流程,包括以下步骤:
- 生成Flutter引擎的代码
- 编译Flutter应用程序的Dart代码
- 打包Flutter应用程序的资源为一个App Bundle
- 处理Flutter应用程序的依赖关系
- 将生成的App Bundle嵌入到iOS应用程序中
而当使用embed_and_thin参数调用xcode_backend.sh脚本时,它会在标准构建和打包流程的基础上添加额外的步骤,包括以下步骤:
- 从App Bundle中提取Flutter引擎的动态库和应用程序二进制文件
- 将Flutter引擎的动态库和应用程序二进制文件嵌入到iOS应用程序中
- 将Flutter引擎的动态库和应用程序二进制文件压缩为一个"thinning"版本,以减小应用程序的大小和启动时间。
因此,使用embed_and_thin参数调用xcode_backend.sh脚本可以生成一个经过优化的、精简的iOS应用程序,它只包含用到的Flutter引擎动态库和应用程序二进制文件,减小了应用程序的大小和启动时间。但是,这个命令可能需要一些额外的构建时间,因为它需要提取、嵌入和压缩Flutter引擎的动态库和应用程序二进制文件。
网友评论