Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce --release apk and ipa sizes #16833

Closed
eseidelGoogle opened this issue Apr 21, 2018 · 30 comments
Closed

Reduce --release apk and ipa sizes #16833

eseidelGoogle opened this issue Apr 21, 2018 · 30 comments

Comments

@eseidelGoogle
Copy link
Contributor

@eseidelGoogle eseidelGoogle commented Apr 21, 2018

We've had a bunch of issues related to this, most directly #12456 as another. Attempting to create an issue which we can drive to action.

Android Instant Apps #7614 will require a < 4MB initial .apk size (which doesn't have to include the Flutter engine perhaps, but should).

Our current "hello_world" apk size at time of writing is:
basic_material_app_android__compile release_size_bytes is 7907K
basic_material_app_ios__compile release_size_bytes is 9365K

My understanding is their is a ton of low hanging fruit here.

  • @mraleph has identified there is ~1mb of extra binary size in the dart runtime currently due to including observatory assets.
  • @chinmaygarde has previously stated that he believes there is about 1mb (compressed) of ICU data size to save with some compile flag changes.
  • It's possible we could even find a copy of ICU on Android or iOS which we could use directly (via GMSCore or other installed assets, we could even imagine having a fall-back path of locations to look). We investigated this in both #392 and #394 but things may have changed.

I'm sure there is a ton more we could do here with some investigation. This was our 3rd most requested work area in our most recent user survey.

FYI @cbracken @Hixie

@Hixie Hixie added this to the bucket4 milestone Apr 22, 2018
@ohir
Copy link

@ohir ohir commented Apr 23, 2018

Support for Vector Graphics resources precompiled to some compact representation is crucial for the apk size, too. See #1831 discussion.

There are tools in AndroidStudio to convert .svg files to the xml based VectorDrawable.
This xml can be a source file compiled for more compact format, e.g. one sketched at
https://godoc.org/golang.org/x/exp/shiny/iconvg.

@dnfield
Copy link
Member

@dnfield dnfield commented Apr 23, 2018

@ohir while asset compression/refactoring can certainly reduce APK/IPA size, I think this issue has more to do with "base" binary size for a bare minimum flutter app.

@chinmaygarde
Copy link
Member

@chinmaygarde chinmaygarde commented Apr 28, 2018

Update: Was investigating this some more today. We only include observatory assets in the Debug and Profile runtime modes. However, vmservice sources are included in the release mode even though we will never initialize the same in that mode.

@mraleph
Copy link
Member

@mraleph mraleph commented Apr 29, 2018

@chinmaygarde I am working on removing vmservice in release mode in dart-lang/sdk#32834

@saeidakbari
Copy link

@saeidakbari saeidakbari commented May 7, 2018

Also mention that if you look at Storage usage of an app after installing the application in settings, its getting two or tree time more storage than the same app written in native.
I think its too heavy in comparison.

@rickyazhari
Copy link

@rickyazhari rickyazhari commented Jun 28, 2018

Is there any way to reduce IPA size in release mode? I upload into appstore for testflight and got 32.8 MB for estimating appstore download size and 10 MB more bigger for app testflight. But i build release for APK android. it only got 15MB.

@eseidelGoogle
Copy link
Contributor Author

@eseidelGoogle eseidelGoogle commented Jun 28, 2018

I believe the IPAs are built to include both 32-bit and 64-bit binaries, but the AppStore only sends part of the IPA down. @cbracken would know.

@cbracken
Copy link
Member

@cbracken cbracken commented Jun 28, 2018

Correct; by default, we build universal binaries for iOS that include both armv7 and arm64 architectures for all three of:

  • Flutter.framework (the engine)
  • App.framework (the AOT-compiled app)
  • Runner (the entrypoint of the app)

Apple does app-thinning on their end such that the end-user downloads only the bits relevant to their device. To get the download size, log in to App Store Connect, select your app, navigate to the Activity page, select your build, then click the App Store File Sizes link.

