Tauri 客户端快速上手指南(Rust 多版本管理)

60 阅读3分钟

Tauri 快速上手指南

一、环境安装

⚠️ Tauri 需要 RustNode.js 两个运行环境,请分别安装。

1. Rust

Rust 官网安装文档

Windows 环境安装时,在安装 Visual Studio 时需要注意的问题,要不然运行项目会报错

2. Node.js

Node.js 安装配置指南


rustup 常用命令(Rust 版本管理)

操作命令
已安装版本rustup toolchain list
更新rustup update
安装版本rustup install stable / nightly / 1.75.0
默认版本rustup default stable
卸载rustup uninstall nightly

项目内固定版本:在项目根目录建 rust-toolchain.toml,内容:channel = "stable"(或具体版本号),进入该目录会自动用该版本。
可安装版本列表见:static.rust-lang.org/dist/


验证安装

rustc --version
cargo --version
node --version
npm --version

二、创建项目

npm create tauri-app@latest

按提示依次选择配置项:

✔ Project name · tauri-app                    # 项目名称
✔ Identifier · com.dengzemiao.tauri-app       # 应用标识符(反向域名格式)
✔ Choose which language to use for your frontend · TypeScript / JavaScript
✔ Choose your package manager · npm           # 包管理器:pnpm/yarn/npm/deno/bun
✔ Choose your UI template · Vue               # UI 模板:Vanilla/Vue/React/Svelte 等
✔ Choose your UI flavor · TypeScript          # 语言风格:TypeScript/JavaScript

创建完成后进入项目并安装依赖:

cd tauri-app
npm install

初始化移动端支持(可选):

npm run tauri android init   # Android 初始化
npm run tauri ios init       # iOS 初始化

运行项目:

平台命令
桌面端开发npm run tauri dev
Android 开发npm run tauri android dev
iOS 开发npm run tauri ios dev

三、Hello World 与运行

# Tauri 项目:文件结构、交互流程、开发指南

编辑 src/index.html(Vanilla 模板),例如:

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>My App</title>
  <style>
    body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; font-family: sans-serif; }
    h1 { font-size: 3rem; }
  </style>
</head>
<body>
  <h1>你好,世界!</h1>
</body>
</html>

开发运行:

npm run tauri dev

四、项目名称与图标

编辑 src-tauri/tauri.conf.json:改 productNameidentifierapp.windows[0].title 等。

图标:准备 1024×1024 PNG,放到 src-tauri/icons/icon.png,执行:

npm run tauri icon src-tauri/icons/icon.png

五、打包

⚠️ Tauri 不支持交叉编译,只能在对应平台打对应平台的包。

通用打包命令

npm run tauri build

各平台打包说明

平台打包环境要求产物路径产物格式
macOS需在 macOS 系统执行src-tauri/target/release/bundle/dmg/.dmg
Windows需在 Windows 系统执行src-tauri/target/release/bundle/msi/.msi / .exe
Linux需在 Linux 系统执行src-tauri/target/release/bundle/deb/.deb / .AppImage

多平台打包方案

如需同时打包多个平台,推荐使用 GitHub Actions 进行 CI/CD 自动构建:

# .github/workflows/release.yml
name: Release
on:
  push:
    tags: ['v*']

jobs:
  build:
    strategy:
      matrix:
        include:
          - platform: macos-latest    # macOS 打包
            args: --target universal-apple-darwin
          - platform: windows-latest  # Windows 打包
            args: ''
          - platform: ubuntu-latest   # Linux 打包
            args: ''
    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - uses: dtolnay/rust-action@stable
      - run: npm install
      - uses: tauri-apps/tauri-action@v0
        with:
          args: ${{ matrix.args }}

💡 提示:在本地开发时,直接运行 npm run tauri build 即可,系统会自动打包当前平台的安装包。


六、常用命令

开发调试

命令说明
npm run tauri dev桌面端开发运行
npm run tauri android devAndroid 开发运行
npm run tauri ios deviOS 开发运行

打包构建

命令平台说明
npm run tauri buildmacOS在 macOS 上执行,生成 .dmg
npm run tauri buildWindows在 Windows 上执行,生成 .msi / .exe
npm run tauri buildLinux在 Linux 上执行,生成 .deb / .AppImage
npm run tauri android buildAndroid打包 Android APK
npm run tauri ios buildiOS打包 iOS IPA

其他命令

命令说明
npm run tauri icon <path>从图片生成各平台图标
cargo tauri info查看环境信息

七、目录结构

<项目名>/
├── src/                 # 前端
├── src-tauri/           # Tauri/Rust
│   ├── icons/
│   ├── src/main.rs
│   ├── Cargo.toml
│   └── tauri.conf.json
└── package.json

常见问题

首次编译很慢?
正常,Rust 首次会拉依赖并编译,之后会快很多。

macOS 打包签名报错?
开发测试可在 tauri.conf.jsonbundle 里设 "macOS": { "signingIdentity": null }

如何调试?
开发模式下窗口内右键 → 检查元素,可开 DevTools。