Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Flutter uses its own DartVM for native apps

I've read that Dart uses its own DartVM. Thus Flutter can use JIT (for things like hot-reload) while developing your app and AOT to build the latter into a "native" app (.apk/.ipa).

According to the docs for both platforms (Android/iOS):

ahead-of-time (AOT) compiled into a native, ARM library

But how does Flutter use its own DartVM, for example, on Android, if the app already runs via Dalvik/ART? And does the VM is built into our .apk so it can be delivered to a real Android device?

If DartVM isn't used for the end-builds then what about the GC stuff? Flutter tends to create a lot of short-lived objects which is fine for DartVM's Generational GC but isn't so good for the ART's one.

like image 498
Nikolay Kulachenko Avatar asked Dec 23 '22 03:12

Nikolay Kulachenko


1 Answers

The Flutter engine uses the Dart VM in two distinct modes:

  • Running in debug mode at development time, the flutter run command watches the host filesystem for changes to Dart source, and when it detects such changes, quickly compiles the Dart source to kernel (intermediate) format. This is then synced to the engine running on the device via hot-reload, and executed in JIT mode (Android) or interpreted mode (iOS).
  • Running in profile or release mode, the Dart source is first compiled to kernel intermediate form, then run through gen_snapshot, which emits AOT ARM machine code, which is then linked into the final app. You can find a detailed explanation of AOT mode operation on our wiki.

Note that there's a thin set of platform bindings for Android written in Java that interface with the engine via JNI, but the core of Flutter's engine is written in C++ and built with the NDK, as such those bits aren't relying on Dalvik/ART.

like image 106
cbracken Avatar answered Jan 08 '23 04:01

cbracken