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,惊呆了

相关推荐
2501_9371892317 小时前
2025 优化版神马影视 8.8 源码系统|零基础部署
android·源码·开源软件·源代码管理·机顶盒
モンキー・D・小菜鸡儿19 小时前
Android Jetpack Compose 基础控件介绍
android·kotlin·android jetpack·compose
无风之翼19 小时前
android15 休眠唤醒过程中有时候屏幕显示时间一闪而过
android·锁屏
如此风景21 小时前
iOS SwiftUI开发所有修饰符使用详解
ios
mumuWorld21 小时前
KSCrash 实现机制深度分析
ios·源码阅读
方白羽21 小时前
Android全局悬浮拖拽视图
android·app·客户端
AskHarries1 天前
Google 登录问题排查指南
flutter·ios·app
Jerry1 天前
Compose 高级状态和附带效应
android
崽崽长肉肉1 天前
Swift中的知识点总结
ios·swift
2501_916007471 天前
苹果手机iOS应用管理全指南与隐藏功能详解
android·ios·智能手机·小程序·uni-app·iphone·webview