Tauri 是一个开源框架,用于构建轻量级、高性能的桌面应用程序。它是使用 Web 技术(如 HTML、CSS 和 JavaScript)来创建用户界面,同时利用 Rust 语言提供的api功能。Tauri 的目标是提供一种更高效、更安全的方式来开发跨平台的桌面应用程序。
Tauri1.0版本已经非常完善的支持所有桌面端了,现在的2.0已经开始测试移动端开发了,我们可以提前尝鲜一下。
安装Tauri2.0
安装最新版本的tauri,才可以创建移动端工程
pnpm create tauri-app --beta
或者
npm create tauri-app@latest -- --beta
或者
yarn create tauri-app --beta
或者
cargo install create-tauri-app
cargo create-tauri-app --beta
初始化移动端工程
默认只会支持运行桌面端,想要支持移动端,就要先生成移动端公工程。要先运行init初始化,然后才可以开启dev或者build移动端代码。下面的代码如果有使用npm或者yarn或者pnpm,就需要在开头加上npm run 或者 yarn run 或者 pnpm run 命令。
初始化移动端:
// ios初始化
npm run tauri ios init
// andriod初始化
npm run tauri android init
运行提示:
可能会需要安装cocoapods 或者更新一些依赖,按照提示安装或者更新就好了,看到Make cool apps! 🌻 🐕 🎉就说明成功了
> tauri-all@0.0.0 ios init /Users/song/Project/my/tauri-all
> tauri ios initInfo detected rustc version 1.76.0 (07dca489a 2024-2-4)
/opt/homebrew/bin/xcodegen
Info package `xcodegen` present: true
/opt/homebrew/bin/idevicesyslog
Info package `libimobiledevice` present: true
/usr/local/bin/pod
Info package `cocoapods` present: true
Outdated dependencies:
- `xcodegen` is at 2.39.1; latest version is 2.40.1
Would you like these outdated dependencies to be updated for you? [Y/n]: Y
/opt/homebrew/Cellar/xcodegen/2.39.1/bin/xcodegen
/opt/homebrew/Cellar/xcodegen/2.39.1/share/xcodegen/ (31 files)
==> Fetching xcodegen
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/xcodegen-2.40.1.arm64_sonoma.bottle.tar.gz
######################################################################################################### 100.0%
==> Reinstalling xcodegen
==> Pouring xcodegen-2.40.1.arm64_sonoma.bottle.tar.gz
🍺 /opt/homebrew/Cellar/xcodegen/2.40.1: 37 files, 7.6MB
==> Running `brew cleanup xcodegen`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Removing: /Users/song/Library/Caches/Homebrew/xcodegen--2.39.1... (2.3MB)
Info "/Users/song/Project/my/tauri-all/src-tauri" relative to "/Users/song/Project/my/tauri-all/src-tauri/gen/apple" is "../../"
Generating Xcode project...
⚙️ Generating plists...
⚙️ Generating project...
⚙️ Writing project...
Created project at /Users/song/Project/my/tauri-all/src-tauri/gen/apple/tauri-all.xcodeproj
victory: Project generated successfully!
Make cool apps! 🌻 🐕 🎉
* 终端将被任务重用,按任意键关闭。
运行完会在项目路径下生成apple工程:
运行移动端工程
需要先运行上一步的init操作,才可以运行这一步哦,不然会报错提示的
// ios dev
npm run tauri ios dev
// android dev
npm run tauri android dev
第一次如果运行报错:说明需要初始化ios工程
> tauri-all@0.0.0 ios /Users/song/Project/my/tauri-all
> tauri ios dev/opt/homebrew/bin/ios-deploy
Info package `ios-deploy` present: true
Detected iOS simulators:
[0] Apple Vision Pro
[1] iPad (10th generation)
[2] iPad Air (5th generation)
[3] iPad Pro (11-inch) (4th generation)
[4] iPad Pro (12.9-inch) (6th generation)
[5] iPad mini (6th generation)
[6] iPhone 15
[7] iPhone 15 Plus
[8] iPhone 15 Pro
[9] iPhone 15 Pro Max
[10] iPhone SE (3rd generation)
Enter an index for a simulator above.
Simulator: 6
Info Starting simulator iPhone 15
Error Xcode project directory /Users/song/Project/my/tauri-all/src-tauri/gen/apple doesn't exist. Please run `tauri ios init` and try again.
ELIFECYCLE Command failed with exit code 1.
打包移动端
移动端编译命令如下:
// ios
npm run tauri ios build
// android
npm run tauri android build
运行效果:
配置package.json
将以上命令配置到package.json里面,就可以很方便的运行相关的命令了:
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview",
"tauri": "tauri dev",
"ios init": "tauri ios init",
"ios dev": "tauri ios dev",
"ios build": "tauri ios build",
"android init": "tauri android init",
"android dev": "tauri android dev",
"android build": "tauri android build"
},
错误问题解决
1.如果运行出现提示:
> tauri-all@0.0.0 dev /Users/song/Project/my/tauri-all
> vite
VITE v5.2.11 ready in 148 ms➜ Local: http://localhost:1420/
➜ Network: use --host to expose
Warn Waiting for your frontend dev server to start on http://192.168.1.18:1420/...
Warn Waiting for your frontend dev server to start on http://192.168.1.18:1420/...
说明需要开启本地ip服务,在vite.config.ts里面添加:host: '0.0.0.0',
然后再开启服务,就没有这个提示了。
2.ios以失败告终☹️
> tauri-all@0.0.0 tauri /Users/song/Project/my/tauri-all
> tauri dev "ios" "xcode-script" "-v" "--platform" "iOS" "Simulator" "--sdk-root" "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.4.sdk" "--framework-search-paths" "/Users/song/Library/Developer/Xcode/DerivedData/tauri-all-hclwbtkmftykqbcxwjkykkdptclq/Build/Products/debug-iphonesimulator \".\"" "--header-search-paths" "/Users/song/Library/Developer/Xcode/DerivedData/tauri-all-hclwbtkmftykqbcxwjkykkdptclq/Build/Products/debug-iphonesimulator/include " "--gcc-preprocessor-definitions" " DEBUG=1" "--configuration" "debug" "arm64-sim"Running BeforeDevCommand (`pnpm dev`)
error: unexpected argument 'ios' foundUsage: cargo build [OPTIONS]
For more information, try '--help'.
Info Watching /Users/song/Project/my/tauri-all/src-tauri for changes...
ELIFECYCLE Command failed with exit code 1.note: Run script build phase 'Build Rust Code' will be run during every build because the option to run the script phase "Based on dependency analysis" is unchecked. (in target 'tauri-all_iOS' from project 'tauri-all')
** BUILD FAILED **
The following build commands failed:
PhaseScriptExecution Build\ Rust\ Code /Users/song/Library/Developer/Xcode/DerivedData/tauri-all-hclwbtkmftykqbcxwjkykkdptclq/Build/Intermediates.noindex/tauri-all.build/debug-iphonesimulator/tauri-all_iOS.build/Script-A4488FA325FA75FF5FA88F9C.sh (in target 'tauri-all_iOS' from project 'tauri-all')
(1 failure)
Error command ["xcodebuild"] exited with code 65
ELIFECYCLE Command failed with exit code 1.