Flutter apps are too big in size
Asked Answered
S

18

143

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?

Stomachache answered 2/3, 2018 at 8:3 Comment(5)
At first I'd look into the APK file and analyze what parts cause the size #3599710 (please post some of the gained info). You should be aware that a flutter app contains the whole runtime, so there is no dependency of a specific Android SDK version (except for plugins), therefore bigger size is inevitable. Flutter is not even 1.0. The Flutter team is aware of the size and mentioned that they'll address that later.Rompish
This is normal for flutter due to additional necessary files, in my android go device it has taken 60mbs just for default app in flutter new project, in this sense I think it takes even more on Android Go devices.Arterio
Your lucky! For React Native + Expo, I have to distribute a 30MB APK and the IPA download is 110MB (but get's slimmed on the app store). Also, one answer pointed out that the size is fixed.Musso
i would happily swap all the things flutter offers for a few extra megabytes added in the apk sizeLoyola
It's still by far smaller than a react-native or Ionic App ;-)Calv
J
83

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.

Java answered 2/3, 2018 at 17:25 Comment(5)
+1 for the overhead comment. Just to add an updated thread on this github.com/flutter/flutter/issues/16833 . The basic app now is ~4MB in Android and ~10MB in iOS.Schulein
@Ricardo BRGWeb 10MB on iOS where - as a build in appstore connect? It's certainly not, it's more like 30MB but Android is certainly as small as you say.Telegraphese
I have an app on playstore built with flutter and the size is 1.5mb .. play.google.com/store/apps/details?id=site.appsfactoryPosner
For simple react native apps I think ~10MBFincher
@Vaibhav are you sure about "Flutter's overhead size is fixed."? I could not find any reference for that. I understand it does make sense and the documentation provided a link in the faq measuring the engine size to be something between (3-4MB) but the overall overhead could be other customization code rather than just the pure optimized engine size. So I was wondering if there is any benchmark for the overall overhead hoe=w much it could beObadias
B
216

Seems there is a minimum-limit for App size, which's around 4 MiB for Android, see the other answer for said limit's details and iOS, but to optimize continue reading:

One way that I use to reduce my App's size is to run:

flutter clean

Note to run above before you run the build command, which's for Android's App-bundle:

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

And for Android's APK:

flutter build apk --target-platform android-arm,android-arm64,android-x64

When I run the build command without the clean command, I get around 32 MiB, but if I run the clean command first, I get around 18 MiB download size.

Also, Flutter does not yet support x86 arch,
see: deployment/android

Bramble answered 25/10, 2019 at 5:40 Comment(8)
This is the only answer which answers the question on how to optimize.Vincentvincenta
Is this release enough for when releasing to the play store? Will it work on most of the device? @CanaanEtaiContexture
Yes @JithinJudeBramble
I published the app and it worked. Now I'm getting good reviews from users. Thanks @CanaanEtai , helped a lot in reducing app install size.Contexture
Can someone explain how it works?Polytheism
It does not work for me. The flutter clean command before build does not affect the appbundle size as flutter devtools shows by two diffs. I've executed flutter build appbundle --analyze-size --target-platform android-x64 during development and have compared to the same build analysis after flutter clean. The difference was +4Byte actually (appbundle after clean is lager on 4 bytes)Guillot
I added x86_64 arch (android-x64) to your --target-platform, but because you say same command gives different size, that's no problem, right?Calv
In other words, does said clean command really optimize, or, is clean simply fixing Flutter's "auto including x86_64 by mistake, even if android-x64 is not passed"?Calv
J
83

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.

Java answered 2/3, 2018 at 17:25 Comment(5)
+1 for the overhead comment. Just to add an updated thread on this github.com/flutter/flutter/issues/16833 . The basic app now is ~4MB in Android and ~10MB in iOS.Schulein
@Ricardo BRGWeb 10MB on iOS where - as a build in appstore connect? It's certainly not, it's more like 30MB but Android is certainly as small as you say.Telegraphese
I have an app on playstore built with flutter and the size is 1.5mb .. play.google.com/store/apps/details?id=site.appsfactoryPosner
For simple react native apps I think ~10MBFincher
@Vaibhav are you sure about "Flutter's overhead size is fixed."? I could not find any reference for that. I understand it does make sense and the documentation provided a link in the faq measuring the engine size to be something between (3-4MB) but the overall overhead could be other customization code rather than just the pure optimized engine size. So I was wondering if there is any benchmark for the overall overhead hoe=w much it could beObadias
V
46

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

Vo answered 3/10, 2020 at 12:16 Comment(3)
what can we do for the web build ?Ihram
Running the above commands shows that my app is ~10MB, but when I install it on my Android phone, it's almost 500MB. What would be the reason for that and how could I see that in the analysis?Superficial
Never mind, that was the debug apk, the release apk is about 40MBSuperficial
C
28

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

Chilung answered 8/1, 2020 at 4:43 Comment(2)
@Chilung by having multiple apk, how we publish this multiple apk to playstore?Papyraceous
@Papyraceous You can add multiple apks to one release but version of each apk should be incrementally updated.Chilung
W
16

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!

Wellheeled answered 2/3, 2018 at 8:9 Comment(3)
7mb is fine. but flutter now generates ipa of 61mb, which is way too high for basic app.Belongings
Hope you are building debug version. Try building a release version. The above size is for a Hello World app in flutter. The size can vary depending on native dependencies and other plugins.Wellheeled
it was a release build. after some debugging, i found out that swift core libraries contributes to half of the total size of IPA, if we use swift, which was my case. Final universal IPA size with swift libs was ~21mb and thinned IPA was ~9mbBelongings
F
9

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.

Fujimoto answered 21/7, 2021 at 14:44 Comment(0)
N
7

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

Niles answered 9/1, 2022 at 7:35 Comment(1)
The best so far, thanks bossAccounting
M
5

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

Malachite answered 21/10, 2019 at 23:7 Comment(0)
M
4

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

Melanous answered 2/3, 2018 at 8:33 Comment(0)
H
4

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
Homomorphism answered 30/12, 2021 at 13:7 Comment(0)
L
2

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
Linguini answered 28/9, 2020 at 12:50 Comment(0)
O
1

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

Odine answered 26/9, 2019 at 10:31 Comment(0)
Y
1

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

Yawning answered 2/5, 2020 at 23:15 Comment(1)
Your answer was very helpfulCheryl
D
0

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.

Dart answered 28/5, 2020 at 17:46 Comment(0)
S
0

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

Senter answered 9/2, 2021 at 5:57 Comment(0)
S
0

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
Sequence answered 10/9, 2021 at 16:38 Comment(0)
I
0

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

Ironsides answered 31/10, 2021 at 8:4 Comment(0)
I
-2

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

Inurbane answered 6/3, 2021 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.