如何将 Flutter 中捆绑的 Dart SDK 替换为 Apple Silicon (ARM64) 上的原生 运行?

How to replace the bundled Dart SDK in Flutter to run natively on Apple Silicon (ARM64)?

Dart SDK 正式支持 ARM64,截至目前,2.14.2 是支持 ARM64 的最新(稳定)Dart SDK。尽管它与我的 Flutter 设置中捆绑的版本相同,但它似乎 运行 在 Intel 架构上(Activity 监视器显示 dart 进程 运行ning 在 Intel 上)。

我手动尝试替换我的 flutter 安装上的 dart SDK bu 替换 flutter-directory/bin/cache/dart-sdk/ 为从 dart.dev archive 下载的为 ARM64 制作的 Dart SDK 的 zip 文件的内容。但是尝试在 Android 模拟器上 运行 一个应用程序(在 ARM64 上 运行s 并且正在处理我的旧 Flutter 设置),抛出这个错误:

Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
lib/main.dart:1
Snapshot not compatible with the current VM configuration: the snapshot requires 'release no-code_comments
no-dwarf_stack_traces_mode lazy_async_stacks lazy_dispatchers 
use_bare_instructions no-dedup_instructions
no-"asserts" "use_field_guards" "use_osr" x64-sysv no-null-safety' but the VM has 'release no-code_comments no-
dwarf_stack_traces_mode lazy_async_stacks 
lazy_dispatchers use_bare_instructions no-dedup_instructions no-"asserts" "use_field_guards" "use_osr" arm64-sysv no-null-safety'
2
the Dart compiler exited unexpectedly.

有没有其他方法可以在 M1 设备上进行完整的 ARM64 Flutter 设置?

Flutter 版本 2.5.1

Dart 版本 2.14.2

设备:MacBook Air(M1,2020)

Flutter好像还不能用,见:

Apple Silicon support in the Dart SDK

[...] Note that the Dart SDK bundled in the Flutter SDK doesn’t have these improvements yet.

https://medium.com/dartlang/announcing-dart-2-14-b48b9bb2fb67

[宣布 Dart 2.14][屏幕截图]:https://i.stack.imgur.com/N8Qcc.png

并且:

Get the Dart SDK

[...] As of Flutter 1.21, the Flutter SDK includes the full Dart SDK. So if you have Flutter installed, you might not need to explicitly download the Dart SDK. Consider downloading the Dart SDK if any of the following are true:

  • You don’t use Flutter.
  • You use a pre-1.21 version of Flutter.
  • You want to reduce disk space requirements or download time, and your use case doesn’t require Flutter. For example, you might have a continuous integration (CI) setup that requires Dart but not Flutter.

https://dart.dev/get-dart

[获取 Dart SDK][屏幕截图]: https://i.stack.imgur.com/rawJV.png

更新:从Flutter 3.0开始,Flutter自带arm64 dart sdk,所以你只需要更新到最新版本



旧答案:

  1. 直接从dart.dev下载dart sdk: https://dart.dev/get-dart/archive - 解压后,您会看到一个 dart-sdk 文件夹
  2. 转到你的 flutter 安装根目录,然后输入此路径:flutter-sdk-root/bin/cache
  3. 将上一步的 dart-sdk 替换为您下载的

现在,这部分是 hacky,所以我不能真正保证它总是有效。

如果您现在尝试 运行 应用程序,dart 编译器将会崩溃。

使用 flutter run -v(它启用详细模式,以获得更多日志)并且由于几次 google 查找,我能够弄清楚问题是由一个名为 frontend_server.dart.snapshot 位于sdk中的2个地方:

  • flutter/bin/cache/dart-sdk/bin/snapshots - 针对 arm 的新快照,您刚刚将其粘贴到此处
  • flutter/bin/cache/artifacts/engine/darwin-x64 - x64
  • 的旧快照
  1. 从上面提到的第一个路径复制一个名为frontend_server.dart.snapshot的文件到第二个路径,替换旧文件

我已经 运行 进行了一次非常随意的测试,让我感觉 hello world 应用程序的编译时间是否有任何改进。随意我的意思是我没有关闭任何在后台运行的程序。

测试过程如下:

  • 运行 ios 模拟器上的应用程序
  • 终止应用程序并运行 flutter clean
  • 运行 再次应用并记下构建时间

初步结果非常有希望:

  • ~17s - 16" mbp i9
  • ~16s - 通过 rosetta 的空气 m1
  • ~12s - air m1 native
  • ~11s - 14" mbp m1 最大原生
$ dart --version
Dart SDK version: 2.15.0-116.0.dev (dev) (Thu Sep 16 09:47:01 2021 -0700) on "macos_arm64"

$ flutter --version
Flutter 2.6.0-11.0.pre • channel dev • https://github.com/flutter/flutter.git
Framework • revision 4b330ddbed (5 weeks ago) • 2021-09-16 17:29:58 -0700
Engine • revision 5b81c6d615
Tools • Dart 2.15.0 (build 2.15.0-116.0.dev)

更新:从Flutter 3.0.0开始,默认使用arm64 dart sdk。如有必要,补丁程序仍可用于旧版本。


使用此处的其他答案,我创建了 flutter_m1_patcher

获取Flutter自带的Dart SDK的版本,替换为arm64版本