@eseidelGoogle
Copy link
Contributor Author

@eseidelGoogle eseidelGoogle commented Jun 28, 2018

We may want to add @cbracken's comment above to the FAQ: https://flutter.io/faq/#why-is-my-apk-or-ipa-so-big

@rickyazhari
Copy link

@rickyazhari rickyazhari commented Jul 6, 2018

Thanks for feed back. Our apps actually has live right know in app store its called tanamduit financial platform for mutual fund. I do build with only single architecture(arm64) and seem success when export via adhoc just got ~12MB with single IPA. But when upload into app store still got a huge size( ~ 42 MB) looking forward into play store after figure out how to handle custom onBackPress when user hit the back button instead than hit button in app .

Keep up the good work. Still looking forward about my issue.

@nitinmehtaa
Copy link

@nitinmehtaa nitinmehtaa commented Jul 28, 2018

My default flutter .apk file size is 90.40 MB, the one which shows increment counter. like how can we go ahead with this language for our production code? This is way to much than java or swift.

@jogboms
Copy link

@jogboms jogboms commented Jul 28, 2018

@nitinmehtaa I believe that's for debug build, try building for release and watch the filesize change dramatically.

@zoechi zoechi added the a: size label Jul 29, 2018
@goderbauer
Copy link
Member

@goderbauer goderbauer commented Jul 30, 2018

@nitinmehtaa Do a flutter build apk --release on your app and check the size of the produced APK.

@Hixie
Copy link
Contributor

@Hixie Hixie commented Jul 30, 2018

@nitinmehtaa 90MB is definitely too big. If that was a release build, please file a bug and we'll help you look into it.

@nitinmehtaa
Copy link

@nitinmehtaa nitinmehtaa commented Aug 1, 2018

@jogboms @goderbauer @Hixie Thanks a bunch, App size is ~29 MB now on release build but that is also way too high for my app. I've built simple UI using widgets. no Backend. Here is my project link : https://github.com/nitinmehtaa/e-commerce-suit-in-flutter
can anyone clone and check why this apk seems too heavy. It would be a great help and learning for me.

@zoechi
Copy link
Contributor

@zoechi zoechi commented Aug 1, 2018

https://github.com/nitinmehtaa/e-commerce-suit-in-flutter/tree/master/build/app/outputs/apk/debug is in the debug folder which indicates it was created by a debug build. You need to measure the size of a release build as mentioned above.

@eseidelGoogle
Copy link
Contributor Author

@eseidelGoogle eseidelGoogle commented Aug 1, 2018

flutter build apk should build you a release apk, which I would expect to be smaller. If you're still having issues, please file a separate bug and folks can help.

https://github.com/flutter/flutter/issues/16191 is a request about writing down some of our tips/tricks for understanding APK size. @goderbauer is actively working on reducing APK size as we speak.

@goderbauer
Copy link
Member

@goderbauer goderbauer commented Aug 1, 2018

@nitinmehtaa Out of curiosity I took a look at your app. If I build it with the commands mentioned multiple times on this issues I get a final APK size of 7.5MB (this is on current master of flutter), which is about what I would have expected for the complexity of the app.

Not sure how you're arriving at your numbers?

~/dev $ git clone git@github.com:nitinmehtaa/e-commerce-suit-in-flutter.git
Cloning into 'e-commerce-suit-in-flutter'...
remote: Counting objects: 423, done.
remote: Compressing objects: 100% (253/253), done.
remote: Total 423 (delta 84), reused 415 (delta 80), pack-reused 0
Receiving objects: 100% (423/423), 87.21 MiB | 5.35 MiB/s, done.
Resolving deltas: 100% (84/84), done.
Checking out files: 100% (378/378), done.
~/dev $ cd e-commerce-suit-in-flutter/
~/dev/e-commerce-suit-in-flutter (master) $ flutter build apk --release
Running "flutter packages get" in e-commerce-suit-in-flutter...  9.4s
Initializing gradle...                                       1.4s
Resolving dependencies...                                   10.1s
Running 'gradlew assembleRelease'...                        34.8s
Built build/app/outputs/apk/release/app-release.apk (7.5MB).
@goderbauer
Copy link
Member

