'How to replace the bundled Dart SDK in Flutter to run natively on Apple Silicon (ARM64)?
Dart SDK officially supports ARM64 and as of now, 2.14.2 is the latest (stable) Dart SDK that has support for ARM64. Though it was the same version that was bundled in my Flutter setup, it seemed to run on Intel architecture (Activity monitor shows dart processes running on Intel).
I manually tried replacing the dart SDK on my flutter installation bu replacing flutter-directory/bin/cache/dart-sdk/
with the contents of a zip file of the Dart SDK made for ARM64, downloaded from dart.dev archive. But trying to run an app on an Android emulator (which runs on ARM64 and was working on my old Flutter setup), throws this error:
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.
Is there any other way to do a completely ARM64 Flutter setup on M1 devices?
Flutter version 2.5.1
Dart version 2.14.2
Device: MacBook Air (M1, 2020)
Solution 1:[1]
Update: Starting with Flutter 3.0, Flutter ships with arm64 dart sdk, so all you have to do is update to the latest version
Old answer:
- Download dart sdk directly from dart.dev: https://dart.dev/get-dart/archive -
once you extract it, you will see a
dart-sdk
folder - Go to your flutter installation root, and enter this path:
flutter-sdk-root/bin/cache
- Replace
dart-sdk
from the previous step with the one you've downloaded
Now, this part is hacky, so I can't really guarantee it will always work.
If you try to run the app now, dart compiler will crash.
Using flutter run -v
(which enables verbose mode, for more logs) and thanks to a couple of google lookups I was able to figure out that the problem is caused by a snapshot file called frontend_server.dart.snapshot
which is located in 2 places in the sdk:
flutter/bin/cache/dart-sdk/bin/snapshots
- new snapshot that targets arm, you've just pasted it hereflutter/bin/cache/artifacts/engine/darwin-x64
- old snapshot still forx64
- Copy a file called
frontend_server.dart.snapshot
from the first path mentioned above to the second path, replacing the old file
I've run a very casual test to give me a feeling if there's any improvement in compile time of a hello world app. By casual I mean I didn't close any programs that were in the background.
The process of the test was like following:
- run the app on ios simulator
- kill the app and run flutter clean
- run the app again and note down the build time
And the initial results are pretty promising:
- ~17s - 16" mbp i9
- ~16s - air m1 via rosetta
- ~12s - air m1 native
- ~11s - 14" mbp m1 max native
$ 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)
Solution 2:[2]
Update: As of Flutter 3.0.0, the arm64 dart sdk is used by default. The patcher can still be used for older versions if necessary.
Using the other answers here, I created flutter_m1_patcher.
It gets the version of Flutter's bundled Dart SDK and replaces it with the arm64 version.
Solution 3:[3]
It seems it can't be used with Flutter yet, as seen in:
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
[Announcing Dart 2.14][ScreenShot]: https://i.stack.imgur.com/N8Qcc.png
And:
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.
[Get the Dart SDK][ScreenShot]: https://i.stack.imgur.com/rawJV.png
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Lothurius Mobile Developer |