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

Which Automatic Update Methods Does Tauri Support? How to Implement?

3月6日 23:31

Tauri is a modern framework for building cross-platform desktop applications using Rust and Web technologies. Its core advantages include being lightweight and high-performance. Automatic update functionality is critical for enhancing user experience and ensuring application security, especially in development scenarios with frequent iterations. This article will delve into Tauri's supported automatic update mechanisms, combining official practices with code examples to provide actionable implementation solutions. Tauri itself does not natively include an update system, but through its plugin-based design, it can seamlessly integrate third-party update libraries for efficient version management.

Tauri's Supported Automatic Update Methods

Tauri officially strongly recommends using the tauri-plugin-updater plugin (a Rust implementation based on electron-updater), which provides out-of-the-box GitHub Releases integration and supports the three major platforms: Windows, macOS, and Linux.

Core Features:

  • Automatic version detection: Periodically scan for new versions using the check_update() method.
  • User interaction: Provide pop-up notifications and progress bars to avoid user churn caused by forced updates.
  • Multi-source support: Compatible with update sources such as GitHub Releases and custom APIs.

Implementation Steps:

  1. Add plugin dependencies to Cargo.toml:
toml
[dependencies] tauri = { version = "2.0", features = ["updater"] } # Optional: Add HTTP client (e.g., reqwest) reqwest = "0.11"
  1. Configure the update service: In src/main.rs, initialize the plugin and set the update source. For example, using GitHub Releases as the source:
rust
use tauri::AppHandle; use tauri_plugin_updater::Updater; fn main() { let app = tauri::Builder::default() .plugin(tauri_plugin_updater::init()) .build() .expect("failed to build app"); // Configure via GitHub Releases source (example) let updater = app.updater(); updater.set_release_url("https://api.github.com/repos/your-repo/releases").await; }
  1. Handle update events: In the application lifecycle, call update logic.
rust
// Trigger update check in event handler app.updater().check_update().await; // Listen for update status (example) app.updater().on_update(|status| { match status { tauri_plugin_updater::UpdateStatus::NewVersionAvailable => { // Show user notification println!("New version available: {}", status.version); } tauri_plugin_updater::UpdateStatus::UpdateCompleted => { // Restart application app.restart().unwrap(); } } });

Tauri Automatic Update Flowchart

2. Custom Update Implementation: Deep Integration and Flexibility

For scenarios requiring high customization (such as private repositories or complex logic), bypass the official plugin and directly implement update logic using reqwest and serde.

Technical Approach:

  • HTTP Requests: Retrieve update information using reqwest and parse JSON data.
  • Download and Installation: Use std::fs to handle file downloads and replacements.

Key Code Example:

rust
use reqwest::blocking::get; use serde_json::Value; use std::fs; fn check_for_update() -> Result<(), Box<dyn std::error::Error>> { let response = get("https://api.example.com/updates/v1").unwrap(); let json: Value = response.json().unwrap(); // Check for new version if let Some(new_version) = json["version"]["tag_name"]["as_str"]["as_str"]["as_str"] { let current_version = env::var("APP_VERSION").unwrap_or("1.0.0"); if new_version > current_version { // Download update package (simplified example) let download_url = json["assets"][0]["browser_download_url"]["as_str"]["as_str"]["as_str"]; let mut response = get(&download_url).unwrap(); let mut file = File::create("app-update.exe").unwrap(); // Adjust for platform std::io::copy(&mut response, &mut file).unwrap(); // Installation logic: replace executable and restart std::process::Command::new("app-update.exe").spawn().unwrap(); } } Ok(()) }

标签:Tauri