flutter-reducing-app-size
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReducing 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 flag generates a
--analyze-sizefile detailing the byte size of packages, libraries, classes, and functions.*-code-size-analysis_*.json
- 调试版 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 flag.
--analyze-size - Locate the generated file in the
*-code-size-analysis_*.jsondirectory.build/
Conditional Logic:
- If targeting Android: Run or
flutter build apk --analyze-size.flutter build appbundle --analyze-size - If targeting iOS: Run . Note: This creates a
flutter build ios --analyze-sizefile 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..app - If targeting Desktop: Run .
flutter build [windows|macos|linux] --analyze-size
使用此工作流生成大小分析所需的原始数据。
任务进度:
- 确定目标平台(apk、appbundle、ios、linux、macos、windows)。
- 运行带有参数的Flutter构建命令。
--analyze-size - 在目录中找到生成的
build/文件。*-code-size-analysis_*.json
条件逻辑:
- 如果目标平台是Android: 运行或
flutter build apk --analyze-size。flutter build appbundle --analyze-size - 如果目标平台是iOS: 运行。注意:此命令会生成一个
flutter build ios --analyze-size文件,可用于相对内容大小分析,但无法估算App Store最终下载大小。如需获取准确的iOS指标,请使用估算iOS下载大小工作流。.app - 如果目标平台是桌面端: 运行。
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 in the terminal.
dart devtools - Select "Open app size tool" from the DevTools landing page.
- Upload the generated file.
*-code-size-analysis_*.json - Inspect the treemap or tree view to identify large packages, libraries, or assets.
- Feedback Loop:
- Identify the largest contributors to app size.
- Determine if the dependency or asset is strictly necessary.
- Remove, replace, or optimize the identified component.
- Regenerate the Size Analysis JSON and compare the new build against the old build using the DevTools "Diff" tab.
使用此工作流可视化并深入分析大小分析JSON文件。
任务进度:
- 在终端中运行启动DevTools。
dart devtools - 从DevTools首页选择“打开应用大小工具”。
- 上传生成的文件。
*-code-size-analysis_*.json - 检查树形图或树状视图,识别体积较大的包、库或资源。
- 反馈循环:
- 找出应用大小的最大贡献者。
- 判断该依赖或资源是否是必需的。
- 移除、替换或优化识别出的组件。
- 重新生成大小分析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 () in Xcode.
build/ios/archive/*.xcarchive - 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 file to evaluate projected sizes per device.
App Thinning Size Report.txt
使用此工作流获取不同设备上iOS下载和安装大小的准确预测。
任务进度:
- 在中配置应用版本和构建号。
pubspec.yaml - 运行生成Xcode归档文件。
flutter build ipa --export-method development - 在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 and
pubspec.yamldirectory. Delete any images, fonts, or files not actively referenced in the codebase.assets/ - 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, or WebP conversion before bundling them into the app.imageoptim
应用以下策略来主动减小应用的编译体积。
任务进度:
- 拆分调试信息: 从编译后的二进制文件中剥离调试符号,并将其存储在单独的文件中。
- 移除未使用资源: 审核和
pubspec.yaml目录。删除代码库中未主动引用的图片、字体或文件。assets/ - 最小化库资源: 审查第三方包。如果某个包导入了大量资源文件(例如大型图标集或本地化文件)但仅使用了其中一小部分,请考虑使用替代包或自定义实现。
- 压缩媒体资源: 在将PNG和JPEG资源打包到应用之前,使用、
pngquant或WebP转换等工具进行压缩。imageoptim
Examples
示例
Generating Size Analysis (Android)
生成大小分析(Android)
bash
undefinedbash
undefinedGenerate the size analysis JSON for an Android App Bundle
为Android App Bundle生成大小分析JSON
flutter build appbundle --analyze-size --target-platform=android-arm64
undefinedflutter build appbundle --analyze-size --target-platform=android-arm64
undefinedSplitting Debug Info (Release Build)
拆分调试信息(发布构建)
bash
undefinedbash
undefinedBuild an APK while stripping debug info to reduce binary size
构建APK时剥离调试信息以减小二进制大小
flutter build apk --obfuscate --split-debug-info=build/app/outputs/symbols
undefinedflutter build apk --obfuscate --split-debug-info=build/app/outputs/symbols
undefinedReading the iOS App Thinning Size Report
读取iOS App瘦身大小报告
When reviewing , look for the specific target device to understand the true impact on the user:
App Thinning Size Report.txttext
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 uncompressedInterpretation: The end-user download size (compressed) is 5.4 MB, and the on-device footprint (uncompressed) is 13.7 MB.
查看时,找到特定目标设备以了解对用户的实际影响:
App Thinning Size Report.txttext
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。