@goderbauer goderbauer commented Aug 2, 2018

To document some recent wins in the quest for smaller APK/IPA sizes:

  • flutter/engine#5784 removed Dart's VM Service from the compiled Dart code in release mode:
    • reduced dart code snapshots (contains framework + app code) on Android by 1.5MB (uncompressed)
    • reduced overall release APK size by 475KB (compressed)
  • flutter/engine#5922 switched Flutter to a new minimal ICU data config:
    • reduced size of ICU data bundled with APKs/IPAs by 5.1MB (uncompressed)
    • reduced overall release APK size by 1.6MB (compressed)

Overall release APKs are now 2MB smaller with those improvements. This brings the release APK size of a minimal Flutter app down to 4.7MB (from previously 6.7MB).

Similar size reductions can also be seen on iOS for IPAs, but due to Apple's encryption and App Thinning process for IPAs uploaded to the App Store it's harder to document exact numbers.

Update Sept 28: App Store Connect reports for the iPhone X that these changes reduced release IPA download sizes by 3.8MB and the minimal Flutter app on iOS is now down to 12.6MB.

@aegis123
Copy link

@aegis123 aegis123 commented Aug 6, 2018

Can you also make use of the android bundle/apk split which makes separate APK's per supported CPU architecture? like an APK only for armv64 and an apk only for x86?

@goderbauer
Copy link
Member

@goderbauer goderbauer commented Aug 13, 2018

@aegis123 Can you file a separate issue for that and cc me?

@matthew-carroll matthew-carroll moved this from To do to To do (Performance) in Add-to-App (stale) Aug 20, 2018
@matthew-carroll matthew-carroll moved this from To do (Performance) to In progress in Add-to-App (stale) Sep 13, 2018
@goderbauer
Copy link
Member

@goderbauer goderbauer commented Oct 3, 2018

There have been some more wins:

  • flutter/engine#6097 switches flutter to an even smaller ICU data config:
    • reduced size of ICU data bundled with APKs/IPAs by 363KB (uncompressed)
    • reduced overall release APK size by 84KB (compressed), similar reduction on iOS
  • flutter/buildroot#167 fixed symbol visibility and flutter/buildroot#165 tweaked compiler flags for Android:
    • reduced libflutter.so by 499KB (uncompressed)
    • reduced overall release APK size by 263KB (compressed)
  • flutter/engine#6247 is stripping debug symbols from Flutter.framework:
    • reduced Flutter.framework by 3.8MB (uncompressed)
    • reduced overall release IPA download size by 0.7 MB (as reported by App Store Connect for iPhone X)
    • reduced overall release IPA install size by 4MB (as reported by App Store Connect for iPhone X)
  • flutter/engine#6337 fixes symbol visibility of Dart on iOS:
    • reduced Flutter.framework by 145KB (uncompressed)
  • #22245 is stripping debug symbols from App.framework:
    • reduces App.framework by ~23% (uncompressed), that's minus 1.6MB for the minimal flutter app or minus 6MB for Flutter Gallery (uncompressed)
    • reduced release IPA download size of minimal Flutter app by 0.1MB on iPhone X as per App Store Connect
    • reduced release IPA install size of minimal Flutter app by 0.9MB on iPhone X as per App Store Connect

tl;dr: Overall, on Android our APK size for a minimal Flutter app is now down to 4.48MB (minus 257KB compared to my comment from Aug 2). During the same time, our IPK download size on iOS for the minimal Flutter app has been reduced by 1.2MB and is now down to 11.4MB (as reported by App Store Connect for iPhone X).

@goderbauer
Copy link
Member

