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
Related
I've been using Flutter for almost a year now, I never used Android Studio or Android application development with Java/Kotlin. Most often I get these errors related to Gradle, changing classpaths and implementation and I don't seem to understand any of that.
Any app in the Flutter is created with the help of three languages Dart, C and C++. Here you can also tell that every flutter application is a combination of these three language’s code.
The Entire Flutter framework is created in Dart and it is also used by us to develop an app. Here also notice that we do not directly use C and C++ code to write apps, instead, C and C++ code is used in Graphic rendering engine and Dart Virtual machine to perform their tasks.
Let's dig deeper on how flutter compiles for Android
Graphics Engine’s C and C++ code are compiled with the help of
Android’s NDK (Native Development Kit).
The dart code both SDK’s and ours are compiled through AOT (Ahead-of-Time) compilation process to native ARM and x86 libraries.
After successful compilation, the APK file is generated.
Any widget rendering, input-output, event handling and so on is done by compiled app code.
Here notice that Debug mode builds use a Dart virtual machine to run Dart code in order to enable stateful hot reload.
So related to the folder structure, ios contains part of iOS code and uses CocoaPods to manage dependencies. The android contains part of Android code and uses Gradle to manage dependencies. The lib contains part of Dart code and uses pub to manage dependencies. Cocoapods in iOS corresponds to Podfile and Podfile.lock while pub corresponds to pubspec.yaml and pubspec.lock.
The iOS/Android project under Flutter is still essentially a standard iOS/Android project. Flutter only generates and embeds App.framework and Flutter.framework (iOS) by adding a shell to the BuildPhase. Flutter also adds flutter.jar and vm/isolate_snapshot_data/instr (Android) through gradle to compile Flutter-related code and embed them into a native App.
If you want more depth information about the compilation process see this article https://www.alibabacloud.com/blog/an-in-depth-understanding-of-flutter-compilation-principles-and-optimizations_597747
I'm new to flutter so I really wanted to know do we need android SDK, ios SDK along with flutter SDK to develop flutter applications? As flutter SDK converts code directly into native/machine code so why I am really curious to know if we need android SDK and ios sdk? Yes I know when we make android native applications We need Android SDK but do not know if we need it in flutter apps as well if we need it then what would be the role of android SDK and flutter SDK
Short answer Yes.
Reasons for this is that the flutter SDK only handles the flutter side of compilations such as Dart. technically each flutter app is inside of a native app Shell. flutter uses various native code modules under the hood to make the app work in all the supported platforms and provide Platform dependent functionality such as File System, Camera, etc...
for example, how file systems work in iOS and Android are different, so flutter uses separate native code to work with the file systems in those 2 OSes. So, to compile those two native codes, flutter needs each OSes SDKs such as iOS SDK and Android SDK.
Best example: https://github.com/flutter/plugins/tree/main/packages/path_provider
You can see how this plugin is implemented differently on each supported platforms.
Of course, you do need an SDK in order to build whether an Android or an IOS. To perform certain tasks like accessing a camera, storage, etc.
Upon opening APK files I've found code in Kotlin, instead of Dart. And upon opening IPA files, finding Swift code instead of Dart.
What happens to the Dart code? Is it transpiled to the native SDK language?
If so, what is use of the libflutter.so and libapp.so? assuming that the application is now native, why would it still need the Dart runtime?
Flutter is an SDK developed by Google. It's a Dart library built to provide GUIs with native look & feel. This is achieved via the Flutter Engine (using Google Skia), last I saw built in C++. However, interfacing calls to the native platform are done via their specific SDKs (and native languages, such as Swift and Kotlin/Java).
Dart is both a programming language and a platform (Dart VM). And it can be run in many ways:
Dart Virtual Machine: On Windows, macOS and Linux, using Just-in-Time.
Native: Using dart2native, Dart can be compiled to self-contained, single file, native executables.
JavaScript: Using a source-to-source compiler, Dart code converts to JavaScript and can be run in most web browsers.
AOT / Ahead-of-Time: This is fully native to mobile platforms (iOS / Android) and used mostly for delivery to app stores.
Under the hood, there's a lot of solid magic going on. It may include converting some of your GUI specific related code to native. Enjoy!
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.