flutter-app-size

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

flutter-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:
  1. Is the user measuring Android or Desktop (Linux/macOS/Windows)?
    • Yes: Run
      flutter build <platform> --analyze-size
      . Proceed to DevTools analysis.
    • No: Proceed to step 2.
  2. Is the user measuring iOS?
    • Yes: Run
      flutter build ipa --export-method development
      and generate an Xcode App Thinning Size Report for accurate download estimates.
  3. Is the user analyzing the breakdown of components?
    • Yes: Launch
      dart devtools
      , open the App Size Tool, and upload the generated
      *-code-size-analysis_*.json
      file.
  4. Is the user applying size reduction strategies?
    • Yes: Apply
      --split-debug-info
      , compress assets, and implement
      Platform
      checks for aggressive tree-shaking.
使用如下决策树来确定正确的测量与优化路径:
  1. 用户是否要测量Android或桌面端(Linux/macOS/Windows)的包体积?
    • 是: 运行
      flutter build <platform> --analyze-size
      ,后续进行DevTools分析。
    • 否: 进入步骤2。
  2. 用户是否要测量iOS的包体积?
    • 是: 运行
      flutter build ipa --export-method development
      ,生成Xcode App Thinning Size Report来获取准确的下载体积预估。
  3. 用户是否要分析组件的体积拆解?
    • 是: 启动
      dart devtools
      ,打开App Size Tool,上传生成的
      *-code-size-analysis_*.json
      文件。
  4. 用户是否要应用体积缩减策略?
    • 是: 启用
      --split-debug-info
      、压缩资源、添加
      Platform
      判断来实现更彻底的tree-shaking。

Instructions

使用指南

  1. 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?"
  2. Generate Size Analysis File Execute the Flutter build command with the
    --analyze-size
    flag to compile Dart with code size usage recording.
    bash
    # Replace <platform> with apk, appbundle, ios, linux, macos, or windows
    flutter build <platform> --analyze-size
    Note: This generates a
    *-code-size-analysis_*.json
    file in the
    build/
    directory and prints a high-level summary to the terminal.
  3. Generate iOS App Size Report (iOS Only) If targeting iOS, the
    .app
    file generated by
    --analyze-size
    only evaluates relative size. For an accurate end-user download estimate, generate an Xcode App Size Report.
    bash
    flutter build ipa --export-method development
    • Follow up by instructing the user to:
      1. Open the archive (
        build/ios/archive/*.xcarchive
        ) in Xcode.
      2. Click Distribute App -> Development.
      3. In App Thinning, select "all compatible device variants".
      4. Select Strip Swift symbols.
      5. Export the IPA and review the
        App Thinning Size Report.txt
        .
  4. 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
      *-code-size-analysis_*.json
      file.
    • Use the Treemap or Dominator Tree to identify large packages, libraries, or assets.
  5. 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 Use
    dart:io
    Platform checks to ensure the Dart AOT compiler removes unreachable code for the target platform.
    dart
    import '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();
      }
    }
  6. Validate and Fix After applying reduction strategies, regenerate the size analysis file.
    bash
    flutter build <platform> --analyze-size
    STOP AND ASK THE USER: "Please upload the new
    *-code-size-analysis_*.json
    file to DevTools and compare it with the original using the 'Diff' tab. Did the app size decrease to meet your requirements?"
    • Error State: If the size did not decrease, verify that
      --split-debug-info
      was applied correctly and that large assets (PNG/JPEG) have been compressed or removed.
  1. 确定目标平台和基准线 请先询问用户: "你要优化哪个平台(apk、appbundle、ios、linux、macos、windows)的包体积,是否有明确的体积缩减目标?"
  2. 生成立体积分析文件 携带
    --analyze-size
    参数执行Flutter构建命令,编译Dart时会记录代码体积使用情况。
    bash
    # 将<platform>替换为apk、appbundle、ios、linux、macos或windows
    flutter build <platform> --analyze-size
    注意:该命令会在
    build/
    目录下生成一个
    *-code-size-analysis_*.json
    文件,同时会在终端输出高层次的体积汇总信息。
  3. 生成iOS应用体积报告(仅iOS需要) 如果目标平台是iOS,
    --analyze-size
    生成的
    .app
    文件仅能评估相对体积。要获取最终用户下载体积的准确预估,需要生成Xcode App Thinning Size Report。
    bash
    flutter build ipa --export-method development
    • 后续引导用户执行如下操作:
      1. 在Xcode中打开归档文件(
        build/ios/archive/*.xcarchive
        )。
      2. 点击 Distribute App -> Development
      3. App Thinning 选项中,选择"all compatible device variants"。
      4. 勾选 Strip Swift symbols
      5. 导出IPA,查看
        App Thinning Size Report.txt
        文件。
  4. 在DevTools中分析组件 启动DevTools来查看JSON分析文件的内容。
    bash
    dart devtools
    • 引导用户在DevTools的浏览器界面中打开"App Size Tool",上传
      *-code-size-analysis_*.json
      文件。
    • 使用Treemap或Dominator Tree来识别体积较大的包、库或者资源文件。
  5. 实施体积缩减策略 在构建流程和代码库中应用如下策略:
    策略A:拆分调试信息+代码混淆 从编译后的二进制文件中提取调试符号,缩减最终产物的体积。
    bash
    flutter build appbundle --obfuscate --split-debug-info=build/app/outputs/symbols/
    策略B:平台专属Tree Shaking 使用
    dart:io
    的Platform判断,确保Dart AOT编译器会移除目标平台下不会执行的无效代码。
    dart
    import 'dart:io';
    
    void initializePlatformSpecificFeatures() {
      if (Platform.isWindows) {
        // Windows专属的导入和逻辑
        // 编译Android/iOS版本时AOT编译器会自动剔除这部分代码
        _initWindowsFeatures();
      } else if (Platform.isAndroid) {
        _initAndroidFeatures();
      }
    }
  6. 验证与修复 应用缩减策略后,重新生成立体积分析文件。
    bash
    flutter build <platform> --analyze-size
    请询问用户: "请将新的
    *-code-size-analysis_*.json
    文件上传到DevTools,通过'Diff'标签页和原始报告对比。应用体积是否已经降低到符合你的要求?"
    • 异常情况: 如果体积没有下降,请检查
      --split-debug-info
      是否正确配置,以及大体积资源文件(PNG/JPEG)是否已经压缩或移除。

Constraints

注意限制

  • NEVER use debug builds (
    flutter run
    or standard IDE play buttons) to measure app size. They contain debugging overhead and are not representative of production sizes.
  • 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
    --split-debug-info
    flag.
  • DO NOT rely solely on the iOS
    .app
    file size; always use the Xcode App Thinning Size Report for accurate iOS download size estimates.
  • ALWAYS ensure that
    Platform
    checks use
    dart:io
    constants directly so the AOT compiler can accurately perform tree-shaking.
  • 绝对不要 使用Debug构建(
    flutter run
    或者IDE默认的运行按钮)来测量应用体积,这类构建包含调试冗余内容,无法代表生产环境的真实体积。
  • 绝对不要 认为上传的包体积(比如通用APK或者AAB)就是最终用户的实际下载体积,应用商店会过滤原生库和适配不同密度的资源。
  • 务必 在使用
    --split-debug-info
    参数时提供有效的目录路径。
  • 不要 仅参考iOS
    .app
    文件的体积,要获取准确的iOS下载体积预估请始终使用Xcode App Thinning Size Report。
  • 务必 确保
    Platform
    判断直接使用
    dart:io
    的常量,这样AOT编译器才能准确执行tree-shaking。