How does Dart/Flutter get compiled to Android?
Solution 1
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
Solution 2
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
CEO at InfoSig. Long time Ruby developer specializing in credit card processing systems. POS switches, e-commerce, e-wallets, risk management & prevention, PCI DSS compliance etc.
Updated on December 09, 2022Comments
-
bbozo 11 months
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
-
Ben Butterworth over 3 yearsIts 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?
-
Günter Zöchbauer over 3 years@BenB I guess it is because an Android application is not just an exe file. It's an APK and consists of several components. flutter.dev/docs/resources/faq#run-android might provide more insight.
-
Ben Butterworth about 2 yearsThanks for your reply, I only see it now I think. Unless I forgot all about it 😅
-
user2134488 almost 2 yearsBut here - 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.
-
Günter Zöchbauer almost 2 years@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.