@goderbauer goderbauer commented Oct 3, 2018

Notes about the size difference between Android and iOS:

The majority of the size difference is coming from the second bullet point.

@goderbauer
Copy link
Member

@goderbauer goderbauer commented Oct 25, 2018

Here's another update:

  • libFlutter.so has been reduced by 308 KB (uncompressed) and Flutter.framework by 359 KB (uncompressed) thanks to:
    • The Skia team's work on reducing its footprint in Flutter by removing unnecessary cruft
    • An updated clang version that produces slightly smaller binaries (flutter/engine#6633)
  • App snapshots have been reduced by 127 KB (uncompressed) on Android and 162 KB on iOS (uncompressed, App.Framework) thanks to:

In total, the overall APK size of our famous minimal Flutter app is now down to 4.28 MB (minus 192KB compared to my last update). The IPA download size on iOS of the same app has been reduced by 0.6 MB and is now down to 10.8 MB (as reported by App Store Connect for iPhone X).

@goderbauer
Copy link
Member

@goderbauer goderbauer commented Nov 21, 2018

The minimal Flutter app continues to shrink:

  • freetype2#a10b062 removed support for old/outdated font formats and other unused freetype2 modules
    • reduced libflutter.so by 135 KB (uncompressed)
    • reduced overall release APK size by 84 KB (compressed)

In total, the overall APK size of the minimal Flutter app is now down to 4.20 MB (minus 84 KB compared to my last update). The IPA download size has not changed since the last update.

@eseidelGoogle
Copy link
Contributor Author

@eseidelGoogle eseidelGoogle commented Dec 13, 2018

My understanding is that we've gotten most of the meat we're going to get here w/o more radical changes. Is my understanding correct? If so, is there still value in keeping this bug open @goderbauer?

@goderbauer
Copy link
Member

@goderbauer goderbauer commented Dec 13, 2018

Your understanding is correct. My plan is to do one last update on this bug with the latest numbers and then close it.

@goderbauer
Copy link
Member

@goderbauer goderbauer commented Dec 13, 2018

Some additional changes have landed that shrunk our minimal Flutter app further:

  • #25258 switched us to a custom-compiled libcxxx and libcxxabi for Android:
    • reduced libflutter.so by 117 KB (uncompressed)
    • reduced overall release APK size by 46 KB (compressed)
  • flutter/engine#6926 updated us to a newer clang version
    • reduced Flutter.framework by 35 KB (uncompressed)
    • reduced libflutter.so by 12 KB (uncompressed)
    • reduced overall release APK size by 8 KB (compressed)
  • #25300 removed an unused font from the minimal Flutter app
    • reduced overall release APK size by 62 KB (compressed)

All in all, the download size of the minimal Flutter app APK as reported by apkanalyzer from the Android SDK is now down to 4.06 MB. IPA download size as reported by Apple's App Store Connect for an iPhone X remains at 10.8 MB.

With that, I am now going to close this issue. We've pretty much removed all cruft from a Flutter APK/IPA at this point. Reducing the size further would require more radical changes that we should track in separate issues.

Thanks for following along!

@goderbauer goderbauer closed this Dec 13, 2018
Add-to-App (stale) automation moved this from In progress to Done Dec 13, 2018
@csedeepakyadav
Copy link

@csedeepakyadav csedeepakyadav commented Jul 16, 2019

I would recommend splitting apk to get the best possible reduced app size

my initial apk size was 9.8mb :-

Built build\app\outputs\apk\release\app-release.apk (9.8MB).

After Splitting:-

Built build\app\outputs\apk\release\app-armeabi-v7a-release.apk (4.9MB).
Built build\app\outputs\apk\release\app-arm64-v8a-release.apk (5.2MB).

To split the APKs per ABI, run:

flutter build apk --target-platform android-arm,android-arm64 --split-per-abi
Learn more on: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split

@lock
Copy link

@lock lock bot commented Apr 15, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@lock lock bot locked and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.