flutter-app-size
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseflutter-app-size-optimization
flutter-app-size-optimization
Goal
目标
Analyzes and optimizes Flutter application size by measuring build artifacts, generating size analysis reports, utilizing Dart DevTools for component breakdown, and implementing specific size reduction strategies such as debug info splitting, resource compression, and platform-specific tree-shaking. Assumes a configured Flutter environment and target platform availability.
通过测量构建产物、生成体积分析报告、使用Dart DevTools进行组件拆解,以及实施调试信息拆分、资源压缩、平台专属tree-shaking等特定体积缩减策略,来分析和优化Flutter应用的体积。本指南默认你已配置好Flutter环境,且目标平台可用。
Decision Logic
决策逻辑
Use the following decision tree to determine the correct measurement and optimization path:
- Is the user measuring Android or Desktop (Linux/macOS/Windows)?
- Yes: Run . Proceed to DevTools analysis.
flutter build <platform> --analyze-size - No: Proceed to step 2.
- Yes: Run
- Is the user measuring iOS?
- Yes: Run and generate an Xcode App Thinning Size Report for accurate download estimates.
flutter build ipa --export-method development
- Yes: Run
- Is the user analyzing the breakdown of components?
- Yes: Launch , open the App Size Tool, and upload the generated
dart devtoolsfile.*-code-size-analysis_*.json
- Yes: Launch
- Is the user applying size reduction strategies?
- Yes: Apply , compress assets, and implement
--split-debug-infochecks for aggressive tree-shaking.Platform
- Yes: Apply
使用如下决策树来确定正确的测量与优化路径:
- 用户是否要测量Android或桌面端(Linux/macOS/Windows)的包体积?
- 是: 运行 ,后续进行DevTools分析。
flutter build <platform> --analyze-size - 否: 进入步骤2。
- 是: 运行
- 用户是否要测量iOS的包体积?
- 是: 运行 ,生成Xcode App Thinning Size Report来获取准确的下载体积预估。
flutter build ipa --export-method development
- 是: 运行
- 用户是否要分析组件的体积拆解?
- 是: 启动 ,打开App Size Tool,上传生成的
dart devtools文件。*-code-size-analysis_*.json
- 是: 启动
- 用户是否要应用体积缩减策略?
- 是: 启用 、压缩资源、添加
--split-debug-info判断来实现更彻底的tree-shaking。Platform
- 是: 启用
Instructions
使用指南
-
Determine Target Platform and Baseline STOP AND ASK THE USER: "Which platform (apk, appbundle, ios, linux, macos, windows) are you targeting for size optimization, and do you have a specific size reduction goal?"
-
Generate Size Analysis File Execute the Flutter build command with theflag to compile Dart with code size usage recording.
--analyze-sizebash# Replace <platform> with apk, appbundle, ios, linux, macos, or windows flutter build <platform> --analyze-sizeNote: This generates afile in the*-code-size-analysis_*.jsondirectory and prints a high-level summary to the terminal.build/ -
Generate iOS App Size Report (iOS Only) If targeting iOS, thefile generated by
.apponly evaluates relative size. For an accurate end-user download estimate, generate an Xcode App Size Report.--analyze-sizebashflutter build ipa --export-method development- Follow up by instructing the user to:
- Open the archive () in Xcode.
build/ios/archive/*.xcarchive - Click Distribute App -> Development.
- In App Thinning, select "all compatible device variants".
- Select Strip Swift symbols.
- Export the IPA and review the .
App Thinning Size Report.txt
- Open the archive (
- Follow up by instructing the user to:
-
Analyze Components in DevTools Launch DevTools to inspect the JSON analysis file.bash
dart devtools- Instruct the user to open the "App Size Tool" in the DevTools browser UI and upload the file.
*-code-size-analysis_*.json - Use the Treemap or Dominator Tree to identify large packages, libraries, or assets.
- Instruct the user to open the "App Size Tool" in the DevTools browser UI and upload the
-
Implement Size Reduction Strategies Apply the following strategies to the build process and codebase:Strategy A: Split Debug Info and Obfuscate Extract debug symbols from the compiled binary to reduce the final artifact size.bash
flutter build appbundle --obfuscate --split-debug-info=build/app/outputs/symbols/Strategy B: Platform-Specific Tree Shaking UsePlatform checks to ensure the Dart AOT compiler removes unreachable code for the target platform.dart:iodartimport 'dart:io'; void initializePlatformSpecificFeatures() { if (Platform.isWindows) { // Windows-specific imports and logic // The AOT compiler will strip this out when building for Android/iOS _initWindowsFeatures(); } else if (Platform.isAndroid) { _initAndroidFeatures(); } } -
Validate and Fix After applying reduction strategies, regenerate the size analysis file.bash
flutter build <platform> --analyze-sizeSTOP AND ASK THE USER: "Please upload the newfile to DevTools and compare it with the original using the 'Diff' tab. Did the app size decrease to meet your requirements?"*-code-size-analysis_*.json- Error State: If the size did not decrease, verify that was applied correctly and that large assets (PNG/JPEG) have been compressed or removed.
--split-debug-info
- Error State: If the size did not decrease, verify that
-
确定目标平台和基准线 请先询问用户: "你要优化哪个平台(apk、appbundle、ios、linux、macos、windows)的包体积,是否有明确的体积缩减目标?"
-
生成立体积分析文件 携带参数执行Flutter构建命令,编译Dart时会记录代码体积使用情况。
--analyze-sizebash# 将<platform>替换为apk、appbundle、ios、linux、macos或windows flutter build <platform> --analyze-size注意:该命令会在目录下生成一个build/文件,同时会在终端输出高层次的体积汇总信息。*-code-size-analysis_*.json -
生成iOS应用体积报告(仅iOS需要) 如果目标平台是iOS,生成的
--analyze-size文件仅能评估相对体积。要获取最终用户下载体积的准确预估,需要生成Xcode App Thinning Size Report。.appbashflutter build ipa --export-method development- 后续引导用户执行如下操作:
- 在Xcode中打开归档文件()。
build/ios/archive/*.xcarchive - 点击 Distribute App -> Development。
- 在 App Thinning 选项中,选择"all compatible device variants"。
- 勾选 Strip Swift symbols。
- 导出IPA,查看文件。
App Thinning Size Report.txt
- 在Xcode中打开归档文件(
- 后续引导用户执行如下操作:
-
在DevTools中分析组件 启动DevTools来查看JSON分析文件的内容。bash
dart devtools- 引导用户在DevTools的浏览器界面中打开"App Size Tool",上传文件。
*-code-size-analysis_*.json - 使用Treemap或Dominator Tree来识别体积较大的包、库或者资源文件。
- 引导用户在DevTools的浏览器界面中打开"App Size Tool",上传
-
实施体积缩减策略 在构建流程和代码库中应用如下策略:策略A:拆分调试信息+代码混淆 从编译后的二进制文件中提取调试符号,缩减最终产物的体积。bash
flutter build appbundle --obfuscate --split-debug-info=build/app/outputs/symbols/策略B:平台专属Tree Shaking 使用的Platform判断,确保Dart AOT编译器会移除目标平台下不会执行的无效代码。dart:iodartimport 'dart:io'; void initializePlatformSpecificFeatures() { if (Platform.isWindows) { // Windows专属的导入和逻辑 // 编译Android/iOS版本时AOT编译器会自动剔除这部分代码 _initWindowsFeatures(); } else if (Platform.isAndroid) { _initAndroidFeatures(); } } -
验证与修复 应用缩减策略后,重新生成立体积分析文件。bash
flutter build <platform> --analyze-size请询问用户: "请将新的文件上传到DevTools,通过'Diff'标签页和原始报告对比。应用体积是否已经降低到符合你的要求?"*-code-size-analysis_*.json- 异常情况: 如果体积没有下降,请检查是否正确配置,以及大体积资源文件(PNG/JPEG)是否已经压缩或移除。
--split-debug-info
- 异常情况: 如果体积没有下降,请检查
Constraints
注意限制
- NEVER use debug builds (or standard IDE play buttons) to measure app size. They contain debugging overhead and are not representative of production sizes.
flutter run - NEVER assume the upload package size (e.g., a universal APK or AAB) is the exact download size for end-users. App stores filter native libraries and asset densities.
- ALWAYS provide a valid directory path when using the flag.
--split-debug-info - DO NOT rely solely on the iOS file size; always use the Xcode App Thinning Size Report for accurate iOS download size estimates.
.app - ALWAYS ensure that checks use
Platformconstants directly so the AOT compiler can accurately perform tree-shaking.dart:io
- 绝对不要 使用Debug构建(或者IDE默认的运行按钮)来测量应用体积,这类构建包含调试冗余内容,无法代表生产环境的真实体积。
flutter run - 绝对不要 认为上传的包体积(比如通用APK或者AAB)就是最终用户的实际下载体积,应用商店会过滤原生库和适配不同密度的资源。
- 务必 在使用参数时提供有效的目录路径。
--split-debug-info - 不要 仅参考iOS 文件的体积,要获取准确的iOS下载体积预估请始终使用Xcode App Thinning Size Report。
.app - 务必 确保判断直接使用
Platform的常量,这样AOT编译器才能准确执行tree-shaking。dart:io