Flutter apps are too big in size

JavaAndroidDartFlutter

Java Problem Overview


I have good experience in android app development using java. Recently I came to know about flutter. So, I have tried to create a simple android app with flutter based on official tutorial. But surprisingly the debug app size is 25MB and release apk costs more than 7MB. It is really larger when compare with native developed android app.

Is there any way to optimize it?

Java Solutions


Solution 1 - Java

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 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

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 > (Replace 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: > > /build/app/outputs/apk/release/app-armeabi-v7a-release.apk > /build/app/outputs/apk/release/app-arm64-v8a-release.apk > /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

Solution 5 - Java

Yes ofcourse, the size of the apk or ipa built with flutter will be minimum of ~7mb for a hello world app. This is because, flutter ships a core engine, framework, ICU data, LICENSE file etc with its build output which are mandatory for a flutter app to run.

You can check out the FAQ here to know more about what takes how much size when build.

Hope that helps!

Solution 6 - Java

I have used this command to generate Released apk for production stage:

flutter build apk --split-per-abi

the output apk is located in project folder:

[project]/build/app/outputs/apk/release/app-armeabi-v7a-release.apk

in my case the size of apk is reduced to 5.3 MB (after above command execution). the reason for big size (almost > 50 MB) because they contain everything you might need during a hot restart or restart which prevents building whole system again when you made just a small change in your code .

Release builds are real result of all your code and they are real minified apps without any containers which then you can distribute on google store.

Solution 7 - Java

all this kind of cross-platform app are larger from start
i did work with react-native and hello-world app is about 6 MB
all you can do is make a two release apk ,one for arm cpu and one for x86 cpu in this way you can lower the size about 4 MB but never gonna be small as android

according to google > one way to reduce the size of your APK is to create multiple APKs that contain files for specific screen densities or ABIs.


check here for more info https://developer.android.com/studio/build/configure-apk-splits.html

UPDATE: if my it not fully supported now it ,will be in future for sure, in every phase of flutter development it will be this method work but not too much in beta release , but this method will work better by releasing more version of flutter

Solution 8 - Java

Here is Official Android documentation that made my apps go From ~20mb To ~9mb

Try the Proguard recommendation

Link: https://developer.android.com/topic/performance/reduce-apk-size

Before and After using ProGuard and more enter image description here

Solution 9 - Java

TL;DR: use command flutter build apk --split-per-abi

After running flutter build apk --release, my APK size was 16.2 MB. This APK is called FAT APK which is a single APK that contains binaries for multiple ABIs embedded within it and supports multiple architectures. With flutter build apk --split-per-abi, dart code obsfucates resulting in 3 APK files, size of my app.APK was reduced to 5.6 MB.

Here are the official docs explaining the same: https://docs.flutter.dev/deployment/android

Solution 10 - Java

Add Proguard file with the below code in it

-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }

Note: If the Proguard file does not exist then need to create at the location

/android/app/proguard-rules.pro

Solution 11 - Java

I also came across in this very apk condition when debugging, while in releases around 7-8 mb. This however we could see it in much larger apps, where natively speaking, we need to import many libraries, while with Flutter the work is optimized. So if we assume an app that natively should weigh around 30mb with Flutter it should be similar. What can scare you is in the very basic apps. The important thing however is to optimize the images

Solution 12 - Java

You could use the apk analyzer in android studio to see what causes the app size to be that large.

  • In the case of having large and multiple image assets, you might want to use SVGs instead of PNGs.

  • Where you have to use PNGs or JPGs, you should compress them.

  • You might also want to use cached_network_image and call the image from some external service like Firebase. This will load the image from the internet on first launch and cache it to your app. You can check out cached_network_image on pub.dev.

  • For your fonts, you should use google fonts instead of binding the font files to your app. You can check out google fonts on pub.dev.

  • You should also shrink resources and set minifyEnable to true in your build.gradle in /android/app.

  • For uploading to playstore, you should generate and upload an app bundle instead.

You can follow this read on reducing your flutter app sizes. Some of the recommendations are also applicable to native android development. Reducing flutter app size

Solution 13 - Java

For a debug apk below are steps for optimize -

  1. Analyze for apk using Analyze APK from build tool of Android studio or manually by extracting the zip.

  2. In my case the biggest folder was lib which contains 4 different folder named arm64-v8a , armeabi-v7a ,x86,x86_64

  3. So these all four folders are basically different processor architecture of mobile devices.So to figure out your device falls under which category,below are some examples-

    ARM: This is a mobile processor architecture first and foremost, and what the majority of phones run now. Qualcomm’s Snapdragon, Samsung’s Exynos, and MediaTek’s mobile chips are all examples of ARM processors. Most modern chips are 64-bit, or ARM64.

    x86: This is the architecture specification for Intel chips. As dominant as Intel is in the computer market, these chips are far less common in Android handsets. x86_64 refers to 64-bit Intel chips.*

  4. After finding your desired/required apk ,you can now split your apks based on these processor architecture using the command flutter build apk --debug --split-per-abi

  5. And finally check the build/app/outputs/apk/debug/app-arm64-v8a-debug.apk(here replace with your choice) folder to get your desired and reduced apk.

