Skip to content
Tauri 中文网

Windows 安装程序

用于 Windows 的 Tauri 应用要么以 Microsoft 安装程序(.msi 文件)的形式分发,使用 WiX Toolset v3,要么以安装可执行文件(-setup.exe 文件)的形式分发,使用 NSIS

🌐 Tauri applications for Windows are either distributed as Microsoft Installers (.msi files) using the WiX Toolset v3 or as setup executables (-setup.exe files) using NSIS.

请注意,.msi 安装程序只能在 Windows 上创建,因为 WiX 只能在 Windows 系统上运行。下面展示了 NSIS 安装程序的交叉编译方法。

🌐 Please note that .msi installers can only be created on Windows as WiX can only run on Windows systems. Cross-compilation for NSIS installers is shown below.

本指南提供有关安装程序可用自定义选项的信息。

🌐 This guide provides information about available customization options for the installer.

🌐 Building

要将你的应用构建并打包成 Windows 安装程序,你可以使用 Tauri CLI,并在 Windows 计算机上运行 tauri build 命令:

🌐 To build and bundle your app into a Windows installer you can use the Tauri CLI and run the tauri build command in a Windows computer:

npm run tauri build

🌐 Build Windows apps on Linux and macOS

在 Linux 和 macOS 主机上交叉编译 Windows 应用在使用 NSIS 时是可能的,但有一些注意事项。它不像直接在 Windows 上编译那样简单,而且测试也不如在 Windows 上多。因此,只有在本地虚拟机或像 GitHub Actions 这样的 CI 解决方案对你不起作用时,才应作为最后的手段使用。

🌐 Cross compiling Windows apps on Linux and macOS hosts is possible with caveats when using NSIS. It is not as straight forward as compiling on Windows directly and is not tested as much. Therefore it should only be used as a last resort if local VMs or CI solutions like GitHub Actions don’t work for you.

由于 Tauri 正式仅支持 MSVC Windows 目标,因此设置稍微复杂一些。

🌐 Since Tauri officially only supports the MSVC Windows target, the setup is a bit more involved.

🌐 Install NSIS

一些 Linux 发行版在其软件库中提供 NSIS,例如在 Ubuntu 上,你可以通过运行以下命令来安装 NSIS:

Ubuntu
sudo apt install nsis

但在许多其他发行版中,你必须自己编译 NSIS,或者手动下载在发行版二进制包中未包含的 Stubs 和插件。例如,Fedora 仅提供二进制文件,但不提供 Stubs 和插件:

🌐 But on many other distributions you have to compile NSIS yourself or download Stubs and Plugins manually that weren’t included in the distro’s binary package. Fedora for example only provides the binary but not the Stubs and Plugins:

