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

相关推荐
liang_jy7 小时前
Android SparseArray
android·源码
liang_jy7 小时前
Activity 启动流程扩展篇(一)—— startActivityInner 任务决策全解析
android·源码
NPE~8 小时前
[App逆向]脱壳实战
android·教程·逆向·android逆向·逆向分析
木易 士心8 小时前
别再只会用 drawCircle 了!一文搞懂 Android Canvas 底层机制
android
@大迁世界9 小时前
14个你现在必须关闭的 iOS 26 设置,不然手机很快被它榨干
macos·ios·智能手机·objective-c·cocoa
AtOR CUES10 小时前
MySQL——表操作及查询
android·mysql·adb
怣疯knight11 小时前
安卓App无法增加自定义图片作为图标功能
android
jinanwuhuaguo12 小时前
OpenClaw联邦之心——从孤岛记忆到硅基集体潜意识的拓扑学革命(第二十三篇)
android·人工智能·kotlin·拓扑学·openclaw
Gary Studio14 小时前
安卓HAL C++基础-命名域
android
诸神黄昏EX15 小时前
Android Google XTS
android