初始准备
rust版本一定要1.77.2以上的版本,查看版本和升级版本:
升级命名:
rustup update
不然会报错:
error: package `tauri-plugin-shell v2.0.2` cannot be built because it requires rustc 1.77.2 or newer, while the currently active rustc version is 1.76.0
Either upgrade to rustc 1.77.2 or newer, or use
cargo update tauri-plugin-shell@2.0.2 --precise ver
where `ver` is the latest version of `tauri-plugin-shell` supporting rustc 1.76.0
Failed to run `cargo build`: command ["cargo", "build", "--package", "mobiltauri", "--manifest-path", "/Users/song/Project/my/MobilTauri/src-tauri/Cargo.toml", "--target", "aarch64-apple-ios-sim", "--features", "tauri/rustls-tls", "--lib", "--no-default-features"] exited with code 101
Error Failed to run `cargo build`: command ["cargo", "build", "--package", "mobiltauri", "--manifest-path", "/Users/song/Project/my/MobilTauri/src-tauri/Cargo.toml", "--target", "aarch64-apple-ios-sim", "--features", "tauri/rustls-tls", "--lib", "--no-default-features"] exited with code 101
ELIFECYCLE Command failed with exit code 1.
Command PhaseScriptExecution failed with a nonzero exit code
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 'mobiltauri_iOS' from project 'mobiltauri')
** BUILD FAILED **
创建项目
npm create tauri-app@latest
然后输入项目相关信息。
进入新创建的项目
初始化环境:
pnpm install
android开发:
pnpm tauri android init
pnpm tauri android dev
ios开发:
pnpm tauri ios init
pnpm tauri ios dev
桌面端开发:
pnpm tauri dev
常见错误
ios要给项目分配ios开发者id:
/Users/song/Project/my/MobilTauri/src-tauri/gen/apple/mobiltauri.xcodeproj: error: Signing for "mobiltauri_iOS" requires a development team. Select a development team in the Signing & Capabilities editor. (in target 'mobiltauri_iOS' from project 'mobiltauri')
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 'mobiltauri_iOS' from project 'mobiltauri')
** BUILD FAILED **
command ["xcodebuild"] exited with code 65
Error command ["xcodebuild"] exited with code 65
ELIFECYCLE Command failed with exit code 1.
song$ pnpm tauri ios dev
打开src-tauri/gen/apple路径,使用xcode打开,然后在设置里面配置开发者账号:
开发者账号和证书不要有过期的,不然就会报错。
运行后的效果:
android安装报错:
action request: to initialize Android environment; Android support won't be usable until you fix the issue
below and re-run `tauri android init`!
Have you installed the NDK? The `NDK_HOME` environment variable isn't set, and is required: environment
variable not found
victory: Project generated successfully!
Make cool apps! 🌻 🐕 🎉
需要安装ndk:
点击apply and ok:
安装好之后要配置环境变量。
在您的操作系统中配置 NDK_HOME
环境变量:
NDK的版本号:
Windows
-
搜索 环境变量 ,打开 系统环境变量设置。
-
在 系统变量 中点击 新建 :
- 名称:
NDK_HOME
- 名称:
-
值:Android NDK 的安装路径,例如:
C:\Users\<your-username>\AppData\Local\Android\Sdk\ndk\XX.Y.Z
确保将路径添加到
PATH
变量中,例如:%NDK_HOME%\bin
macOS / Linux
编辑 ~/.bashrc
或 ~/.zshrc
文件,添加以下内容:
export NDK_HOME=~/Library/Android/sdk/ndk/XX.Y.Z
export PATH=$NDK_HOME/bin:$PATH
替换 XX.Y.Z
为 NDK 实际版本号。
使用以下命令应用更改:
source ~/.bashrc
# 或
source ~/.zshrc
运行以下命令以确认 NDK 是否可用:
echo $NDK_HOME
ndk-build --version
立即生效:
source ~/.bash_profile
或者
source ~/.zshrc
如果输出 NDK 的路径和版本号,则配置成功。
然后运行:
pnpm tauri android init
pnpm tauri android dev
JDK版本太低报错:
* What went wrong:
Execution failed for task ':buildSrc:compileKotlin'.
> Could not resolve all files for configuration ':buildSrc:compileClasspath'.
> Could not resolve com.android.tools.build:gradle:8.5.1.
Required by:
project :buildSrc
> Dependency requires at least JVM runtime version 11. This build uses a Java 8 JVM.
* Try:
> Run this build using a Java 11 or newer JVM.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
升级JDK:
Windows/Mac :从 Adoptium 或 Oracle JDK 下载并安装 Java 11 或更高版本
查看java版本:
升级java版本之后,可能会导致NDK重新实效问题,这个时候需要重新运行命令:
pnpm tauri android init
然后再次运行就可以了:
运行后的效果:
打包编译
打包为apk安装包:
pnpm tauri android build
编译后的文件存在路径:
/Users/song/Project/my/MobilTauri/src-tauri/gen/android/app/build/outputs/apk/universal/release/app-universal-release-unsigned.apk
打包后的universal的apk大小:39.5M
arm64位的apk大小: 163M,惊呆了