Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 608
Ashwin Avatar asked Sep 21 '21 18:09

Ashwin


People also ask

Do I need Dart sdk for Flutter?

If you're developing Flutter apps, then you don't need to separately download the Dart SDK; just install Flutter. To learn about other tools you can use for Dart development, see the Dart tools page.

Does Flutter sdk include Dart sdk?

Note: The Flutter SDK includes the full Dart SDK, and has Dart's dart command-line interface in its bin folder. Dart tools may send usage metrics and crash reports to Google.

Which version of Dart do I download with flutter?

Downloading the Flutter SDK also downloads the compatible version of Dart, but if you’ve downloaded the Dart SDK separately, make sure that the Flutter version of dart is first in your path, as the two versions might not be compatible.

What is the current version of the Dart SDK?

The current Dart SDK version is 2.10.4. how can I change the version into a upper version .? In a beta or dev channel How to change the current Dart SDK version? How to replace the bundled Dart SDK in Flutter to run natively on Apple Silicon (ARM64)?

Why can't I find the Dart folder in my system?

Not sure about dart is in bin/dart so take a look at dart directory to see if there is no system files in that if not then delete it and delete whole flutter folder that's all make sure .zshrc is also deleted there's no path given, restart once and install flutter again

What is the latest version of flutter?

At March 4 2021, Flutter 2.0 is release as stable version. But if you have still have old projects that haven’t support yet for Flutter SDK v2.0+ and you also have some project that you want to try with Fluter SDK 2.0+. You may need to switch between Flutter SDK v1.+ and v2.0+.


3 Answers

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

like image 74
eeqk Avatar answered Oct 20 '22 00:10

eeqk


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.

like image 31
Rexios Avatar answered Oct 19 '22 23:10

Rexios


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

like image 4
Rogério Pires Avatar answered Oct 19 '22 22:10

Rogério Pires