测量你的应用体积

许多开发者都会关注应用编译后的大小。Flutter 应用编译出的 APK、app bundle 和 IPA 均持有应用运行需要的所有代码和资源,是完全独立的。一个应用越大,在设备上占用的空间就越多,下载时间就越长,还可能超出 Android 即时应用等实用功能的限制。

Many developers are concerned with the size of their compiled app. As the APK, app bundle, or IPA version of a Flutter app is self-contained and holds all the code and assets needed to run the app, its size can be a concern. The larger an app, the more space it requires on a device, the longer it takes to download, and it may break the limit of useful features like Android instant apps.

调试版本不具有代表性

Debug builds are not representative

默认情况下,使用 flutter run 命令启动应用,或者点击 IDE 的 Play 按钮(如 开发体验初探编写第一个 Flutter 应用 中所使用的),会生成 Flutter 应用的 调试 版本。调试版本体积很大,用于热重载和源码调试。因此,它不能代表用户最终下载的正式版本的应用。

By default, launching your app with flutter run, or by clicking the Play button in your IDE (as used in Test drive and Write your first Flutter app), generates a debug build of the Flutter app. The app size of a debug build is large due to the debugging overhead that allows for hot reload and source-level debugging. As such, it is not representative of a production app end users download.

检查总大小

Checking the total size

flutter build apkflutter build ios 等生成的默认发行版本,是为了方便在 Play 商店和 App Store 上组装你上传的应用包。因此,它们也无法代表你的用户最终下载的大小。应用商店通常会针对不同的下载程序及其硬件,重新处理和拆分你上传的应用包,例如根据手机的 DPI 过滤资源、根据手机的 CPU 架构过滤原生库。

A default release build, such as one created by flutter build apk or flutter build ios, is built to conveniently assemble your upload package to the Play Store and App Store. As such, they’re also not representative of your end-users’ download size. The stores generally reprocess and split your upload package to target the specific downloader and the downloader’s hardware, such as filtering for assets targeting the phone’s DPI, filtering native libraries targeting the phone’s CPU architecture.

估算总大小

Estimating total size

请使用以下指南,获取各个平台下最接近的估算大小。

To get the closest approximate size on each platform, use the following instructions.

Android

根据 Google 的 Play 控制台说明 来查看应用的下载大小。

Follow the Google Play Console’s instructions for checking app download and install sizes.

生成你的应用的上传包:

Produce an upload package for your application:

flutter build appbundle

登录你的 Google Play 控制台。通过拖放 .abb 文件来上传应用的二进制文件。

Log into your Google Play Console. Upload your application binary by drag dropping the .aab file.

Android vitals -> App size 选项卡中查看应用的下载和安装大小。

View the application’s download and install size in the Android vitals -> App size tab.

App size tab in Google Play Console

该下载大小是基于 XXXHDPI (~640dpi) 且架构为 arm64-v8a 的设备来计算的。用户最终的下载大小可能因硬件而异。

The download size is calculated based on an XXXHDPI (~640dpi) device on an arm64-v8a architecture. Your end users’ download sizes may vary depending on their hardware.

顶部选项卡有一个切换下载大小和安装大小的开关。该页面还包含了进一步的优化提示。

The top tab has a toggle for download size and install size. The page also contains optimization tips further below.

iOS

创建一份 Xcode 应用大小报告

Create an Xcode App Size Report.

首先,参照 iOS 创建构建归档指南,配置应用的版本,并开始构建。

First, by configuring the app version and build as described in the iOS create build archive instructions.

然后:

Then:

  1. 选择 Product > Archive 生成一个构建归档。

    Select Product > Archive to produce a build archive.

  2. 在 Xcode Organizer 窗口的侧栏,选择你的 iOS 应用,然后选择你刚刚生成的构建归档。

    In the sidebar of the Xcode Organizer window, select your iOS app, then select the build archive you just produced.

  3. 点击 Distribute App

    Click Distribute App.

  4. 选择一种发布方式。如果你不打算发布该应用,Development 模式是最简单的。

    Select a method of distribution. Development is the simplest if you don’t intend to distribute the application.

  5. App Thinning 中,选择「all compatible device variants」。

    In App Thinning, select ‘all compatible device variants’.

  6. 选择 Rebuild from Bitcode(如果你的项目启用了 bitcode)。

    Select Rebuild from Bitcode (available if bitcode is enabled on your project).

  7. 选择 Strip Swift symbols

    Select Strip Swift symbols.

签名并导出 IPA 包。导出目录中有一个「App Thinning Size Report.txt」文件,其中记录了在不同设备和 iOS 版本上预估的应用程序大小的详细信息。

Sign and export the IPA. The exported directory contains App Thinning Size Report.txt with details about your projected application size on different devices and versions of iOS.

Flutter 1.17 上的默认 demo app 的应用大小报告显示如下:

The App Size Report for the default demo app in Flutter 1.17 shows:

