Flutter使用auto_updater实现windows/mac桌面应用版本升级功能
- 其他
- 2025-08-02 19:51:01

因为windows应用一般大家都是从网上下载的,后期版本肯定会更新,那用flutter开发windows应用,怎么实现应用内版本更新功能了?可以使用auto_updater库, 这个插件允许 Flutter 桌面 应用自动更新自己 (基于 sparkle 和 winsparkle) 地址如下:
pub仓库地址:auto_updater | Flutter Package github仓库地址:https://github.com/leanflutter/auto_updater 中文文档地址:https://github.com/leanflutter/auto_updater/blob/main/README-ZH.md
开始使用先添加依赖,将此添加到你的软件包的 pubspec.yaml 文件:
dependencies: auto_updater: ^0.1.7windows使用需要安装openssl:
安装教程:windows安装Chocolatey方法注意事项,以及安装openssl方法-CSDN博客
然后在main.dart中添加下面代码:
import 'package:auto_updater/auto_updater.dart'; void main() async { // 必须加上这一行。 WidgetsFlutterBinding.ensureInitialized(); String feedURL = 'http://localhost:5002/appcast.xml'; await autoUpdater.setFeedURL(feedURL); await autoUpdater.checkForUpdates(); await autoUpdater.setScheduledCheckInterval(3600); runApp(MyApp()); } 生成私钥运行以下命令:需要分别在 macOS 和 Windows 系统中运行该命令。
dart run auto_updater:generate_keys命令将为你生成私钥(dsa_priv.pem)及公钥(dsa_pub.pem),请备份你的私钥并确保其安全,并将公钥作为 Windows 资源添加到项目中。
macOS运行命令后输出:
A key has been generated and saved in your keychain. Add the `SUPublicEDKey` key to the Info.plist of each app for which you intend to use Sparkle for distributing updates. It should appear like this: <key>SUPublicEDKey</key> <string>pfIShU4dEXqPd5ObYNfDBiQWcXozk7estwzTnF9BamQ=</string>更改文件 macos/Runner/Info.plist 如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> ... + <key>SUPublicEDKey</key> + <string>bHaXClrRGMmKoKP/3HJnr/jn2ODTRPAM3VZhhkI9ZvY=</string> </dict> </plist>Windows运行命令后输出:
Generated two files: dsa_priv.pem: your private key. Keep it secret and don't share it! dsa_pub.pem: public counterpart to include in youe app. BACK UP YOUR PRIVATE KEY AND KEEP IT SAFE! If you lose it, your users will be unable to upgrade!更改文件 windows/runner/Runner.rc 如下:
... +/ +// +// WinSparkle +// +// And verify signature using DSA public key: +DSAPub DSAPEM "../../dsa_pub.pem" 打包应用为了简化打包的过程,这里使用了 Flutter Distributor ,一个专门用于打包和发布 Flutter 应用的完整工具。官方网站:Getting Started | Flutter Distributor
安装flutter_distributor:
#安装命令 dart pub global activate flutter_distributor 安装Inno setup必须安装Inno Setup 6 并且添加中文语言包,且安装目录要使用默认的C盘里面,否则打包失败。因为Flutter Distributor打包exe是基于inno setup 6实现的,默认官网是英文版本的,打包exe之后点击安装都是英文,所以我找到了中文版的inno setup 6和中文语言包,下载地址如下
Inno Setup 6和中文安装包下载地址:inno setup 带中文包.zip - 蓝奏云
添加distribute_options.yaml添加到你的项目根目录
output: dist/ releases: - name: dev jobs: - name: release-windows package: platform: windows target: exe build_args: dart-define: APP_ENV: dev添加 make_config.yaml 到你的项目 windows/packaging/exe (需要手动创建)目录:
make_config.yaml内容: (安装目录和快捷方式的名字,发布者名称配置等)
# AppId 的值唯一标识此应用。 # 不要在其他应用的安装程序中使用相同的 AppId 值。 # 以下为示例代码,实际根据你的项目进行替换 app_id: 5B566538-42B1-4826-A479-AF079F24A65D publisher: Jory Cai display_name: Hello Hado create_desktop_icon: true install_dir_name: hado_world locales: - en - zh开始生成exe文件:
flutter_distributor package --platform windows --targets exe编译成功后:
对生成的exe文件进行升级签名:后面的路径要是你自己生成的文件路径
dart run auto_updater:sign_update dist/1.1.1/flutter_windows-1.1.1+1.1.1-windows-setup.exe签名后的输入要添加到appcast.xml后面的dsaSignature属性里面:
appcast.xml内容:(配置更新版本和更新说明,还有更新文件下载链接等内容)
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle"> <channel> <title>auto_updater_example</title> <description>Most recent updates to auto_updater_example</description> <language>en</language> <item> <title>Version 1.1.0</title> #发行说明-读取html方式(2选1) <sparkle:releaseNotesLink> https://your_domain/your_path/release_notes.html </sparkle:releaseNotesLink> #发行说明-写死方式(2选1) <description> <![CDATA[ <ul> <li>1、新增XX功能</li> <li>2、新增XX功能</li> </ul> ]]> </description> <pubDate>Sun, 16 Feb 2022 12:00:00 +0800</pubDate> #你更新程序的地址 <enclosure url="http://wwww.xxx.com/1.1.0+2/auto_updater_example-1.1.0+2-macos.zip" sparkle:edSignature="pbdyPt92pnPkzLfQ7BhS9hbjcV9/ndkzSIlWjFQIUMcaCNbAFO2fzl0tISMNJApG2POTkZY0/kJQ2yZYOSVgAA==" sparkle:version="1.1.0" sparkle:os="macos" length="13400992" type="application/octet-stream" /> </item> <item> <title>Version 1.1.0</title> #发行说明-读取html方式(2选1) <sparkle:releaseNotesLink> https://your_domain/your_path/release_notes.html </sparkle:releaseNotesLink> #发行说明-写死方式(2选1) <description> <![CDATA[ <ul> <li>1、新增XX功能</li> <li>2、新增XX功能</li> </ul> ]]> </description> <pubDate>Sun, 16 Feb 2022 12:00:00 +0800</pubDate> #你更新程序的地址 <enclosure url="http://www.xx.com/1.1.0+2/auto_updater_example-1.1.0+2-windows.exe" sparkle:dsaSignature="MEUCIQCVbVzVID7H3aUzAY5znpi+ySZKznkukV8whlMFzKh66AIgREUGOmvavlcg6hwAwkb2o4IqVE/D56ipIBshIqCH8rk=" sparkle:version="1.1.0+2" sparkle:os="windows" length="0" type="application/octet-stream" /> </item> </channel> </rss> 新版本更新操作如果你的程序后续更新了,版本升级了,就修改项目根目录里面的 pubspec.yaml文件版本:
修改完之后,再执行命令重新打包:
flutter_distributor package --platform windows --targets exe打包后就会在项目根目录dist下面出现响应版本的exe:
然后运行命令对这个exe进行签名:
dart run auto_updater:sign_update dist/1.1.0/flutter_windows-1.1.0+1.1.0-windows-setup.exe将签名后的结果添加到appcast.xml里面:
并且修改appcast.xml里面响应的版本和更新说明:
然后启动历史版本的程序,检查更新:
点击安装更新后:
等待下载完成:点击安装更新
就会杀死现在的进程,然后重新执行安装操作:
Flutter使用auto_updater实现windows/mac桌面应用版本升级功能由讯客互联其他栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Flutter使用auto_updater实现windows/mac桌面应用版本升级功能”