Search code examples
flutterarm64apple-m1dart

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

  • 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