bbozo
bbozo

Reputation: 7311

How does Dart/Flutter get compiled to Android?

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

Upvotes: 18

Views: 13167

Answers (2)

bbozo
bbozo

Reputation: 7311

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.

Upvotes: 4

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657909

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

Upvotes: 28

Related Questions