Rust应用打包工具tauri-bundler的使用,高效构建跨平台桌面应用安装包

Rust应用打包工具tauri-bundler的使用,高效构建跨平台桌面应用安装包

关于tauri-bundler

tauri-bundler是一个用于将Rust可执行文件打包成特定操作系统应用包的工具。它是从cargo-bundle分叉而来,现在作为Tauri CLI使用的库。

配置

Tauri会自动从tauri.conf.json > bundle对象加载配置,但这个库不依赖它,也可以用于非Tauri应用。

通用设置

这些设置适用于所有(或大多数)操作系统的打包:

  • name: 构建应用的名称。如果未设置,则使用Cargo.toml中的name
  • identifier: [必需] 唯一标识应用的字符串,采用反向DNS格式(如"com.example.appname")
  • icon: [可选] 应用使用的图标,可以是文件路径或glob模式数组
  • version: [可选] 应用版本。如果未设置,则使用Cargo.toml中的version
  • resources: [可选] 将被复制到包资源部分的文件或目录列表
  • copyright: [可选] 与应用关联的版权字符串
  • category: [可选] 应用类别描述
  • short_description: [可选] 应用的简短单行描述
  • long_description: [可选] 应用的多行详细描述

Debian特定设置

这些设置仅在打包deb包时使用:

  • depends: 表示此包依赖的其他包的字符串列表

Mac OS X特定设置

这些设置仅在打包appdmg包时使用:

  • frameworks: 需要与app捆绑的Mac OS X框架列表
  • minimum_system_version: 表示捆绑应用支持的最低Mac OS X版本
  • license: DMG包的许可证文件路径
  • exception_domain: 用于macOS .app包的异常域
  • provider_short_name: 如果Apple ID连接到多个团队,需指定用于公证应用的团队提供者短名称

示例 tauri.conf.json

{
  "productName": "Your Awesome App",
  "version": "0.1.0",
  "identifier": "com.my.app",
  "app": {},
  "bundle": {
    "active": true,
    "shortDescription": "",
    "longDescription": "",
    "copyright": "Copyright (c) You 2021. All rights reserved.",
    "icon": [
      "icons/32x32.png",
      "icons/128x128.png",
      "icons/128x128@2x.png",
      "icons/icon.icns",
      "icons/icon.ico"
    ],
    "resources": ["./assets/**/*.png"],
    "deb": {
      "depends": ["debian-dependency1", "debian-dependency2"]
    },
    "macOS": {
      "frameworks": [],
      "minimumSystemVersion": "10.11",
      "license": "./LICENSE"
    },
    "externalBin": ["./sidecar-app"]
  }
}

完整示例代码

以下是一个完整的Tauri应用配置示例,展示了如何使用tauri-bundler打包跨平台桌面应用:

// Cargo.toml
[package]
name = "my-tauri-app"
version = "0.1.0"
edition = "2021"

[dependencies]
tauri = { version = "1.0", features = ["api-all"] }

