flutter-reducing-app-size

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Reducing Flutter App Size

减小Flutter应用大小

Contents

目录

Core Concepts

核心概念

  • Debug vs. Release: Never use debug builds to measure app size. Debug builds include VM overhead and lack Ahead-Of-Time (AOT) compilation and tree-shaking.
  • Upload vs. Download Size: The size of an upload package (APK, AAB, IPA) does not represent the end-user download size. App stores filter redundant native library architectures and asset densities based on the target device.
  • AOT Tree-Shaking: The Dart AOT compiler automatically removes unused or unreachable code in profile and release modes.
  • Size Analysis JSON: The
    --analyze-size
    flag generates a
    *-code-size-analysis_*.json
    file detailing the byte size of packages, libraries, classes, and functions.
  • 调试版 vs. 发布版: 切勿使用调试版来测量应用大小。调试版包含VM开销,且未进行AOT(提前编译)编译和摇树优化。
  • 上传大小 vs. 下载大小: 上传包(APK、AAB、IPA)的大小并不代表最终用户的下载大小。应用商店会根据目标设备过滤冗余的原生库架构和资源密度。
  • AOT摇树优化: Dart AOT编译器会在Profile和发布模式下自动移除未使用或不可达的代码。
  • 大小分析JSON: 使用
    --analyze-size
    参数会生成一个
    *-code-size-analysis_*.json
    文件,详细列出包、库、类和函数的字节大小。

Workflow: Generating Size Analysis Files

工作流:生成大小分析文件

Use this workflow to generate the raw data required for size analysis.
Task Progress:
  • Determine the target platform (apk, appbundle, ios, linux, macos, windows).
  • Run the Flutter build command with the
    --analyze-size
    flag.
  • Locate the generated
    *-code-size-analysis_*.json
    file in the
    build/
    directory.
Conditional Logic:
  • If targeting Android: Run
    flutter build apk --analyze-size
    or
    flutter build appbundle --analyze-size
    .
  • If targeting iOS: Run
    flutter build ios --analyze-size
    . Note: This creates a
    .app
    file useful for relative content sizing, but not for estimating final App Store download size. Use the Estimating iOS Download Size workflow for accurate iOS metrics.
  • If targeting Desktop: Run
    flutter build [windows|macos|linux] --analyze-size
    .
使用此工作流生成大小分析所需的原始数据。
任务进度:
  • 确定目标平台(apk、appbundle、ios、linux、macos、windows)。
  • 运行带有
    --analyze-size
    参数的Flutter构建命令。
  • build/
    目录中找到生成的
    *-code-size-analysis_*.json
    文件。
条件逻辑:
  • 如果目标平台是Android: 运行
    flutter build apk --analyze-size
    flutter build appbundle --analyze-size
  • 如果目标平台是iOS: 运行
    flutter build ios --analyze-size
    注意:此命令会生成一个
    .app
    文件,可用于相对内容大小分析,但无法估算App Store最终下载大小。如需获取准确的iOS指标,请使用估算iOS下载大小工作流。
  • 如果目标平台是桌面端: 运行
    flutter build [windows|macos|linux] --analyze-size

Workflow: Analyzing Size Data in DevTools

工作流:在DevTools中分析大小数据

Use this workflow to visualize and drill down into the Size Analysis JSON.
Task Progress:
  • Launch DevTools by running
    dart devtools
    in the terminal.
  • Select "Open app size tool" from the DevTools landing page.
  • Upload the generated
    *-code-size-analysis_*.json
    file.
  • Inspect the treemap or tree view to identify large packages, libraries, or assets.
  • Feedback Loop:
    1. Identify the largest contributors to app size.
    2. Determine if the dependency or asset is strictly necessary.
    3. Remove, replace, or optimize the identified component.
    4. Regenerate the Size Analysis JSON and compare the new build against the old build using the DevTools "Diff" tab.
使用此工作流可视化并深入分析大小分析JSON文件。
任务进度:
  • 在终端中运行
    dart devtools
    启动DevTools。
  • 从DevTools首页选择“打开应用大小工具”。
  • 上传生成的
    *-code-size-analysis_*.json
    文件。
  • 检查树形图或树状视图,识别体积较大的包、库或资源。
  • 反馈循环:
    1. 找出应用大小的最大贡献者。
    2. 判断该依赖或资源是否是必需的。
    3. 移除、替换或优化识别出的组件。
    4. 重新生成大小分析JSON文件,并使用DevTools的“对比”标签页将新构建版本与旧版本进行比较。

