tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk

官网链接:What is Tauri? | Tauri

初始准备

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
  1. 搜索 环境变量 ,打开 系统环境变量设置

  2. 系统变量 中点击 新建

    • 名称:NDK_HOME
  3. 值: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 :从 AdoptiumOracle 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,惊呆了

相关推荐
黄林晴14 小时前
如何判断手机是否是纯血鸿蒙系统
android
火柴就是我14 小时前
flutter 之真手势冲突处理
android·flutter
法的空间14 小时前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
循环不息优化不止14 小时前
深入解析安卓 Handle 机制
android
恋猫de小郭14 小时前
Android 将强制应用使用主题图标,你怎么看?
android·前端·flutter
jctech15 小时前
这才是2025年的插件化!ComboLite 2.0:为Compose开发者带来极致“爽”感
android·开源
用户20187928316715 小时前
为何Handler的postDelayed不适合精准定时任务?
android
叽哥15 小时前
Kotlin学习第 8 课:Kotlin 进阶特性:简化代码与提升效率
android·java·kotlin
Cui晨15 小时前
Android RecyclerView展示List<View> Adapter的数据源使用View
android
氦客15 小时前
Android Doze低电耗休眠模式 与 WorkManager
android·suspend·休眠模式·workmanager·doze·低功耗模式·state_doze