跳到主要内容

应用打包

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应用类别

支持的目标格式

格式平台说明
msiWindowsMicrosoft Installer
nsisWindowsNullsoft Scriptable Install System
dmgmacOSApple Disk Image
appmacOS应用 bundle
appimageLinux通用 Linux 包
debLinuxDebian/Ubuntu 包
rpmLinuxRed Hat/Fedora 包

图标配置

图标要求

Tauri 需要多种尺寸的图标用于不同场景:

尺寸用途
32x32Windows 任务栏、小图标
128x128macOS、Linux 应用图标
256x256macOS Retina
512x512macOS 高分辨率
1024x1024macOS 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。

最佳实践

  1. 使用 CI/CD:自动化构建和签名流程
  2. 测试安装包:在干净的虚拟机中测试
  3. 版本管理:遵循语义化版本规范
  4. 代码签名:生产环境必须签名
  5. 增量更新:配置自动更新减少下载量

下一步