Workflow: Estimating iOS Download Size

工作流:估算iOS下载大小

Use this workflow to get an accurate projection of iOS download and installation sizes across different devices.
Task Progress:
  • Configure the app version and build number in
    pubspec.yaml
    .
  • Generate an Xcode archive by running
    flutter build ipa --export-method development
    .
  • Open the generated archive (
    build/ios/archive/*.xcarchive
    ) in Xcode.
  • Click Distribute App and select Development.
  • In the App Thinning configuration, select All compatible device variants.
  • Check the option to Strip Swift symbols.
  • Sign and export the IPA.
  • Open the exported directory and review the
    App Thinning Size Report.txt
    file to evaluate projected sizes per device.
使用此工作流获取不同设备上iOS下载和安装大小的准确预测。
任务进度:
  • pubspec.yaml
    中配置应用版本和构建号。
  • 运行
    flutter build ipa --export-method development
    生成Xcode归档文件。
  • 在Xcode中打开生成的归档文件(
    build/ios/archive/*.xcarchive
    )。
  • 点击分发应用并选择开发
  • 在App瘦身配置中,选择所有兼容设备变体
  • 勾选剥离Swift符号选项。
  • 签名并导出IPA文件。
  • 打开导出的目录,查看
    App Thinning Size Report.txt
    文件,评估各设备的预测大小。

Workflow: Implementing Size Reduction Strategies

工作流:实施大小缩减策略

Apply these strategies to actively reduce the compiled footprint of the application.
Task Progress:
  • Split Debug Info: Strip debug symbols from the compiled binary and store them in separate files.
  • Remove Unused Resources: Audit the
    pubspec.yaml
    and
    assets/
    directory. Delete any images, fonts, or files not actively referenced in the codebase.
  • Minimize Library Resources: Review third-party packages. If a package imports massive resource files (e.g., large icon sets or localization files) but only a fraction is used, consider alternative packages or custom implementations.
  • Compress Media: Compress all PNG and JPEG assets using tools like
    pngquant
    ,
    imageoptim
    , or WebP conversion before bundling them into the app.
应用以下策略来主动减小应用的编译体积。
任务进度:
  • 拆分调试信息: 从编译后的二进制文件中剥离调试符号,并将其存储在单独的文件中。
  • 移除未使用资源: 审核
    pubspec.yaml
    assets/
    目录。删除代码库中未主动引用的图片、字体或文件。
  • 最小化库资源: 审查第三方包。如果某个包导入了大量资源文件(例如大型图标集或本地化文件)但仅使用了其中一小部分,请考虑使用替代包或自定义实现。
  • 压缩媒体资源: 在将PNG和JPEG资源打包到应用之前,使用
    pngquant
    imageoptim
    或WebP转换等工具进行压缩。

Examples

示例

Generating Size Analysis (Android)

生成大小分析(Android)

bash
undefined
bash
undefined

Generate the size analysis JSON for an Android App Bundle

为Android App Bundle生成大小分析JSON

flutter build appbundle --analyze-size --target-platform=android-arm64
undefined
flutter build appbundle --analyze-size --target-platform=android-arm64
undefined

Splitting Debug Info (Release Build)

拆分调试信息(发布构建)

bash
undefined
bash
undefined

Build an APK while stripping debug info to reduce binary size

构建APK时剥离调试信息以减小二进制大小

flutter build apk --obfuscate --split-debug-info=build/app/outputs/symbols
undefined
flutter build apk --obfuscate --split-debug-info=build/app/outputs/symbols
undefined

Reading the iOS App Thinning Size Report

读取iOS App瘦身大小报告

When reviewing
App Thinning Size Report.txt
, look for the specific target device to understand the true impact on the user:
text
Variant: Runner-7433FC8E-1DF4-4299-A7E8-E00768671BEB.ipa
Supported variant descriptors: [device: iPhone12,1, 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
Interpretation: The end-user download size (compressed) is 5.4 MB, and the on-device footprint (uncompressed) is 13.7 MB.
查看
App Thinning Size Report.txt
时,找到特定目标设备以了解对用户的实际影响:
text
Variant: Runner-7433FC8E-1DF4-4299-A7E8-E00768671BEB.ipa
Supported variant descriptors: [device: iPhone12,1, 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
解读:最终用户的下载大小(压缩后)为5.4 MB,设备上的占用空间(未压缩)为13.7 MB。