一、 为什么需要打包?
执行 flutter build windows --release 后,我们得到的不是一个单纯的 .exe,而是一个包含:
- 主程序 (
.exe)
- Flutter 引擎 (
flutter_windows.dll)
- 插件库 (
bitsdojo_...dll)
- 资源文件夹 (
data/)
的复杂目录结构。直接把这个文件夹发给客户显得非常不专业,且容易因文件丢失导致无法运行。我们需要将其封装为 Setup Installer (安装包)。
二、 工具选型:Inno Setup
在对比了 InstallShield (昂贵) 和 NSIS (脚本复杂) 后,我选择了 Inno Setup。
- License: 完全免费,支持商业闭源软件使用。
- Features: 脚本驱动 (Script-driven),支持现代 UI,生成产物体积小。
三、 编写打包脚本 (.iss)
在项目根目录下创建 installer_script.iss,以下是适用于 Flutter 项目的标准模板:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| ; 脚本定义:PV Monitor System 安装包配置 ; 版本: v0.0.1
#define MyAppName "PV Monitor System" #define MyAppVersion "0.0.1" #define MyAppPublisher "JingYing Tech" #define MyAppExeName "PV_Monitor.exe" ; 需与 CMake 中修改的一致
[Setup] AppId={{Generate-Your-Own-GUID-Here} AppName={#MyAppName} AppVersion={#MyAppVersion} AppPublisher={#MyAppPublisher} DefaultDirName={autopf}\{#MyAppName} DisableProgramGroupPage=yes OutputDir=.\installer_output OutputBaseFilename=PV_Monitor_Setup_v0.0.1 Compression=lzma SolidCompression=yes SetupIconFile=windows\runner\resources\app_icon.ico WizardStyle=modern
[Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files] ; 核心:递归包含 Release 目录下的所有文件 Source: "build\windows\x64\runner\Release\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion Source: "build\windows\x64\runner\Release\*.dll"; DestDir: "{app}"; Flags: ignoreversion Source: "build\windows\x64\runner\Release\data\*"; DestDir: "{app}\data"; Flags: ignoreversion recursesubdirs createallsubdirs
[Icons] Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}" Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Run] Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#MyAppName}}"; Flags: nowait postinstall skipifsilent **编译方法:** 双击脚本打开 Inno Setup Compiler,点击 **Build -> Compile** 即可生成安装包。
|
四、 绿色便携版 (Portable)
针对不喜欢安装软件的高级用户(或测试场景),可以用 7-Zip 自解压 或纯 Zip 包。
7-Zip SFX 制作法:
选中 Release 目录下所有文件。
右键 -> 7-Zip -> 添加到压缩包。
勾选 “创建自释放 (SFX) 压缩包”。
生成 .exe,双击即自动解压运行。
五、 分发策略 (Distribution)
为了实现从源码到交付的闭环,我们采用 Gitea Release 作为制品库。
Installer: PV_Monitor_Setup_v0.0.1.exe (面向普通客户)
Portable: PV_Monitor_Portable_v0.0.1.zip (面向工程师/调试)
将这两个文件上传到 Gitea 对应 Tag 的附件中,即可完成一次标准的软件发布流程。对于公网下载,可进一步配合阿里云 OSS 或 Nginx 文件服务器进行加速。