[build-dependencies]
tauri-build = { version = "1.0" }
// src/main.rs
fn main() {
  tauri::Builder::default()
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
// tauri.conf.json
{
  "build": {
    "distDir": "../dist",
    "devPath": "http://localhost:3000",
    "beforeDevCommand": "",
    "beforeBuildCommand": ""
  },
  "ctx": {},
  "tauri": {
    "bundle": {
      "active": true,
      "targets": "all",
      "identifier": "com.example.myapp",
      "icon": [
        "icons/32x32.png",
        "icons/128x128.png",
        "icons/icon.icns",
        "icons/icon.ico"
      ],
      "resources": ["assets/**/*"],
      "externalBin": [],
      "copyright": "",
      "category": "DeveloperTool",
      "shortDescription": "My awesome Tauri app",
      "longDescription": "",
      "deb": {
        "depends": []
      },
      "macOS": {
        "frameworks": [],
        "minimumSystemVersion": "",
        "license": null,
        "exceptionDomain": "",
        "signingIdentity": null,
        "providerShortName": null,
        "entitlements": null
      },
      "windows": {
        "certificateThumbprint": null,
        "digestAlgorithm": "sha256",
        "timestampUrl": ""
      }
    },
    "updater": {
      "active": false
    },
    "allowlist": {
      "all": true
    },
    "windows": [
      {
        "title": "My Tauri App",
        "width": 800,
        "height": 600,
        "resizable": true,
        "fullscreen": false
      }
    ],
    "security": {
      "csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'"
    }
  }
}

安装

在项目目录中运行以下Cargo命令安装tauri-bundler:

cargo add tauri-bundler

或者在Cargo.toml中添加:

tauri-bundler = "2.6.0"

许可证

© 2017 - present, George Burton, Tauri-Apps Organization

本程序根据Apache Software License或MIT License的条款授权。


1 回复

Rust应用打包工具tauri-bundler的使用:高效构建跨平台桌面应用安装包

什么是tauri-bundler

tauri-bundler是Tauri框架的一部分,专门用于将Rust编写的桌面应用程序打包成各个平台的安装包。它支持Windows、macOS和Linux三大主流操作系统,能够生成对应平台的安装程序或可执行文件。

主要特性

  • 跨平台支持:一次打包,多平台分发
  • 自动处理依赖关系
  • 生成平台特定的安装包格式(如Windows的.msi、macOS的.dmg等)
  • 支持应用图标和元数据定制
  • 与Tauri框架无缝集成

安装与基本使用

首先确保你已经有一个Tauri项目。如果没有,可以这样创建一个:

cargo create-tauri-app

然后进入项目目录,tauri-bundler通常已经作为依赖包含在项目中。

配置打包选项

tauri.conf.json中配置打包选项:

{
  "build": {
    "distDir": "../dist",
    "devPath": "http://localhost:3000",
    "beforeDevCommand": "",
    "beforeBuildCommand": ""
  },
  "tauri": {
    "bundle": {
      "active": true,
      "targets": "all",
      "identifier": "com.example.myapp",
      "icon": ["icons/32x32.png", "icons/128x128.png", "icons/128x128@2x.png", "icons/icon.icns", "icons/icon.ico"],
      "resources": [],
      "externalBin": [],
      "copyright": "",
      "category": "DeveloperTool",
      "shortDescription": "",
      "longDescription": "",
      "deb": {
        "depends": []
      },
      "macOS": {
        "frameworks": [],
        "minimumSystemVersion": "",
        "exceptionDomain": "",
        "signingIdentity": null,
        "entitlements": null
      },
      "windows": {
        "certificateThumbprint": null,
        "digestAlgorithm": "sha256",
        "timestampUrl": ""
      }
    }
  }
}

执行打包命令

cargo tauri build

这会为当前操作系统生成安装包。如果要为其他平台打包,可以使用:

cargo tauri build --target x86_64-pc-windows-msvc  # Windows
cargo tauri build --target x86_64-apple-darwin    # macOS
cargo tauri build --target x86_64-unknown-linux-gnu  # Linux

高级配置示例

自定义Windows安装程序

"windows": {
  "wix": {
    "template": "path/to/custom.wxs",
    "language": "en-US",
    "languages": ["en-US", "zh-CN"],
    "upgradeGuid": "YOUR-GUID-HERE"
  }
}

自定义macOS DMG背景

"macOS": {
  "dmg": {
    "background": "path/to/background.png",
    "window": {
      "width": 500,
      "height": 300
    }
  }
}

常见问题解决

  1. 图标问题:确保提供了所有需要的图标尺寸和格式
  2. 签名问题:发布版本需要代码签名,开发时可暂时禁用
  3. 依赖问题:确保目标平台有必要的构建工具链

最佳实践

  • 使用CI/CD自动化打包流程
  • 为不同平台设置不同的配置
  • 测试生成的安装包在实际环境中运行情况
  • 保持Tauri和tauri-bundler版本更新

通过tauri-bundler,你可以轻松地将Rust桌面应用打包为专业的安装程序,大大简化了分发流程。

完整示例demo

以下是一个完整的Tauri项目配置示例,展示了如何使用tauri-bundler进行打包:

  1. 首先创建项目结构:
myapp/
├── src/
│   └── main.rs
├── icons/
│   ├── 32x32.png
│   ├── 128x128.png
│   ├── icon.icns
│   └── icon.ico
├── tauri.conf.json
└── Cargo.toml
  1. Cargo.toml 内容:
[package]
name = "myapp"
version = "0.1.0"
edition = "2021"

[dependencies]
tauri = { version = "1.0", features = ["bundler"] }
  1. tauri.conf.json 完整配置:
{
  "build": {
    "distDir": "../dist",
    "devPath": "http://localhost:3000",
    "beforeDevCommand": "",
    "beforeBuildCommand": ""
  },
  "tauri": {
    "bundle": {
      "active": true,
      "targets": ["deb", "appimage", "dmg", "msi"],
      "identifier": "com.example.myapp",
      "icon": [
        "icons/32x32.png",
        "icons/128x128.png",
        "icons/128x128@2x.png",
        "icons/icon.icns",
        "icons/icon.ico"
      ],
      "resources": [],
      "externalBin": [],
      "copyright": "Copyright © 2023 MyApp",
      "category": "Utility",
      "shortDescription": "A sample Tauri application",
      "longDescription": "This is a sample application built with Tauri framework",
      "deb": {
        "depends": ["libgtk-3-0", "libwebkit2gtk-4.0-37"]
      },
      "macOS": {
        "frameworks": [],
        "minimumSystemVersion": "10.13",
        "exceptionDomain": "",
        "signingIdentity": null,
        "entitlements": null,
        "dmg": {
          "background": null,
          "window": {
            "width": 600,
            "height": 400
          }
        }
      },
      "windows": {
        "certificateThumbprint": null,
        "digestAlgorithm": "sha256",
        "timestampUrl": "",
        "wix": {
          "language": "en-US",
          "languages": ["en-US"],
          "upgradeGuid": "5B3F4A2E-1D47-4B9A-BF3A-3A3A3A3A3A3A"
        }
      }
    }
  }
}
  1. 执行打包命令:
# 开发模式运行
cargo tauri dev

# 构建当前平台安装包
cargo tauri build

# 构建特定平台安装包
cargo tauri build --target x86_64-pc-windows-msvc  # Windows
cargo tauri build --target x86_64-apple-darwin    # macOS
cargo tauri build --target x86_64-unknown-linux-gnu  # Linux
  1. 构建完成后,安装包会生成在:
  • Windows: target/release/bundle/msi/ 目录下
  • macOS: target/release/bundle/dmg/ 目录下
  • Linux: target/release/bundle/appimage/target/release/bundle/deb/ 目录下
回到顶部