测量你的应用体积
Many developers are concerned with the size of theircompiled apps. As the APK, app bundle, or IPA versionof a Flutter app is self contained, and holds all thecode and assets needed to run the app, its sizecan be a concern. The larger an app, the more spaceit requires on a device, and the longer it takes todownload, and it may break the limit of useful featureslike Android instant apps.
By default, launching your app with flutter run
,or by clicking the Play button in your IDE(as used in Test drive andWrite your first Flutter app),generates a debug build of the Flutter app.The app size of a debug build is large due tothe debugging overhead that allows for hot reloadand source level debugging.
To get a better sense of what an end-user actuallyhas to download for a Flutter app, use thefollowing instructions.
Android
Run the following from the top of your Flutter projectto get the size of APK for 32-bit Android devices:
flutter build apk --target-platform=android-arm
The output should look something like the following:
Built build/app/outputs/apk/release/app-release.apk (4.2MB).
For 64-bit Android devices, run the following:
flutter build apk --target-platform=android-arm64
Here is an example output:
Built build/app/outputs/apk/release/app-release.apk (4.6MB).
You can run the following to get 2 APKs,one for 32-bit and one for 64-bit:
flutter build apk --split-per-abi
Here’s some sample output:
Built build/app/outputs/apk/release/app-armeabi-v7a-release.apk (4.2MB).
Built build/app/outputs/apk/release/app-arm64-v8a-release.apk (4.6MB).
请注意 Do not run flutter build apk
directly in Flutter 1.9 (and later), because it generates a fat APK with both 32-bit and 64-bit binaries.
iOS
To get a rough idea of how large your release IPA is,run the following:
flutter build ios && tar -zcf build/app.ipa build/ios/iphoneos/Runner.app && ls -lh build/app.ipa
The resulting IPA file for the example/helloworld
app(as of this writing) is 8.3 MB.
-rw-r--r-- 1 userName primarygroup 8.3M Oct 25 13:47 build/app.ipa
A closer result can be obtained by creating a release archive as described inthe iOS create build archive instructions. If bitcode is enabled on yourproject, you will also have the option to rebuild from bitcode. This optionshould be selected if it is available, to more closely match what the App Storewill produce for your application. You can also select app thinning for aspecific phone architecture, which should be very close to the final IPA sizefrom the store for that device.
To measure an iOS app exactly,you have to upload a release IPA to Apple’sApp Store Connect (instructions)and obtain the size report from there.IPAs are commonly larger than APKs as explainedin How big is the Flutter engine?, asection in the Flutter FAQ.
Reducing app size
Some of the obvious things you can do to make your app smallerare:
- Remove unused resources
- Minimize resource imported from libraries
- Support a limited number of screen densities
- Compress PNG and JPEG files