Solution 14 - Java

There are many possibilities:

First , build your application in release mode by using :

In your terminal : flutter build --release

or just specify the target :
For Android Apk : flutter build apk --release
For Android App Bundle: flutter build appbundle --release
For IOS : flutter build ios --release

> By default, flutter run compiles to debug mode .This explains the > large size of the application . Debug mode (Hot reload , Dart Devtools etc ..) vs Release Mode (Simple Application)

> By default flutter build build for release mode . So you can just do flutter build

Using --split-debug-info flag can dramatically reduce code size. For an example of using this flag, see Obfuscating Dart code.

Some of the other things you can do to make your app smaller are:

  • Remove unused resources
  • Minimize resource imported from libraries
  • Compress PNG and JPEG files

Your can learn more about flutter app size here

Solution 15 - Java

A basic flutter "Hello World" app will be approximately 10mb in iOS and 4mb in Android. This cannot be smaller because of the runtime and LICENSE etc.

To make your app apk size small (for android):

$ flutter clean
$ flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi

Solution 16 - Java

You can obfuscate your code and remove the debug symbols from your binary.

flutter build apk --obfuscate --split-debug-info=/<project-name>/<directory>

Replace "apk" with "ipa" for iOS, and "appbundle" for Android before releasing the application.

For e.g.

flutter build appbundle --obfuscate --split-debug-info=symbols/

This will prevent your code from reverse engineering as well as remove debug symbols which will help in reducing the app size.

I saw around 10% decrease in my app size after doing this but it will vary depending upon your application.

Source: https://flutter.dev/docs/deployment/obfuscate#obfuscating-your-app

Solution 17 - Java

Check this below steps it may help you to reduce app size (only Android), i have reduced my 48mb build to 14mb

Step 1: android/gradle.properties android.enableR8=true

Step 2: android/app/build.gradle

inside -> buildTypes -> release

minifyEnabled true
shrinkResources true
useProguard true

Step 3:

Run

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

or

flutter build apk --split-per-abi

Some other optimizing tips,

1. Image assets

Upload the images in permanent storage path like AWS or in your website server and use the link to that image in your code.

2. Icons

Its recommended to use from Material Icons or Cupertino Icons class. You can add --tree-shake-icons option to flutter build command, to remove all of the not used icons from the bundle. This will potentially save the size of your app. (use svg format icons)

3. Fonts

If we are using more fonts from local assets similar like images these fonts will also increase app size. The best solution is to use google_fonts plugin. This pluign will dynamically download font when it is used.

4. Dynamic App Delivery

We could build an app bundle if we are uploading to playstore or we could split the apk per abi which splits the apk to x64 and x86 bit code. By using appbundle Google Play’s new app serving model, called Dynamic Delivery, uses your app bundle to generate and serve optimized APKs for each user’s device configuration, so they download only the code and resources they need to run your app.

Refer below links for more understanding,

  1. https://developer.android.com/studio/build/shrink-code
  2. https://developer.android.com/guide/app-bundle
  3. https://www.youtube.com/watch?v=9D63S4ZRBls

Solution 18 - Java

My apka build by flutter was of 47mb, Application app bundle -60mb.. After uploading bundle on play store size is11 to 20 mbenter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteephan SelvarajView Question on Stackoverflow
Solution 1 - JavaCanaan EtaiView Answer on Stackoverflow
Solution 2 - JavaVilokan LabsView Answer on Stackoverflow
Solution 3 - JavaiDecodeView Answer on Stackoverflow
Solution 4 - JavaBlasankaView Answer on Stackoverflow
Solution 5 - JavaHemanth RajView Answer on Stackoverflow
Solution 6 - Javauser2448808View Answer on Stackoverflow
Solution 7 - Javanima moradiView Answer on Stackoverflow
Solution 8 - JavaMr. Adrien FeudjioView Answer on Stackoverflow
Solution 9 - JavaabhiView Answer on Stackoverflow
Solution 10 - JavaJitesh MohiteView Answer on Stackoverflow
Solution 11 - JavaAlexPadView Answer on Stackoverflow
Solution 12 - JavaMastersamView Answer on Stackoverflow
Solution 13 - JavaB.shrutiView Answer on Stackoverflow
Solution 14 - JavaKab AgoudaView Answer on Stackoverflow
Solution 15 - JavaMaulik ShahView Answer on Stackoverflow
Solution 16 - Javashubhamgarg1View Answer on Stackoverflow
Solution 17 - JavaDipak RamoliyaView Answer on Stackoverflow
Solution 18 - JavaI'm a BelieverView Answer on Stackoverflow