Variant: Runner-7433FC8E-1DF4-4299-A7E8-E00768671BEB.ipa
Supported variant descriptors: [device: iPhone12,1, os-version: 13.0] and [device: iPhone11,8, os-version: 13.0]
App + On Demand Resources size: 5.4 MB compressed, 13.7 MB uncompressed
App size: 5.4 MB compressed, 13.7 MB uncompressed
On Demand Resources size: Zero KB compressed, Zero KB uncompressed

在这个例子中,设备 iPhone12,1(iPhone 11 的 Model ID / Hardware number)和 iPhone11,8 (iPhone XR) 运行在 iOS 13.0 版本下时,下载大小约为 5.4 MB,安装大小约为 13.7 MB。

In this example, the app has an approximate download size of 5.4 MB and an approximate install size of 13.7 MB on an iPhone12,1 (Model ID / Hardware number for iPhone 11) and iPhone11,8 (iPhone XR) running iOS 13.0.

想要精确测量一个 iOS 应用的体积,你需要先将一个发行版本的 IPA 包上传至 App Store Connect(简介),再获取它的大小报告。 IPA 包一般都比 APK 包要大,这在 Flutter FAQ 中的 Flutter 引擎有多大? 一节中已经阐述过了。

To measure an iOS app exactly, you have to upload a release IPA to Apple’s App Store Connect (instructions) and obtain the size report from there. IPAs are commonly larger than APKs as explained in How big is the Flutter engine?, a section in the Flutter FAQ.

大小拆分

Breaking down the size

从 Flutter 1.22 和 DevTools 0.9.1 版本开始,包含了一个大小分析工具,帮助开发者了解和拆分应用的发行版本。

Starting in Flutter version 1.22 and DevTools version 0.9.1, a size analysis tool is included to help developers understand the breakdown of the release build of their application.

该大小分析工具通过在构建时添加 --analyze-size 标记来调用:

The size analysis tool is invoked by passing the --analyze-size flag when building:

  • flutter build apk --analyze-size
  • flutter build appbundle --analyze-size
  • flutter build ios --analyze-size
  • flutter build linux --analyze-size
  • flutter build macos --analyze-size
  • flutter build windows --analyze-size

这种构建模式和标准的发行构建相比,有以下两方面的区别:

This build is different from a standard release build in two ways.

  1. 该工具编译 Dart 时,记录了 Dart 包的代码大小使用情况。

    The tool compiles Dart in a way that records code size usage of Dart packages.

  2. 该工具在终端上展示了大小拆分的摘要信息,并在 DevTools 中生成了一个 *-code-size-analysis_*.json 文件,用于进行更详细的分析。

    The tool displays a high level summary of the size breakdown in the terminal, and leaves a *-code-size-analysis_*.json file for more detailed analysis in DevTools.

除了分析单个构建,你还可以在 DevTools 中加载两个 *-code-size-analysis_*.json 文件比较差异。详情请阅读 DevTools 文档

In addition to analyzing a single build, two builds can also be diffed by loading two *-code-size-analysis_*.json files into DevTools. See DevTools documentation for details.

Size summary of an Android application in terminal

通过总结,你可以快速了解每种类型(例如资源、原生代码、Flutter 库等)的大小使用情况。编译后的 Dart 原生库会按包进一步拆分,以便快速分析。

Through the summary, you can get a quick idea of the size usage per category (such as asset, native code, Flutter libraries, etc). The compiled Dart native library is further broken down by package for quick analysis.

在 DevTools 中深入分析

Deeper analysis in DevTools

上面生成的 *-code-size-analysis_*.json 文件可以在 DevTools 中进一步深入分析,树和树状图可以将应用内容分割至单文件级别,也可以达到 Dart AOT 产物的函数级别。

The *-code-size-analysis_*.json file produced above can be further analyzed in deeper detail in DevTools where a tree or a treemap view can break down the contents of the application into the individual file level and up to function level for the Dart AOT artifact.

可以通过 flutter pub global run devtools 打开 DevTools,选择 Open app size tool,然后上传 JSON 文件。

This can be done by flutter pub global run devtools, selecting Open app size tool and uploading the JSON file.

Example breakdown of app in DevTools

更多关于 DevTools 中应用大小工具的使用,请看 DevTools 文档

For further information on using the DevTools app size tool, see DevTools documentation.

减少应用大小

Reducing app size

当构建应用的发行版本时,考虑使用 --split-debug-info 标记。该标记会显著减少代码量。关于使用此标记的示例,请查看文档 Obfuscating Dart code

When building a release version of your app, consider using the --split-debug-info tag. This tag can dramatically reduce code size. For an example of using this tag, see Obfuscating Dart code.

其他减少应用大小的方式:

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

  • 删除无用的资源

    Remove unused resources

  • 尽量减少从库中引入的资源

    Minimize resource imported from libraries

  • 压缩 PNG 和 JPEG 文件

    Compress PNG and JPEG files