Flutter App size is still the same even after scaling down images

598 Views Asked by At

I am playing around with flutter and building a simple quiz app. The project is almost ready to publish on play store and have several things to fix. But, I tried new flutter --analyze-size command and found out that I can reduce the file size more by scaling down images. I scaled images (it reduced the sizes significantly) and replaced the original files. Then I ran a flutter clean and flutter pub get and ran flutter build apk --analyze-size --target-platform=android-arm64 just to test again. Although I reduced the image file sizes, the analysis tool shows the same sizes as before.

I also tried invalidate caches/restart option in android studio. But still the same result. When I selected two json files to compare in Diff option in DevTools, it says that they are identical files. Please, Can someone point me out how to fix this without creating a new project and moving files and assets?

P.S. I am on flutter channel beta, 2.5.0 on Windows 10

6

There are 6 best solutions below

3
On

Try rm -rf build in addition to flutter clean.

0
On

You should follow these steps to reduce FLutter app size:

  1. Use google-fonts package
  2. Use flutter build apk --split-per-abi to build apks
  3. remove unwanted icons from bundle. To do this, add --no-tree-shake-icons flag to your apk build command
  4. Use.svg format icons
  5. Use shrinkResources true in /android/app/build.gradle inside release block
  6. Remove unused assets/packages
  7. Use cached images instead of using from assets
  8. Use progaurd(here).
0
On

Rename the images (add a 1 or something trivial), clean, rebuild and then rename back to original names, clean, rebuild.

Flutter can be caching filenames.

2
On

As mentioned on this answer, before running flutter clean, check on your Windows Task Manager if there's some Flutter process running in the background, it's true, stop it.

enter image description here

However, currently you can update your Flutter version to 2.5.1 (stable). How? Running this command: flutter upgrade.

Also, you can switch between the 4 release channels (stable, beta, dev and master) with flutter channel <channel_name>

0
On

Well You can try following things to make sure this doesn't happen,

In order:

  • Run flutter clean
  • Manually delete the /build folder, which is essentially the same as flutter clean
  • As @Guillem Already said there could be a task manager glitch restarting machine should do the trick.
  • flutter pub cache repair basically trigger cache related to pub specs but it may help in your case to trigger cache refresh give it a try.
  • Backup All image assets somewhere and delete them from project just for the sake of building it without it and then on error include images in project and try above steps again.

Let me know if any of this worked for you.

0
On
  1. Make sure the images that you reduced the size are part of your flutter assets. If they're not listed in the pubspec.yml then they're not part of your apk files. If they're not part of your apk file, then no matter the size you reduce, it's not gonna affect the apk size. You will also not see any diff in analyze result file .json either.

  2. Double-check if your images' size is actually changed in your output apk file. When you run --analyze-size, it also created the apk. In Android Studio, you can open the apk and check your images size packed in the apk.

enter image description here

Sometimes the APK size remains the same but the downloadable size is changed. I'm also not sure what's happening here but as long as the actual images size in the apk are reduced and the download size of the end-user is reduced then, the initial purpose is already achieved so I just carry on.

enter image description here