乐闻世界logo
搜索文章和话题

Tauri 应用的构建和打包流程是什么

2月19日 19:24

Tauri 应用的构建和打包流程涉及多个步骤,以下是详细说明:

1. 开发环境准备

安装依赖

bash
# 安装 Rust curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # 安装 Node.js 和包管理器 # 安装系统依赖(macOS) xcode-select --install # 安装系统依赖(Linux) sudo apt update sudo apt install libwebkit2gtk-4.0-dev \ build-essential \ curl \ wget \ file \ libssl-dev \ libayatana-appindicator3-dev \ librsvg2-dev

安装 Tauri CLI

bash
cargo install tauri-cli

2. 项目配置

tauri.conf.json 配置

json
{ "build": { "beforeDevCommand": "npm run dev", "beforeBuildCommand": "npm run build", "devPath": "http://localhost:1420", "distDir": "../dist", "withGlobalTauri": false }, "package": { "productName": "MyApp", "version": "1.0.0" }, "tauri": { "bundle": { "identifier": "com.example.myapp", "icon": ["icons/32x32.png", "icons/128x128.png", "icons/128x128@2x.png", "icons/icon.icns", "icons/icon.ico"], "targets": ["dmg", "msi", "appimage"], "category": "Developer Tool" } } }

3. 开发模式

bash
npm run tauri dev

这个命令会:

  1. 运行 beforeDevCommand 启动开发服务器
  2. 编译 Rust 代码
  3. 启动应用并连接到开发服务器
  4. 支持热重载

4. 构建生产版本

基础构建

bash
npm run tauri build

构建流程:

  1. 运行 beforeBuildCommand 构建前端资源
  2. 编译 Rust 代码为二进制文件
  3. 打包前端资源到应用中
  4. 生成平台特定的安装包

构建特定平台

bash
# macOS npm run tauri build -- --target universal-apple-darwin # Windows npm run tauri build -- --target x86_64-pc-windows-msvc # Linux npm run tauri build -- --target x86_64-unknown-linux-gnu

5. 打包选项

macOS 打包

json
{ "bundle": { "targets": ["dmg", "app"], "macOS": { "signingIdentity": "Developer ID Application: Your Name", "entitlements": "entitlements.plist", "hardenedRuntime": true, "minimumSystemVersion": "10.13" } } }

Windows 打包

json
{ "bundle": { "targets": ["msi", "nsis"], "windows": { "certificateThumbprint": "YOUR_CERTIFICATE_THUMBPRINT", "digestAlgorithm": "sha256", "timestampUrl": "http://timestamp.digicert.com" } } }

Linux 打包

json
{ "bundle": { "targets": ["appimage", "deb"], "linux": { "deb": { "depends": ["libwebkit2gtk-4.0-37"] } } } }

6. 代码签名

macOS 签名

bash
# 导入证书 security import certificate.p12 -k ~/Library/Keychains/login.keychain # 自动签名(在配置中设置)

Windows 签名

bash
# 使用 signtool signtool sign /f certificate.pfx /p password /t http://timestamp.digicert.com app.exe

7. 发布流程

GitHub Actions 自动化

yaml
name: Release on: push: tags: - 'v*' jobs: release: strategy: matrix: os: [macos-latest, ubuntu-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 - uses: dtolnay/rust-toolchain@stable - run: npm install - run: npm run tauri build - uses: softprops/action-gh-release@v1 with: files: src-tauri/target/release/bundle/*

8. 构建优化

减小包体积

toml
[profile.release] opt-level = "z" lto = true codegen-units = 1 strip = true

并行构建

bash
cargo build --release -j $(nproc)

9. 常见问题

构建失败

  • 检查系统依赖是否完整
  • 确认 Rust 和 Node.js 版本兼容
  • 查看详细错误日志

签名问题

  • 确保证书有效
  • 检查签名配置
  • 验证时间戳服务器可用性

跨平台构建

  • 使用 GitHub Actions 或 CI/CD
  • 配置不同的构建目标
  • 测试各平台兼容性
标签:Tauri