hi-client/package_single_exe.md
shanshanzhong b86d645acf
Some checks failed
Build Windows / 编译 libcore (Windows) (20.15.1) (push) Successful in 19m27s
Build Windows / build (push) Failing after 7h11m30s
feat(ci): 添加 Windows 单文件打包工作流
添加 Enigma Virtual Box 和 7-Zip 备选方案,用于将 Flutter Windows 应用打包为单文件 EXE
包含自动化脚本和文档说明,优化构建产物分发
2025-11-09 21:18:52 -08:00

4.0 KiB
Raw Blame History

Flutter Windows 单文件 EXE 打包指南

🎯 目标

将 Flutter Windows 应用打包成单个可执行文件,便于分发和部署。

📋 当前状态

现有构建输出

build/windows/x64/runner/Release/
├── hostexecutor.exe      # 主程序
├── flutter_windows.dll   # Flutter 引擎
├── msvcp140.dll         # Visual C++ 运行时
├── vcruntime140.dll     # Visual C++ 运行时
├── vcruntime140_1.dll   # Visual C++ 运行时
└── data/                # 应用数据文件夹
    ├── app.so           # Dart 代码编译结果
    └── flutter_assets/   # 资源文件

问题分析

Flutter 默认构建会生成多个文件,因为:

  1. Flutter 引擎 (flutter_windows.dll) - 必须包含
  2. Visual C++ 运行时 - 系统依赖
  3. 应用数据 (data 文件夹) - 包含资源和 Dart 代码

🔧 解决方案

方案一:使用 Enigma Virtual Box推荐

步骤 1下载安装

  1. 下载 Enigma Virtual Box
  2. 安装并运行

步骤 2打包配置

  1. 主程序文件: 选择 build/windows/x64/runner/Release/hostexecutor.exe
  2. 添加文件夹: 选择整个 Release 文件夹
  3. 输出文件: 设置输出路径和文件名
  4. 文件选项: 勾选"压缩文件"

步骤 3生成单文件

点击"Process"生成单个可执行文件。

方案二:使用 Inno Setup安装程序

步骤 1下载安装

  1. 下载 Inno Setup
  2. 安装并运行

步骤 2创建脚本

[Setup]
AppName=HostExecutor
AppVersion=1.0.0
DefaultDirName={autopf}\HostExecutor
OutputBaseFilename=HostExecutor_Setup

[Files]
Source: "build\windows\x64\runner\Release\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs

[Icons]
Name: "{group}\HostExecutor"; Filename: "{app}\hostexecutor.exe"

步骤 3编译生成

编译脚本生成安装程序。

方案三:使用 Windows 自带工具(高级)

使用 dotnet publish(需要 .NET 包装)

# 需要创建 .NET 包装器项目
dotnet publish -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true

🚀 自动化脚本

Enigma Virtual Box 自动化脚本

# package_single_exe.ps1
$enigmaPath = "C:\Program Files\Enigma Virtual Box\enigmavb.exe"
$inputFile = "build\windows\x64\runner\Release\hostexecutor.exe"
$outputFile = "dist\HostExecutor_Single.exe"
$folderPath = "build\windows\x64\runner\Release"

& $enigmaPath /sf $inputFile /lf $outputFile /folder $folderPath /compress

📦 打包后文件结构

单文件输出

dist/
└── HostExecutor_Single.exe  # 单个可执行文件50-100MB

文件大小分析

  • 原始文件: 约 30-50MB
  • 压缩后: 约 25-40MB取决于压缩率
  • 最终大小: 50-100MB包含所有依赖

⚠️ 注意事项

运行时依赖

  1. Windows 版本: Windows 10 版本 1903+ 或 Windows 11
  2. 系统组件: 确保目标系统有最新的 Windows 更新
  3. 防病毒软件: 单文件可能被误报,需要添加信任

性能影响

  • 启动时间: 单文件启动会稍慢(需要解压)
  • 内存使用: 运行时内存占用相同
  • 文件大小: 比多文件版本大约 10-20%

🎯 推荐方案

开发阶段

使用多文件版本,便于调试和更新。

分发阶段

使用 Enigma Virtual Box 创建单文件版本:

  1. 简单易用 - 图形化界面
  2. 压缩率高 - 有效减小文件大小
  3. 兼容性好 - 支持所有 Windows 版本
  4. 免费 - 个人和商业使用都免费

📋 下一步行动

  1. 安装 Enigma Virtual Box
  2. 测试单文件打包
  3. 验证运行效果
  4. 创建自动化打包流程

🔗 相关资源