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.
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.
Related
Could anyone please explain behind the scenes of the flutter determines written code is for android or ios.
Dart is ahead-of-time (AOT) compiled into fast native X86 or ARM code for Android and iOS devices. Flutter can also call out to Android and use Android features only available in Java (same with iOS). Flutter is written in DART so we can't call that it compiles but DART does and it's rendering engine does.
Flutter Framework provides all widgets and packages whereas Flutter SDK allows you to build apps for android and IOS
The engine’s C/C++code is compiled with Android’s NDK or iOS’ LLVM. Both pieces are wrapped in a “runner” Android and iOS project, resulting in an apk or ipa file respectively. On app launch, any rendering, input, or event is delegated to the compiled Flutter engine and app code.
I thought images would be much clear instead of writing many things.
More of these:
https://docs.flutter.dev/resources/faq#run-android
I have a question that why is C++ used in Android App Development, What Requirements aren't Full filled by Java or Kotlin so C++ Came to Picture.
Android by itself is kind of linux system, where app is run by davlik virtual machine (and byte code for it is compiled form java / kotlin). Sometimes there is a need to be closer to operation system and develop in native code - say in C/C++. NAtive parts of software have access to other APIs not available from davlik VM
I am trying to digest the following information from Flutter site.
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
native, ARM, and x86 libraries. Those libraries are 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, is delegated to the compiled
Flutter and app code. This is similar to the way many game engines
work.
Is each Flutter app (more precisely each Android app that is created with Flutter) published with Flutter engine attached?
Yes, it is included in every app. There is no code sharing between apps on Android/iOS. Also each app could have a different version of the engine (depending on when it was compiled)
Note that the with the upcoming Flutter 1.0, the overhead (on Android) is less than 5M, which is not bad. I believe the overhead is similar on iOS.
libflutter.so is 27.1MB on my emulator for each of a full / production app and a hello world app.
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
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
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.
I have heard it said that the C++ code runs "natively" on all platforms. By this I guess that what is being suggested is that the cross-platform ability of Qt is not using something like HTML 5.
But does this mean that on Android Qt code runs in the Dalvik virtual machine?
Android understands either Dalvik or the newer ART. Dalvik and ART both have the ability to link to C/C++ code through the Android NDK which is Android's take on JNI. QT for Android while being C++ based still requires a minimal amount of Java based initialization. What this means is that even if the C++ code runs natively (architecture dependent) to the underlying OS, it is still required to be presented through code that runs on the VM (Android specific) as the VM does not directly understand C++ but can link to it through it's own Java based framework which is the Android NDK.
Please check https://www.qt.io/blog/2013/07/23/anatomy-of-a-qt-5-for-android-application
At the very top of levels, a Qt for Android application consists of two parts:
The Qt application:
This is the cross-platform code and resources that you, as the app developer, manage yourself, and which are summarized by your qmake .pro file.
An Android application launcher:
This is generated for you by Qt Creator the first time you connect your project to a Qt for Android Kit.
So, Android application launcher should run in Dalvik VM. I can't sure if this works well with ART runtime of Android 5.0