flutter 命令分析
- dart flutter_tools.snapshot run
- dart frontend_server.dart.snapshot --output-dill app.dill
flutter 命令
flutter attach
flutter run
转化为 dart flutter_tools.snapshot attach
dart flutter_tools.snapshot run
这是我们平时用到的flutter 运行和attach命令。
flutter tools 调试
打开android studio,新建一个command line app 目标程序
新建内容如下图展示:
点击android studio debug 按钮,就可以运行flutter tools命令,以源码的方式执行flutter
源码 compile.dart
/Volumes/huc/opt/fvm/versions/3.0.5/packages/flutter_tools/lib/src/compile.dart
这个文件定义一个类,实现flutter编译功能,类名如下
java
class DefaultResidentCompiler implements ResidentCompiler
在该类方法打上断点:
javascript
Future<CompilerOutput?> _compile(
String scriptUri,
String? outputPath,
{String? additionalSource}
) async
编译函数:
dart
final List<String> command = <String>[
_artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
'--disable-dart-dev',
frontendServer,
'--sdk-root',
sdkRoot,
'--incremental',
if (testCompilation)
'--no-print-incremental-dependencies',
'--target=$targetModel',
// TODO(zanderso): remove once this becomes the default behavior
// in the frontend_server.
// https://github.com/flutter/flutter/issues/52693
'--debugger-module-names',
// TODO(annagrin): remove once this becomes the default behavior
// in the frontend_server.
// https://github.com/flutter/flutter/issues/59902
'--experimental-emit-debug-metadata',
for (final Object dartDefine in dartDefines)
'-D$dartDefine',
if (outputPath != null) ...<String>[
'--output-dill',
outputPath,
],
if (librariesSpec != null) ...<String>[
'--libraries-spec',
librariesSpec!,
],
if (packagesPath != null) ...<String>[
'--packages',
packagesPath!,
],
...buildModeOptions(buildMode, dartDefines),
if (trackWidgetCreation) '--track-widget-creation',
if (fileSystemRoots != null)
for (final String root in fileSystemRoots) ...<String>[
'--filesystem-root',
root,
],
if (fileSystemScheme != null) ...<String>[
'--filesystem-scheme',
fileSystemScheme!,
],
if (initializeFromDill != null) ...<String>[
'--initialize-from-dill',
initializeFromDill!,
],
if (assumeInitializeFromDillUpToDate) '--assume-initialize-from-dill-up-to-date',
if (additionalSource != null) ...<String>[
'--source',
additionalSource,
'--source',
'package:flutter/src/dart_plugin_registrant.dart',
'-Dflutter.dart_plugin_registrant=${Uri.file(additionalSource)}',
],
if (platformDill != null) ...<String>[
'--platform',
platformDill!,
],
if (aopConfig) ...<String>[
'--aop',
'1',
],
if (unsafePackageSerialization == true) '--unsafe-package-serialization',
...?extraFrontEndOptions,
];
_logger.printTrace(command.join(' '));
print(command.join(' '));
_server = await _processManager.start(command);
添加 command打印,输出如下:
bash
/Volumes/huc/opt/fvm/current/bin/cache/dart-sdk/bin/dart
--disable-dart-dev
/Volumes/huc/opt/fvm/current/bin/cache/dart-sdk/bin/snapshots/frontend_server.dart.snapshot
--sdk-root /Volumes/huc/opt/fvm/current/bin/cache/artifacts/engine/common/flutter_patched_sdk/
--incremental
--target=flutter
--debugger-module-names
--experimental-emit-debug-metadata
-DFLUTTER_WEB_AUTO_DETECT=true
--output-dill
/var/folders/4j/z1n6phgx4d11klg60p7d3b240000gn/T/flutter_tools.lcUU8k/flutter_tool.ZQKt3c/app.dill
--packages
/Users/axx/Desktop/test_app/.dart_tool/package_config.json
-Ddart.vm.profile=false -Ddart.vm.product=false
--enable-asserts
--track-widget-creation
--filesystem-scheme org-dartlang-root
--initialize-from-dill
build/c075001b96339384a97db4862b8ab8db.cache.dill.track.dill
--source /Users/axx/Desktop/test_app/.dart_tool/flutter_build/dart_plugin_registrant.dart
--source package:flutter/src/dart_plugin_registrant.dart
-Dflutter.dart_plugin_registrant=file:///Users/axx/Desktop/test_app/.dart_tool/flutter_build/dart_plugin_registrant.dart
--flutter-widget-cache
--enable-experiment=alternative-invalidation-strategy
这个简化为:
css
dart frontend_server.dart.snapshot --output-dill app.dill
总结
通过flutter tools源码的调试,总结了flutter build执行主要步骤如下:
-
flutter命令执行shell脚本,调用flutter tools flutter tools 编译版在 bin/cache下面,
/Volumes/huc/opt/fvm/versions/3.0.5/bin/cache/flutter_tools.snapshot
这个是首次flutter调用即时编译的执行版本。 -
flutter tools调用bin/cache/dart-sdk下面的dart sdk 相关程序:frontend_server.dart.snapshot 等
/Volumes/huc/opt/fvm/versions/3.0.5/bin/cache/dart-sdk/bin/snapshots/frontend_server.dart.snapshot