flutter tools调试与flutter build调用dart-sdk

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

相关推荐
Jewel1053 小时前
Flutter代码混淆
android·flutter·ios
一头小火烧12 小时前
flutter打包签名问题
flutter
sunly_12 小时前
Flutter:异步多线程结合
flutter
AiFlutter12 小时前
Flutter网络通信-封装Dio
flutter
B.-12 小时前
Flutter 应用在真机上调试的流程
android·flutter·ios·xcode·android-studio
有趣的杰克12 小时前
Flutter【04】高性能表单架构设计
android·flutter·dart
sunly_1 天前
Flutter:父组件,向子组件传值,子组件向二级页面传值
flutter
爱学习的绿叶1 天前
flutter TabBarView 动态添加删除页面
flutter
趴菜小玩家1 天前
使用 Gradle 插件优化 Flutter Android 插件开发中的 Flutter 依赖缺失问题
android·flutter·gradle
jhonjson2 天前
Flutter开发之flutter_local_notifications
flutter·macos·cocoa