Flutter apps are too big in size

We Are Going To Discuss About Flutter apps are too big in size. So lets Start this Java Article.

Flutter apps are too big in size

  1. Flutter apps are too big in size

    One way that i use to reduce my app size is to use;
    flutter clean
    before i run the build command;
    flutter build appbundle --target-platform android-arm,android-arm64
    When i run the build command without the clean command, i get around 32mb, but if i run the clean command first, i get around 18mb

  2. Flutter apps are too big in size

    One way that i use to reduce my app size is to use;
    flutter clean
    before i run the build command;
    flutter build appbundle --target-platform android-arm,android-arm64
    When i run the build command without the clean command, i get around 32mb, but if i run the clean command first, i get around 18mb

Solution 1

One way that i use to reduce my app size is to use;

flutter clean

before i run the build command;

flutter build appbundle --target-platform android-arm,android-arm64

When i run the build command without the clean command, i get around 32mb, but if i run the clean command first, i get around 18mb

Original Author Canaan Etai Of This Content

Solution 2

Flutter team acknowledges it here.

There’s an explanation for this here, quoting the same –

In August 2018, we measured the size of a minimal Flutter app (no
Material Components, just a single Center widget, built with flutter
build apk), bundled and compressed as a release APK, to be
approximately 4.7MB.

For this simple app, the core engine is approximately 3.2MB
(compressed), the framework + app code is approximately 840KB
(compressed), the LICENSE file is 55KB (compressed), necessary Java
code (classes.dex) is 57KB (compressed), and there is approximately
533KB of (compressed) ICU data.

Of course, YMMV, and we recommend that you measure your own app, by
running flutter build apk and looking at
build/app/outputs/apk/release/app-release.apk.

Also, the relative differences in apk size would likely be smaller with larger apps. Flutter’s overhead size is fixed.

Original Author Vilokan Labs Of This Content

Solution 3

First check these:

  • As other answer mentioned remove all unnecessary assets(images, fonts
    and files).

If you have too many fonts that will affect apk size heavily and flutter also made a solution for that by creating a package for you to get fonts from google fonts library(awesome package that give you access to so much fonts and flexibility to use anywhere). Get the package here and Read more here.

  • Remove unnecessary packages/ plugin that doesnt use(Not much affect
    though).

  • Remove unused resources

  • Minimize resource imported from libraries

  • Support a limited number of screen densities

  • Compress PNG and JPEG files

Read this also: Measuring your app’s size

Please note these too:

If you build your apk using flutter build apk it will contains both arm-32 and arm-64 apks(Which flutter will show in console when you building apk). If you are building app bundle this is not an issue and its size is much smaller.

To avoid one flat apk containing arm-32 and arm-64, you can build them separately using below two commands:

flutter build apk --target-platform=android-arm

Above will produce arm-32 bit apk. Go to project -> build -> app -> release and rename the apk to this: app-armeabi-v7a-release.apk.

then increment version code in pubspec.yaml, next flutter pub get and do this:

flutter build apk --target-platform=android-arm64

Above will produce arm-64 bit apk. Go to project -> build -> app -> release and rename the apk to this: app-arm64-v8a-release.apk.

Then you can submit two apks separately(lower apk version first).

Since, you have to run two commands by incrementing version code, flutter made it easier by this command (flutter > 1.5.4 I think): flutter build apk --split-per-abi. That command will increment apk version code for the second apk and give you two renamed apks (Please note that this command will produce with higher version code(ex: 3222)).

From doc:

From the command line:

Enter cd <app dir>
(Replace <app dir> with your application’s directory.)
Run `flutter build apk --split-per-abi`
(The flutter build command defaults to `--release`.)

This command results in two APK files:

<app dir>/build/app/outputs/apk/release/app-armeabi-v7a-release.apk
<app dir>/build/app/outputs/apk/release/app-arm64-v8a-release.apk
<app dir>/build/app/outputs/apk/release/app-x86_64-release.apk

Removing the --split-per-abi flag results in a fat APK that contains
your code compiled for all the target ABIs. Such APKs are larger in
size than their split counterparts, causing the user to download
native binaries that are not applicable to their device’s architecture

read more here.

I also heard somewhere that in latest flutter update they have made flutter app size even smaller. There is a added issue for this also: https://github.com/flutter/flutter/issues/16833

Original Author Blasanka Of This Content

Solution 4

Use following commands to analyze which is taking up most your app’s space.

For Android’s AppBundle:

flutter build appbundle --target-platform android-arm --analyze-size
flutter build appbundle --target-platform android-arm64 --analyze-size
flutter build appbundle --target-platform android-x64 --analyze-size

For Android’s APK:

flutter build apk --target-platform android-arm --analyze-size
flutter build apk --target-platform android-arm64 --analyze-size
flutter build apk --target-platform android-x64 --analyze-size

For iOS:

flutter build ios --analyze-size

For Linux:

flutter build linux --analyze-size

For macOS

flutter build macos --analyze-size

For Windows

flutter build windows --analyze-size

This includes native code, assets, and even a package-level breakdown of compiled Dart code.


DevTools

If you want to see the visual representation of it, use DevTools. For that after running previous command, run:

flutter pub global run devtools --appSizeBase=apk-code-size-analysis_01.json

You’ll be then directed to your browser, where you will see something like this:

enter image description here

Original Author iDecode Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment