18

I can't find any concrete resources on this, does Dart get compiled to JVM, or did the Google's team compile the Dart VM to be run on the JVM and then run Dart inside Dart VM inside JVM?

The former makes more sense and it goes inline with the "no bridge" mantra, but the latter seems more inline with how integration between native & flutter code looks like

bbozo
  • 7,075
  • 3
  • 30
  • 56

2 Answers2

24

Dart is compiled to native machine code (ARM, Intel, ...) executable and bundled with some native platform code (Java, Kotlin, Objective-C/Swift) to interact with the native platform.

See also

How does Flutter run my code on Android? The engine’s C and C++ code

are compiled with Android’s NDK. The Dart code (both the SDK’s and yours) are ahead-of-time (AOT) compiled into a native, ARM library. That library is included in a “runner” Android project, and the whole thing is built into an APK. When launched, the app loads the Flutter library. Any rendering, input or event handling, and so on, are delegated to the compiled Flutter and app code. This is similar to the way many game engines work.

Debug mode builds use a virtual machine (VM) to run Dart code (hence the “debug” banner they show to remind people that they’re slightly slower) in order to enable stateful hot reload.

How does Flutter run my code on iOS? The engine’s C and C++ code are

compiled with LLVM. The Dart code (both the SDK’s and yours) are ahead-of-time (AOT) compiled into a native, ARM library. That library is included in a “runner” iOS project, and the whole thing is built into an .ipa. When launched, the app loads the Flutter library. Any rendering, input or event handling, and so on, are delegated to the compiled Flutter and app code. This is similar to the way many game engines work.

Debug mode builds use a virtual machine (VM) to run Dart code (hence the “debug” banner they show to remind people that they’re slightly slower) in order to enable stateful hot reload.

https://flutter.io/docs/resources/faq#how-does-flutter-run-my-code-on-android

See also https://proandroiddev.com/flutters-compilation-patterns-24e139d14177

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Its mentioned here that dart code is compiled into a native ARM library. Why is it called a library, and not an application? And can it be compiled into non-ARM libraries? Surely ARM isn't the only chip for android? – Ben Butterworth May 26 '20 at 08:33
  • 2
    @BenB I guess it is because an Android application is not just an exe file. It's an APK and consists of several components. https://flutter.dev/docs/resources/faq#run-android might provide more insight. – Günter Zöchbauer May 26 '20 at 08:42
  • Thanks for your reply, I only see it now I think. Unless I forgot all about it – Ben Butterworth Sep 13 '21 at 19:56
  • But here - https://dart.dev/overview#runtime it was told that "On native platforms, the Dart runtime is automatically included inside self-contained executables, and is part of the Dart VM provided by the dart run command.". So it looks like it uses VM even in production build. – user2134488 Dec 29 '21 at 08:11
  • @user2134488 Runtime doesn't mean VM. Runtime is for example garbage collector. The VM is only used when the code is interpreted (during development), when it's compiled to binary, no VM is used. – Günter Zöchbauer Dec 29 '21 at 14:40
  • @GünterZöchbauer So would it be somewhat accurate to say that in an AOT compiled app, Dart runs on the native device, in a Dart runtime, for the whole app lifecycle? One task of this dart runtime is to essentially walk the widget tree and produce a Layer tree, which is sort of at the edge of the Dart runtime code that gets handed over to the native C/C++ code in the Flutter engine, to handle rendering that to the screen? And I would probably assume from that, that all of the Dart developer code for an app is not compiled to C/C++ but is contained in the Dart runtime? – BeniaminoBaggins Jul 05 '23 at 08:13
  • 1
    @BeniaminoBaggins With AOT there is no Dart involved after compiling. The Dart runtime is, like in Go, just very basic stuff like garbage collection. Walking the tree and the rest of what you mentioned is part of the Flutter framework. Everything is compiled to native machine code (similar to Go, C++, Rust, ...) **before** (Ahead Of Time) it is packaged and installed on the device. Not sure what you mean with "Dart developer code". During development Dart is executed on a virtual machine somewhat similar to Java for quicker change-reload cycles (only compile what changed/hot reload) – Günter Zöchbauer Jul 05 '23 at 10:09
  • 1
    @BeniaminoBaggins There is no difference between Flutter Dart code and developer Dart code. Flutter brings in some additional C, C++, Java, Kotlin, Swift, ObjectivC libraries that are compiled independently by different compilers, but it all ends up as native machine code. The difference you are talking about does not really exist this way. The relevant difference (release and development mode) is about whether the compilation to machine code happens ahead of time (AOT - before deployment to the device) or just in time (JIT - just before the code is executed on the device). ... – Günter Zöchbauer Jul 05 '23 at 14:58
  • 1
    @BeniaminoBaggins ... After the compilation step it's always the device that does the work. – Günter Zöchbauer Jul 05 '23 at 14:58
4

Sometimes you find the answer immediately after you ask it -_- Found this reddit answer

Both!

When developing, Flutter uses the VM so you can get nice things such hot reloading.But for production it compiles down (AOT) to a native ARM library then uses NDK on Android and LLVM on iOS to embed on native apps (runners).

That is why you get a debug/slow mode banner on the top-right corner, to remember you that, you are using the VM.

Check https://flutter.io/faq/#technology

Also https://www.youtube.com/watch?v=FUxV4MIhS3g

P.S. This doesn't mean that Dart VM isn't suitable for production environments, you can still use it on server-side or long-running tasks, just like JVM, CRL, Node.js etc. I'm personally using it for a HTTP API and really enjoying it.

bbozo
  • 7,075
  • 3
  • 30
  • 56