21

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)

Ashwin
  • 353
  • 4
  • 11

3 Answers3

37

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:

  1. Download dart sdk directly from dart.dev: https://dart.dev/get-dart/archive - once you extract it, you will see a dart-sdk folder
  2. Go to your flutter installation root, and enter this path: flutter-sdk-root/bin/cache
  3. 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 here
  • flutter/bin/cache/artifacts/engine/darwin-x64 - old snapshot still for x64
  1. 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)

enter image description here

eeqk
  • 3,492
  • 1
  • 15
  • 22
  • It really works. Should be accepted as the right answer. Thank you so much. – Rogério Pires Oct 29 '21 at 14:58
  • Works for me too on M1 Max – RobDil Nov 03 '21 at 21:37
  • It works on my machine! Thank you – Darari Nur Amali Dec 04 '21 at 09:53
  • This is a hacky way of letting the installed (x64) dart use something from the native ARM64 dart. Hopefully Google will provide the ARM64 package for apple silicon. What if you upgraded the flutter, would the ARM64 dart be updated to the (x64) dart? – dotslash Dec 12 '21 at 00:49
  • Didn't work for me. My Android studio stopped working and give me error reporting corrupted flutter executables – Jay Mann Dec 13 '21 at 08:34
  • 3
    it works with flutter 2.8 and dart 2.15 – ch13mob Dec 13 '21 at 16:33
  • 1
    After replacing files, I got `Can't load Kernel binary: Invalid SDK hash.` – Chris Maverick Dec 17 '21 at 03:42
  • @ChrisMaverick could you share steps to reproduce? I can confirm that it still works on latest beta release and even master channel. Since your error is related to `SDK hash` it would make sense if you had a wrong version of Dart SDK - please make sure you have a version corresponding to the Flutter SDK. Run `dart --version` before replacing the SDK/snapshots to find you which one you have to download. For example Flutter `2.9.0-0.1.pre` requires Dart `2.16.0-80.1.beta`. – eeqk Dec 17 '21 at 08:41
  • today I installed flutter with its the latest update from the website and when I run `dart --version` I get `Dart SDK version: 2.16.2 (stable) (Tue Mar 22 13:15:13 2022 +0100) on "macos_x64"` you updated your answer that since beta 2.12.0 it comes by default, Here I'm on 2.16 stable and it's not by defualt – Shalabyer May 08 '22 at 17:05
  • 1
    @Shalabyer you have to use beta channel as this architecture was not released yet to stable, enable it with `flutter channel beta`. Also, make sure your terminal is not running through rosetta, you can check that using `arch` command which should output `arm64` – eeqk May 09 '22 at 07:30
7

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.

Rexios
  • 532
  • 6
  • 11
  • Thank you, that makes life easier! The package does not support different Flutter versions installed via `fvm` - to update the different versions, I switch the fvm default by typing `fvm global`, selecting the version and then running `flutterpatch` command. – RobDil Jan 08 '22 at 00:33
  • From version 1.5 it is now possible to set path to Flutter using `-p`option instead. Thanks for the update! :) – RobDil Jan 28 '22 at 06:19
4

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.

https://dart.dev/get-dart

[Get the Dart SDK][ScreenShot]: https://i.stack.imgur.com/rawJV.png