Fedora
sudo dnf in mingw64-nsis
wget https://github.com/tauri-apps/binary-releases/releases/download/nsis-3/nsis-3.zip
unzip nsis-3.zip
sudo cp nsis-3.08/Stubs/* /usr/share/nsis/Stubs/
sudo cp -r nsis-3.08/Plugins/** /usr/share/nsis/Plugins/

🌐 Install LLVM and the LLD Linker

由于默认的 Microsoft 链接器仅在 Windows 上工作,我们还需要安装一个新的链接器。 要编译用于设置应用图标等的 Windows 资源文件,我们还需要 llvm-rc 二进制文件,该文件是 LLVM 项目的一部分。

🌐 Since the default Microsoft linker only works on Windows we will also need to install a new linker. To compile the Windows Resource file which is used for setting the app icon among other things we will also need the llvm-rc binary which is part of the LLVM project.

Ubuntu
sudo apt install lld llvm

在 Linux 上,如果你添加了在构建脚本中编译 C/C++ 依赖的依赖包,还需要安装 clang 包。默认的 Tauri 应用不需要这个。

🌐 On Linux you also need to install the clang package if you added dependencies that compile C/C++ dependencies as part of their build scripts. Default Tauri apps should not require this.

🌐 Install the Windows Rust target

假设你正在为 64 位 Windows 系统构建:

🌐 Assuming you’re building for 64-bit Windows systems:

Terminal window
rustup target add x86_64-pc-windows-msvc

🌐 Install cargo-xwin

我们将使用 [cargo-xwin] 作为 Tauri 的“运行器”,而不是手动设置 Windows SDK。

🌐 Instead of setting the Windows SDKs up manually we will use [cargo-xwin] as Tauri’s “runner”:

Terminal window
cargo install --locked cargo-xwin

默认情况下,cargo-xwin 会将 Windows SDK 下载到项目本地文件夹中。 如果你有多个项目并且想要共享这些文件,你可以设置 XWIN_CACHE_DIR 环境变量为首选位置的路径。

🌐 By default cargo-xwin will download the Windows SDKs into a project-local folder. If you have multiple projects and want to share those files you can set the XWIN_CACHE_DIR environment variable with a path to the preferred location.

🌐 Building the App

现在应该像将运行器和目标添加到 tauri build 命令一样简单:

🌐 Now it should be as simple as adding the runner and target to the tauri build command:

npm run tauri build -- --runner cargo-xwin --target x86_64-pc-windows-msvc

构建输出将会在 target/x86_64-pc-windows-msvc/release/bundle/nsis/

🌐 The build output will then be in target/x86_64-pc-windows-msvc/release/bundle/nsis/.

🌐 Building for 32-bit or ARM

Tauri CLI 默认使用你机器的架构来编译可执行文件。假设你在一台 64 位机器上开发,CLI 将生成 64 位的应用。

🌐 The Tauri CLI compiles your executable using your machine’s architecture by default. Assuming that you’re developing on a 64-bit machine, the CLI will produce 64-bit applications.

如果你需要支持 32 位 机器,你可以使用 --target 标志用 不同的 Rust目标 编译你的应用:

🌐 If you need to support 32-bit machines, you can compile your application with a different Rust target using the --target flag:

npm run tauri build -- --target i686-pc-windows-msvc

默认情况下,Rust 只会为你的机器目标安装工具链,所以你需要先安装 32 位 Windows 工具链:rustup target add i686-pc-windows-msvc

🌐 By default, Rust only installs toolchains for your machine’s target, so you need to install the 32-bit Windows toolchain first: rustup target add i686-pc-windows-msvc.

如果你需要为 ARM64 构建,你首先需要安装额外的构建工具。 要做到这一点,打开 Visual Studio Installer,点击“修改”,然后在“单个组件”标签下安装“C++ ARM64 构建工具”。 在撰写本文时,VS2022 中的确切名称是 MSVC v143 - VS 2022 C++ ARM64 build tools (Latest)。 现在你可以使用 rustup target add aarch64-pc-windows-msvc 添加 Rust 目标,然后使用上述方法编译你的应用:

🌐 If you need to build for ARM64 you first need to install additional build tools. To do this, open Visual Studio Installer, click on “Modify”, and in the “Individual Components” tab install the “C++ ARM64 build tools”. At the time of writing, the exact name in VS2022 is MSVC v143 - VS 2022 C++ ARM64 build tools (Latest). Now you can add the rust target with rustup target add aarch64-pc-windows-msvc and then use the above-mentioned method to compile your app:

npm run tauri build -- --target aarch64-pc-windows-msvc

🌐 Supporting Windows 7

默认情况下,Microsoft 安装程序(.msi)在 Windows 7 上无法运行,因为如果未安装,它需要下载 WebView2 引导程序(如果操作系统未启用 TLS 1.2,可能会失败)。Tauri 提供了一个选项来嵌入 WebView2 引导程序(请参见下面的 嵌入 WebView2 引导程序 部分)。基于 NSIS 的安装程序(-setup.exe)在 Windows 7 上也支持 downloadBootstrapper 模式。

🌐 By default, the Microsoft Installer (.msi) does not work on Windows 7 because it needs to download the WebView2 bootstrapper if not installed (which might fail if TLS 1.2 is not enabled in the operating system). Tauri includes an option to embed the WebView2 bootstrapper (see the Embedding the WebView2 Bootstrapper section below). The NSIS based installer (-setup.exe) also supports the downloadBootstrapper mode on Windows 7.

此外,要在 Windows 7 中使用通知 API,你需要启用 windows7-compat Cargo 功能:

🌐 Additionally, to use the Notification API in Windows 7, you need to enable the windows7-compat Cargo feature:

Cargo.toml
[dependencies]
tauri-plugin-notification = { version = "2.0.0", features = [ "windows7-compat" ] }

🌐 FIPS Compliance

如果你的系统要求 MSI 包符合 FIPS 标准,你可以在运行 tauri build 之前将 TAURI_BUNDLER_WIX_FIPS_COMPLIANT 环境变量设置为 true。在 PowerShell 中,你可以像这样为当前终端会话设置它:

🌐 If your system requires the MSI bundle to be FIPS compliant you can set the TAURI_BUNDLER_WIX_FIPS_COMPLIANT environment variable to true before running tauri build. In PowerShell you can set it for the current terminal session like this:

Terminal window
$env:TAURI_BUNDLER_WIX_FIPS_COMPLIANT="true"

🌐 WebView2 Installation Options

安装程序默认会下载 WebView2 引导程序,并在未安装运行时的情况下执行它。或者,你可以嵌入引导程序、嵌入离线安装程序,或使用固定的 WebView2 运行时版本。请参阅下表以比较这些方法:

🌐 The installers by default download the WebView2 bootstrapper and executes it if the runtime is not installed. Alternatively, you can embed the bootstrapper, embed the offline installer, or use a fixed WebView2 runtime version. See the following table for a comparison between these methods:

安装方式需要互联网连接?额外安装程序大小备注
downloadBootstrapper0MBDefault
会产生较小的安装程序大小,但不推荐通过 .msi 文件在 Windows 7 上部署。
embedBootstrapper~1.8MB在 Windows 7 上对 .msi 安装程序提供更好的支持。
offlineInstaller~127MB内嵌 WebView2 安装程序。推荐用于离线环境。
fixedVersion~180MB内嵌固定版本的 WebView2。
skip0MB⚠️ 不推荐
不会作为 Windows 安装程序的一部分安装 WebView2。

🌐 Downloaded Bootstrapper

这是用于构建 Windows 安装程序的默认设置。它会下载引导程序并运行它。 需要互联网连接,但会生成较小的安装程序大小。 如果你打算通过 .msi 安装程序分发到 Windows 7,不推荐使用此设置。

🌐 This is the default setting for building the Windows Installer. It downloads the bootstrapper and runs it. Requires an internet connection but results in a smaller installer size. This is not recommended if you’re going to be distributing to Windows 7 via .msi installers.

tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "downloadBootstrapper"
}
}
}
}

🌐 Embedded Bootstrapper

要嵌入 WebView2 引导程序,请将 webviewInstallMode 设置为 embedBootstrapper。这会使安装程序的大小增加约 1.8MB,但会提高与 Windows 7 系统的兼容性。

🌐 To embed the WebView2 Bootstrapper, set the webviewInstallMode to embedBootstrapper. This increases the installer size by around 1.8MB, but increases compatibility with Windows 7 systems.

tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "embedBootstrapper"
}
}
}
}

🌐 Offline Installer

要嵌入 WebView2 引导程序,请将 webviewInstallMode 设置为 offlineInstaller。这会使安装程序的大小增加约 127MB,但即使没有互联网连接,也可以安装你的应用。

🌐 To embed the WebView2 Bootstrapper, set the webviewInstallMode to offlineInstaller. This increases the installer size by around 127MB, but allows your application to be installed even if an internet connection is not available.

tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "offlineInstaller"
}
}
}
}

🌐 Fixed Version

使用系统提供的运行时对于安全性非常好,因为 WebView 的漏洞补丁由 Windows 管理。 如果你想控制每个应用上的 WebView2 分发(无论是为了自己管理发布补丁,还是在可能无法联网的环境中分发应用),Tauri 都可以为你打包运行时文件。

🌐 Using the runtime provided by the system is great for security as the webview vulnerability patches are managed by Windows. If you want to control the WebView2 distribution on each of your applications (either to manage the release patches yourself or distribute applications on environments where an internet connection might not be available) Tauri can bundle the runtime files for you.

  1. 微软的网站 下载 WebView2 固定版本运行时。在此示例中,下载的文件名为 Microsoft.WebView2.FixedVersionRuntime.128.0.2739.42.x64.cab
  2. 将文件解压到核心文件夹:
Terminal window
Expand .\Microsoft.WebView2.FixedVersionRuntime.128.0.2739.42.x64.cab -F:* ./src-tauri
  1. tauri.conf.json 中配置 WebView2 运行时路径:
tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "fixedRuntime",
"path": "./Microsoft.WebView2.FixedVersionRuntime.98.0.1108.50.x64/"
}
}
}
}
  1. 运行 tauri build 以生成带有修复后的 WebView2 运行时的 Windows 安装程序。

🌐 Skipping Installation

你可以通过将 webviewInstallMode 设置为 skip 来从安装程序中移除 WebView2 运行时下载检查。如果用户没有安装运行时,你的应用将无法正常工作。

🌐 You can remove the WebView2 Runtime download check from the installer by setting webviewInstallMode to skip. Your application WILL NOT work if the user does not have the runtime installed.

如果用户未安装运行时且不会尝试安装,则你的应用将无法运行。

🌐 Your application WILL NOT work if the user does not have the runtime installed and won’t attempt to install it.

tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "skip"
}
}
}
}

🌐 Customizing the WiX Installer

请参阅 [WiX 配置],以获取完整的自定义选项列表。

🌐 See the WiX configuration for the complete list of customization options.

🌐 Installer Template

.msi Windows 安装程序包是使用 WiX Toolset v3 构建的。目前,除了预定义的 配置 外,你可以通过使用自定义 WiX 源代码(一个具有 .wxs 文件扩展名的 XML 文件)或通过 WiX 片段来更改它。

🌐 The .msi Windows Installer package is built using the WiX Toolset v3. Currently, apart from pre-defined configurations, you can change it by using a custom WiX source code (an XML file with a .wxs file extension) or through WiX fragments.

🌐 Replacing the Installer Code with a Custom WiX File

Tauri 定义的 Windows 安装程序 XML 被配置为适用于基于简单 webview 的应用的常见用例(你可以在 这里 找到它)。 它使用 handlebars,因此 Tauri CLI 可以根据你的 tauri.conf.json 定义来定制你的安装程序。 如果你需要一个完全不同的安装程序,可以在 tauri.bundle.windows.wix.template 配置自定义模板文件。

🌐 The Windows Installer XML defined by Tauri is configured to work for the common use case of simple webview-based applications (you can find it here). It uses handlebars so the Tauri CLI can brand your installer according to your tauri.conf.json definition. If you need a completely different installer, a custom template file can be configured on tauri.bundle.windows.wix.template.

🌐 Extending the Installer with WiX Fragments

A WiX fragment 是一个容器,你可以在其中配置 WiX 提供的几乎所有内容。 在这个例子中,我们将定义一个写入两个注册表项的片段:

🌐 A WiX fragment is a container where you can configure almost everything offered by WiX. In this example, we will define a fragment that writes two registry entries:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<!-- these registry entries should be installed
to the target user's machine -->
<DirectoryRef Id="TARGETDIR">
<!-- groups together the registry entries to be installed -->
<!-- Note the unique `Id` we provide here -->
<Component Id="MyFragmentRegistryEntries" Guid="*">
<!-- the registry key will be under
HKEY_CURRENT_USER\Software\MyCompany\MyApplicationName -->
<!-- Tauri uses the second portion of the
bundle identifier as the `MyCompany` name
(e.g. `tauri-apps` in `com.tauri-apps.test`) -->
<RegistryKey
Root="HKCU"
Key="Software\MyCompany\MyApplicationName"
Action="createAndRemoveOnUninstall"
>
<!-- values to persist on the registry -->
<RegistryValue
Type="integer"
Name="SomeIntegerValue"
Value="1"
KeyPath="yes"
/>
<RegistryValue Type="string" Value="Default Value" />
</RegistryKey>
</Component>
</DirectoryRef>
</Fragment>
</Wix>

将片段文件保存到 src-tauri/windows/fragments 文件夹中,使用 .wxs 扩展名,并在 tauri.conf.json 上引用它:

🌐 Save the fragment file with the .wxs extension in the src-tauri/windows/fragments folder and reference it on tauri.conf.json:

tauri.conf.json
{
"bundle": {
"windows": {
"wix": {
"fragmentPaths": ["./windows/fragments/registry.wxs"],
"componentRefs": ["MyFragmentRegistryEntries"]
}
}
}
}

请注意,ComponentGroupComponentFeatureGroupFeatureMerge 元素 ID 必须在 componentGroupRefscomponentRefsfeatureGroupRefsfeatureRefsmergeRefs 上分别引用 tauri.conf.jsonwix 对象,才能包含在安装程序中。

🌐 Note that ComponentGroup, Component, FeatureGroup, Feature and Merge element ids must be referenced on the wix object of tauri.conf.json on the componentGroupRefs, componentRefs, featureGroupRefs, featureRefs and mergeRefs respectively to be included in the installer.

🌐 Internationalization

WiX 安装程序默认使用 en-US 语言构建。国际化 (i18n) 可以通过 tauri.bundle.windows.wix.language 属性进行配置,定义 Tauri 应该针对哪些语言构建安装程序。你可以在 微软的网站 的 Language-Culture 列中找到要使用的语言名称。

🌐 The WiX Installer is built using the en-US language by default. Internationalization (i18n) can be configured using the tauri.bundle.windows.wix.language property, defining the languages Tauri should build an installer against. You can find the language names to use in the Language-Culture column on Microsoft’s website.

🌐 Compiling a WiX Installer for a Single Language

要创建针对特定语言的单个安装程序,请将 language 值设置为一个字符串:

🌐 To create a single installer targeting a specific language, set the language value to a string:

tauri.conf.json
{
"bundle": {
"windows": {
"wix": {
"language": "fr-FR"
}
}
}
}

🌐 Compiling a WiX Installer for Each Language in a List

要编译针对一系列语言的安装程序,请使用数组。每种语言将创建一个特定的安装程序,语言键作为后缀:

🌐 To compile an installer targeting a list of languages, use an array. A specific installer for each language will be created, with the language key as a suffix:

tauri.conf.json
{
"bundle": {
"windows": {
"wix": {
"language": ["en-US", "pt-BR", "fr-FR"]
}
}
}
}

🌐 Configuring the WiX Installer Strings for Each Language

可以为每种语言定义一个配置对象来配置本地化字符串:

🌐 A configuration object can be defined for each language to configure localization strings:

tauri.conf.json
{
"bundle": {
"windows": {
"wix": {
"language": {
"en-US": null,
"pt-BR": {
"localePath": "./wix/locales/pt-BR.wxl"
}
}
}
}
}
}

localePath 属性定义了语言文件的路径,这是一个配置语言文化的 XML 文件:

🌐 The localePath property defines the path to a language file, a XML configuring the language culture:

<WixLocalization
Culture="en-US"
xmlns="http://schemas.microsoft.com/wix/2006/localization"
>
<String Id="LaunchApp"> Launch MyApplicationName </String>
<String Id="DowngradeErrorMessage">
A newer version of MyApplicationName is already installed.
</String>
<String Id="PathEnvVarFeature">
Add the install location of the MyApplicationName executable to
the PATH system environment variable. This allows the
MyApplicationName executable to be called from any location.
</String>
<String Id="InstallAppFeature">
Installs MyApplicationName.
</String>
</WixLocalization>

目前,Tauri 引用以下本地化字符串:LaunchAppDowngradeErrorMessagePathEnvVarFeatureInstallAppFeature。 你可以定义自己的字符串,并在自定义模板或片段中使用 "!(loc.TheStringId)" 引用它们。 有关更多信息,请参阅[WiX 本地化文档]。

🌐 Currently, Tauri references the following locale strings: LaunchApp, DowngradeErrorMessage, PathEnvVarFeature and InstallAppFeature. You can define your own strings and reference them on your custom template or fragments with "!(loc.TheStringId)". See the WiX localization documentation for more information.

🌐 Customizing the NSIS Installer

请参阅 [NSIS 配置],以获取完整的自定义选项列表。

🌐 See the NSIS configuration for the complete list of customization options.

🌐 Installer Template

由 Tauri 定义的 NSIS 安装程序的 .nsi 脚本被配置为适用于基于简单 webview 的应用的常见使用场景(你可以在 这里 找到它)。它使用 handlebars,因此 Tauri CLI 可以根据你的 tauri.conf.json 定义为你的安装程序添加品牌。如果你需要一个完全不同的安装程序,可以在 tauri.bundle.windows.nsis.template 上配置自定义模板文件。

🌐 The NSIS Installer’s .nsi script defined by Tauri is configured to work for the common use case of simple webview-based applications (you can find it here). It uses handlebars so the Tauri CLI can brand your installer according to your tauri.conf.json definition. If you need a completely different installer, a custom template file can be configured on tauri.bundle.windows.nsis.template.

🌐 Extending the Installer

如果你只需要扩展一些安装步骤,你可能能够使用安装程序钩子,而不是替换整个安装程序模板。

🌐 If you only need to extend some installation steps you might be able to use installer hooks instead of replacing the entire installer template.

支持的钩子有:

🌐 Supported hooks are:

  • NSIS_HOOK_PREINSTALL:在复制文件、设置注册表项值和创建快捷方式之前运行。
  • NSIS_HOOK_POSTINSTALL:在安装程序完成复制所有文件、设置注册表项和创建快捷方式后运行。
  • NSIS_HOOK_PREUNINSTALL:在删除任何文件、注册表项和快捷方式之前运行。
  • NSIS_HOOK_POSTUNINSTALL:在文件、注册表项和快捷方式被删除后运行。

例如,在 src-tauri/windows 文件夹中创建一个 hooks.nsh 文件,并定义你需要的钩子:

🌐 For example, create a hooks.nsh file in the src-tauri/windows folder and define the hooks you need:

!macro NSIS_HOOK_PREINSTALL
MessageBox MB_OK "PreInstall"
!macroend
!macro NSIS_HOOK_POSTINSTALL
MessageBox MB_OK "PostInstall"
!macroend
!macro NSIS_HOOK_PREUNINSTALL
MessageBox MB_OK "PreUnInstall"
!macroend
!macro NSIS_HOOK_POSTUNINSTALL
MessageBox MB_OK "PostUninstall"
!macroend

然后你必须配置 Tauri 以使用该钩子文件:

🌐 Then you must configure Tauri to use that hook file:

tauri.conf.json
{
"bundle": {
"windows": {
"nsis": {
"installerHooks": "./windows/hooks.nsh"
}
}
}
}

🌐 Installing Dependencies with Hooks

你可以使用安装程序钩子来自动安装你的应用所需的系统依赖。这对于运行时依赖尤其有用,例如 Visual C++ 可再发行组件、DirectX、OpenSSL 或其他可能并非所有 Windows 系统上都存在的系统库。

🌐 You can use installer hooks to automatically install system dependencies that your application requires. This is particularly useful for runtime dependencies like Visual C++ Redistributables, DirectX, OpenSSL or other system libraries that may not be present on all Windows systems.

MSI 安装程序示例(Visual C++ 可再发行组件):

!macro NSIS_HOOK_POSTINSTALL
; Check if Visual C++ 2019 Redistributable is installed (via Windows Registry)
ReadRegDWord $0 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64" "Installed"
${If} $0 == 1
DetailPrint "Visual C++ Redistributable already installed"
Goto vcredist_done
${EndIf}
; Install from bundled MSI if not installed
${If} ${FileExists} "$INSTDIR\resources\vc_redist.x64.msi"
DetailPrint "Installing Visual C++ Redistributable..."
; Copy to TEMP folder and then execute installer
CopyFiles "$INSTDIR\resources\vc_redist.x64.msi" "$TEMP\vc_redist.x64.msi"
ExecWait 'msiexec /i "$TEMP\vc_redist.x64.msi" /passive /norestart' $0
; Check wether installation process exited successfully (code 0) or not
${If} $0 == 0
DetailPrint "Visual C++ Redistributable installed successfully"
${Else}
MessageBox MB_ICONEXCLAMATION "Visual C++ installation failed. Some features may not work."
${EndIf}
; Clean up setup files from TEMP and your installed app
Delete "$TEMP\vc_redist.x64.msi"
Delete "$INSTDIR\resources\vc_redist.x64.msi"
${EndIf}
vcredist_done:
!macroend

关键考虑因素:

  • 一个好的做法是总是通过注册表项、文件存在性检查或使用 Windows where 命令来确认依赖是否已经安装。
  • 使用 /passive/quiet/silent 标志以避免中断安装流程。查看 msiexec 选项以获取 .msi 文件,或查阅安装手册了解应用特定的标志
  • 包含 /norestart 以防止在安装过程中自动重启系统,适用于会重启用户设备的安装
  • 清理临时文件和打包的安装程序,以避免应用体积过大。
  • 卸载时,请注意依赖可能会与其他应用共享。
  • 如果安装失败,请提供有意义的错误消息。

确保将依赖安装程序打包在你的 src-tauri/resources 文件夹中,并添加到 tauri.conf.json,以便它们被打包,并且在从 $INSTDIR\resources\ 安装时可以访问:

🌐 Ensure to bundle the dependency installers in your src-tauri/resources folder and add to tauri.conf.json so they get bundled, and can be accessed during installation from $INSTDIR\resources\:

tauri.conf.json
{
"bundle": {
"resources": [
"resources/my-dependency.exe",
"resources/another-one.msi
]
}
}

🌐 Install Modes

默认情况下,安装程序将仅为当前用户安装你的应用。此选项的优点是安装程序运行时不需要管理员权限,但应用会安装在 %LOCALAPPDATA% 文件夹中,而不是 C:/Program Files

🌐 By default the installer will install your application for the current user only. The advantage of this option is that the installer does not require Administrator privileges to run, but the app is installed in the %LOCALAPPDATA% folder instead of C:/Program Files.

如果你希望应用安装可以在整个系统范围内使用(这需要管理员权限),你可以将 installMode 设置为 perMachine

🌐 If you prefer your app installation to be available system-wide (which requires Administrator privileges) you can set installMode to perMachine:

tauri.conf.json
{
"bundle": {
"windows": {
"nsis": {
"installMode": "perMachine"
}
}
}
}

或者,你可以让用户选择应用是仅为当前用户安装还是系统范围安装,通过将 installMode 设置为 both。请注意,安装程序执行时需要管理员权限。

🌐 Alternatively you can let the user choose whether the app should be installed for the current user only or system-wide by setting the installMode to both. Note that the installer will require Administrator privileges to execute.

有关更多信息,请参见 NSISInstallerMode

🌐 See NSISInstallerMode for more information.

🌐 Internationalization

NSIS 安装程序是一个多语言安装程序,这意味着你始终有一个包含所有选定翻译的安装程序。

🌐 The NSIS Installer is a multi-language installer, which means you always have a single installer which contains all the selected translations.

你可以使用 tauri.bundle.windows.nsis.languages 属性指定要包含的语言。NSIS 支持的语言列表可在 [NSIS GitHub 项目] 中找到。还有一些 [Tauri 特定的翻译] 是必需的,所以如果你看到未翻译的文本,请随时在 [Tauri 的主仓库] 中提交功能请求。你也可以提供 自定义翻译文件

🌐 You can specify which languages to include using the tauri.bundle.windows.nsis.languages property. A list of languages supported by NSIS is available in the NSIS GitHub project. There are a few Tauri-specific translations required, so if you see untranslated texts feel free to open a feature request in Tauri’s main repo. You can also provide custom translation files.

默认情况下,操作系统的默认语言用于确定安装程序的语言。你还可以配置安装程序,在安装程序内容显示之前显示语言选择器:

🌐 By default the operating system default language is used to determine the installer language. You can also configure the installer to display a language selector before the installer contents are rendered:

tauri.conf.json
{
"bundle": {
"windows": {
"nsis": {
"displayLanguageSelector": true
}
}
}
}

🌐 Minimum Webview2 version

如果你的应用需要仅在较新版本的 Webview2 中可用的功能(例如自定义 URI 方案),你可以指示 Windows 安装程序验证当前的 Webview2 版本,并在其与目标版本不匹配时运行 Webview2 引导程序。

🌐 If your app requires features only available in newer Webview2 versions (such as custom URI schemes), you can instruct the Windows installer to verify the current Webview2 version and run the Webview2 bootstrapper if it does not match the target version.

tauri.conf.json
{
"bundle": {
"windows": {
"nsis": {
"minimumWebview2Version": "110.0.1531.0"
}
}
}
}

Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站