应用打包
Tauri 支持将应用打包为各平台的原生安装包,方便分发和安装。
打包配置
基础配置
在 tauri.conf.json 中配置打包选项:
{
"bundle": {
"active": true,
"targets": ["msi", "nsis", "dmg", "appimage", "deb", "rpm"],
"identifier": "com.example.myapp",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/[email protected]",
"icons/icon.icns",
"icons/icon.ico"
],
"resources": [],
"copyright": "© 2024 Your Name",
"category": "DeveloperTool",
"shortDescription": "A Tauri App",
"longDescription": "A longer description of your app"
}
}
配置项说明
| 配置项 | 说明 |
|---|---|
active | 是否启用打包 |
targets | 打包目标格式 |
identifier | 应用唯一标识符(反向域名格式) |
icon | 应用图标路径列表 |
resources | 额外资源文件 |
copyright | 版权信息 |
category | 应用类别 |
支持的目标格式
| 格式 | 平台 | 说明 |
|---|---|---|
msi | Windows | Microsoft Installer |
nsis | Windows | Nullsoft Scriptable Install System |
dmg | macOS | Apple Disk Image |
app | macOS | 应用 bundle |
appimage | Linux | 通用 Linux 包 |
deb | Linux | Debian/Ubuntu 包 |
rpm | Linux | Red Hat/Fedora 包 |
图标配置
图标要求
Tauri 需要多种尺寸的图标用于不同场景:
| 尺寸 | 用途 |
|---|---|
| 32x32 | Windows 任务栏、小图标 |
| 128x128 | macOS、Linux 应用图标 |
| 256x256 | macOS Retina |
| 512x512 | macOS 高分辨率 |
| 1024x1024 | macOS App Store |
图标格式
- Windows:
.ico文件(包含多尺寸) - macOS:
.icns文件 - Linux:
.png文件
自动生成图标
使用 Tauri CLI 从一张大图自动生成所有图标:
npm run tauri icon /path/to/icon.png
这将生成所有平台所需的图标文件到 src-tauri/icons/ 目录。
执行打包
开发构建
npm run tauri build
生产构建
npm run tauri build -- --release
指定目标
# 仅构建 Windows MSI
npm run tauri build -- --target msi
# 构建多个目标
npm run tauri build -- --target msi --target nsis
平台特定配置
Windows
MSI 配置:
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "downloadBootstrapper"
},
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
"timestampUrl": ""
}
}
}
WebView2 安装模式:
| 模式 | 说明 |
|---|---|
downloadBootstrapper | 下载 WebView2 引导程序 |
embedBootstrapper | 嵌入 WebView2 引导程序 |
offlineInstaller | 使用离线安装包 |
fixedRuntime | 固定运行时版本 |
macOS
签名和公证:
{
"bundle": {
"macOS": {
"frameworks": [],
"minimumSystemVersion": "10.13",
"signingIdentity": null,
"entitlements": null
}
}
}
命令行签名:
# 签名应用
codesign --force --options runtime --sign "Developer ID Application: Your Name" MyApp.app
# 公证
xcrun altool --notarize-app --primary-bundle-id "com.example.myapp" \
--username "[email protected]" --password "@keychain:AC_PASSWORD" \
--file MyApp.dmg
Linux
AppImage 配置:
{
"bundle": {
"linux": {
"appimage": {
"bundleMediaFramework": false
}
}
}
}
自动更新
配置更新器
安装插件:
cargo add tauri-plugin-updater
npm install @tauri-apps/plugin-updater
配置权限:
{
"permissions": ["updater:default"]
}
Rust 配置:
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_updater::Builder::new().build())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
前端检查更新:
import { check } from "@tauri-apps/plugin-updater";
import { relaunch } from "@tauri-apps/plugin-process";
async function checkForUpdates() {
const update = await check();
if (update) {
console.log(`发现新版本: ${update.version}`);
// 下载并安装更新
await update.downloadAndInstall();
// 重启应用
await relaunch();
}
}
配置更新服务器:
{
"bundle": {
"createUpdaterArtifacts": true
},
"plugins": {
"updater": {
"active": true,
"endpoints": ["https://updates.example.com/{{target}}/{{arch}}/{{current_version}}"],
"dialog": true,
"pubkey": "YOUR_PUBLIC_KEY"
}
}
}
代码签名
Windows 代码签名
使用证书签名:
# 使用 signtool
signtool sign /f certificate.pfx /p password /tr http://timestamp.digicert.com /td sha256 /fd sha256 MyApp.exe
配置到 Tauri:
{
"bundle": {
"windows": {
"certificateThumbprint": "YOUR_CERT_THUMBPRINT"
}
}
}
macOS 代码签名
创建签名配置:
{
"bundle": {
"macOS": {
"signingIdentity": "Developer ID Application: Your Name (TEAM_ID)"
}
}
}
发布检查清单
发布前检查
- 应用图标已配置
- 应用标识符正确
- 版本号已更新
- 版权信息已填写
- 代码已签名(生产环境)
- 测试安装包可正常安装
- 测试应用可正常运行
- 自动更新已配置(如需要)
版本管理
语义化版本:
{
"version": "1.2.3"
}
- 主版本号:不兼容的 API 修改
- 次版本号:向下兼容的功能新增
- 修订号:向下兼容的问题修正
常见问题
打包失败
问题:找不到图标
Error: icon path does not exist
解决:确保图标文件存在于 src-tauri/icons/ 目录。
问题:权限不足
Error: permission denied
解决:以管理员权限运行终端。
安装问题
Windows: WebView2 未安装
解决:在 tauri.conf.json 中配置 WebView2 安装模式为 downloadBootstrapper。
macOS: 应用无法打开
解决:确保应用已签名,或右键点击选择「打开」。
Linux: 缺少依赖
解决:确保目标系统已安装 WebKitGTK。
最佳实践
- 使用 CI/CD:自动化构建和签名流程
- 测试安装包:在干净的虚拟机中测试
- 版本管理:遵循语义化版本规范
- 代码签名:生产环境必须签名
- 增量更新:配置自动更新减少下载量
下一步
- 速查表 - 常用 API 速查