Accoding to Flutter's build modes,
Compilation is optimized for fast startup, fast execution, and small package sizes.
What exactly does optimizing for small package sizes mean?
For web apps, the doc states:
The build is minified and tree shaking has been performed.
Does Flutter perform dead-code elimination on Dart code for Android/iOS when building the app in release mode? If so, how?
Comment: When analyzing the generated Android release apk
using Deeper analysis in DevTools
, it seems that dead code is not included. So at least for Android, it seems that Flutter performs dead-code elimination in release mode.
Tree Shaking is also used in Flutter to reduce the final packet size. Flutter provides three construction modes. For each mode, the Flutter compiler has different optimizations on the output binary files. The Tree Shaking mechanism can't be triggered in the Debug mode.
But fear not, Flutter is here to the rescue… Flutter is Google's mobile UI framework that provides a fast and expressive way for developers to build native apps on both iOS & Android, using a single codebase. That seems great.
What kinds of apps can I build with Flutter? Flutter is designed to support mobile apps that run on both Android and iOS, as well as interactive apps that you want to run on your web pages or on the desktop.
Tools like webpack will detect dead code and mark it as “unused module” but it won't remove the code. Webpack relies on minifiers to cleanup dead code, one of them is UglifyJS plugin, which will eliminate the dead code from the bundle. It only works with import and export . It won't work with CommonJS require syntax.
(Running flutter build defaults to a release build.) The release bundle for your app is created at [project]/build/app/outputs/bundle/release/app.aab. By default, the app bundle contains your Dart code and the Flutter runtime compiled for armeabi-v7a (ARM 32-bit), arm64-v8a (ARM 64-bit), and x86-64 (x86 64-bit).
Tree shaking by default When compiling a Flutter web application, the JavaScript bundle is generated by the dart2js compiler. A release build has the highest level of optimization, which includes tree shaking your code. Tree shaking is the process of eliminating dead code, by only including code that is guaranteed to be executed.
During a typical development cycle, you test an app using flutter run at the command line, or by using the Run and Debug options in your IDE. By default, Flutter builds a debug version of your app. When you’re ready to prepare a release version of your app, for example to publish to the Google Play Store , this page can help.
By default, the app bundle contains your Dart code and the Flutter runtime compiled for armeabi-v7a (ARM 32-bit), arm64-v8a (ARM 64-bit), and x86-64 (x86 64-bit). An app bundle can be tested in multiple ways—this section describes two.
I also wonder if dart compiler (not just flutter) does the tree shaking or not. In release mode, Flutter doc only mentions: Compilation is optimized for fast startup, fast execution, and small package sizes.
I think if they don't mention tree shaking here, it means the optimization doesn't have this feature.
But this article says yes: https://medium.com/flutter-community/excluding-dart-code-from-the-release-compiled-executable-7af8c18cd241
It would be nice if someone can give an official link about this.
Add information about code optimization for Android:
I don't see any information about tree shaking for iOS and linux:
Yes you are right with your assumption that dart performs within its release build tree shaking which can dramatically reduce the size of the app.
Here is the official explanation from the flutter team for the Release App Build:
Release: Use release mode for deploying the app, when you want maximum optimization and minimal footprint size. For mobile, release mode (which is not supported on the simulator or emulator), means that: Assertions are disabled. Debugging information is stripped out. Debugging is disabled. Compilation is optimized for fast startup, fast execution, and small package sizes. Service extensions are disabled.
Release mode for a web app means that: The build is minified and tree shaking has been performed. The app is compiled with the dart2js compiler for best performance.
You can read more about it within the following link: https://flutter.dev/docs/testing/build-modes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With