终端运行

1
pnpm tauri android build

出现报错

1
2
3
Have you installed the Android SDK? The `ANDROID_HOME` environment variable isn't set, and is required: environment variable not found: environment variable not found
Error Have you installed the Android SDK? The `ANDROID_HOME` environment variable isn't set, and is required: environment variable not found: environment variable not found
 ELIFECYCLE  Command failed with exit code 1.
1
2
不用android Studio,只用SDK就可以了
——ZTY

需要下载适用于Windows的Command-line tools。

1
https://developer.android.com/studio?hl=zh-cn#command-tools

往下翻,上面全都是AndroidStudio的东西。

然后下面有一个:“仅限命令行工具”

然后点windows的,下载。

解压下载的ZIP文件到一个你想要存放SDK的位置,例如 D:\Program Files\android-sdk

现在变成这样

1
D:\Program Files\android-sdk\cmdline-tools

然后到

1
D:\Program Files\android-sdk\cmdline-tools\bin

这个目录下,打开powershell,可以看到有sdkmanager.bat

然后运行以下命令来更新SDK管理器并安装所需的组件:

1
2
.\sdkmanager --sdk_root=D:\android-sdk --update
.\sdkmanager --sdk_root=D:\android-sdk "platform-tools" "platforms;android-33" "build-tools;33.0.0"

但这一步可能会出现java版本不够的问题,还要去安装一下新的java版本。

这里我们选择了API级别33作为示例,但你可以根据需要选择其他版本。platform-tools 包含了adb和其他工具,platforms;android-33 是目标安卓平台,而 build-tools;33.0.0 则是构建工具。

3. 设置环境变量

为了使系统能够识别安卓SDK的位置,你需要设置 ANDROID_HOMEPATH 环境变量。

  • 打开“控制面板” -> “系统和安全” -> “系统” -> “高级系统设置”。
  • 点击“环境变量”按钮。
  • 在“系统变量”部分点击“新建”来创建 ANDROID_HOME 变量,并将其值设为你的SDK路径,例如 D:\Program Files\android-sdk
  • 编辑Path变量,添加两个新条目:
    • %ANDROID_HOME%\platform-tools
    • %ANDROID_HOME%\cmdline-tools\bin

确保这些路径正确无误,并且指向你解压的SDK文件夹。

1
注:这里实际运行了之前的Powershell的指令后,发现D盘根目录下有多了个android-sdk,和我自己创建的不是一个位置了,算了,干脆就D盘根目录那个得了

4. 验证安装

重新启动命令提示符或PowerShell以应用新的环境变量,然后输入以下命令来验证是否成功设置了SDK:

1
adb version

如果显示了ADB的版本信息,说明环境变量设置正确,SDK已经成功安装。

5. 尝试再次构建

返回到你的Tauri项目目录,并再次尝试运行构建命令:

powershell深色版本

1
pnpm tauri android build

结果又发现,还要安装NDK

以下是报错信息

1
2
3
Have you installed the NDK? The `NDK_HOME` environment variable isn't set, and is required: environment variable not found: environment variable not found
Error Have you installed the NDK? The `NDK_HOME` environment variable isn't set, and is required: environment variable not found: environment variable not found
 ELIFECYCLE  Command failed with exit code 1.

什么是NDK?

1
2
3
4
NDK(Native Development Kit)是安卓开发者工具集的一部分,允许你在安卓应用中编写原生代码(C或C++)。NDK主要用于以下几种情况:
性能关键的应用:对于需要高性能计算的任务,如图形处理、音频/视频编码解码、物理模拟等,使用C/C++可以提供更高效的执行。
复用现有的C/C++库:如果你已经有一个用C/C++编写的库,并希望在安卓应用中使用它,NDK可以帮助你将这些库集成到安卓项目中。
硬件访问:某些情况下,直接访问硬件特性(如传感器、相机等)可能需要使用NDK。

可以通过 sdkmanager 来安装NDK,或者手动下载并安装。

如果是通过sdkmanager安装:

1
.\sdkmanager --sdk_root=D:\android-sdk "ndk;25.2.9519653"

这里我们选择了版本 25.2.9519653 作为示例,但你可以根据需要选择其他版本。你可以通过 .\sdkmanager --sdk_root=D:\android-sdk --list 查看可用的NDK版本。

1
https://developer.android.com/ndk/downloads?spm=5176.28103460.0.0.297c5d27aPFGKR&hl=zh-cn

在这里下载NDK

下载完成后,解压到一个你想要存放NDK的位置,例如 D:\android-ndk

2. 设置 NDK_HOME 环境变量

为了让系统能够找到NDK,你需要设置 NDK_HOME 环境变量。

  1. 打开环境变量设置
    • Win + R 打开“运行”对话框,输入 sysdm.cpl 并按回车。
    • 在“系统属性”窗口中,点击“高级”选项卡,然后点击“环境变量”按钮。
  2. **设置 NDK_HOME**:
    • 在“系统变量”部分,点击“新建”按钮。
    • 输入变量名为 NDK_HOME
    • 输入变量值为NDK的路径,例如 D:\android-sdk\ndk\25.2.9519653D:\android-ndk(如果你手动下载并解压)。
    • 点击“确定”。
  3. **更新 Path**:
    • 在“系统变量”部分,找到并选择 Path 变量,然后点击“编辑”按钮。
    • 点击“新建”,然后输入 %NDK_HOME%\toolchains\llvm\prebuilt\windows-x86_64\bin
    • 点击“确定”保存更改。
  4. 应用更改
    • 点击“确定”关闭所有窗口,使环境变量生效。

至此,可以运行打包命令了。

但打包命令运行后还会报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
D:\Projects\Project-Tools\project-graph>pnpm tauri android build

> project-graph@0.1.0 tauri D:\Projects\Project-Tools\project-graph
> tauri "android" "build"

Running beforeBuildCommand `pnpm build`

> project-graph@0.1.0 build D:\Projects\Project-Tools\project-graph
> tsc && vite build

vite v6.0.3 building for production...
08:11:44 [generouted] scanned 14 routes in 10 ms
✓ 1838 modules transformed.
dist/index.html 0.54 kB │ gzip: 0.34 kB
dist/assets/icon-Dk6gBW_T.png 18.41 kB
dist/assets/MiSans-Medium-BI47lJDT.woff2 4,928.04 kB
dist/assets/MiSans-Bold-BoL4u17n.woff2 5,067.65 kB
dist/assets/index-PCSmR35J.css 25.05 kB │ gzip: 4.92 kB
dist/assets/zh-TW-BeZ0zjlT.js 3.31 kB │ gzip: 3.16 kB
dist/assets/zh-CN-DN8c-byM.js 4.78 kB │ gzip: 4.75 kB
dist/assets/en-CpaVvmDx.js 9.99 kB │ gzip: 4.06 kB
dist/assets/index-CmMmwWGG.js 768.42 kB │ gzip: 231.42 kB

(!) Some chunks are larger than 500 kB after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
✓ built in 7.30s
Downloaded android_system_properties v0.1.5
Downloaded android-tzdata v0.1.1
Downloaded cesu8 v1.1.0
Downloaded combine v4.6.7
Downloaded tao-macros v0.1.3
Downloaded linux-raw-sys v0.4.14
Downloaded proc-macro-crate v3.2.0
Downloaded iana-time-zone v0.1.61
Downloaded num_enum v0.7.3
Downloaded jni-sys v0.3.0
Downloaded is-wsl v0.4.0
Downloaded pathdiff v0.2.2
Downloaded num_enum_derive v0.7.3
Downloaded errno v0.3.9
Downloaded jni v0.21.1
Downloaded ndk-sys v0.6.0+11769913
Downloaded ndk v0.9.0
Downloaded signal-hook-registry v1.4.2
Downloaded rustix v0.38.40
Downloaded windows-targets v0.42.2
Downloaded is-docker v0.2.0
Downloaded ndk-context v0.1.1
Downloaded windows-sys v0.45.0
Downloaded 23 crates (5.6 MB) in 7.42s (largest was `windows-sys` at 2.6 MB)
Compiling serde v1.0.215
Compiling libc v0.2.164
Compiling cfg-if v1.0.0
Compiling toml_edit v0.22.22
Compiling stable_deref_trait v1.2.0
Compiling smallvec v1.13.2
Compiling once_cell v1.20.2
Compiling zerofrom v0.1.4
Compiling itoa v1.0.11
Compiling memchr v2.7.4
Compiling writeable v0.5.5
Compiling siphasher v0.3.11
Compiling litemap v0.7.3
Compiling fnv v1.0.7
Compiling icu_locid_transform_data v1.5.0
Compiling log v0.4.22
Compiling icu_properties_data v1.5.0
Compiling percent-encoding v2.3.1
error[E0463]: can't find crate for `core`
|
= note: the `aarch64-linux-android` target may not be installed
= help: consider downloading the target with `rustup target add aarch64-linux-android`

For more information about this error, try `rustc --explain E0463`.
error: could not compile `cfg-if` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `smallvec` (lib) due to 1 previous error
error[E0463]: can't find crate for `std`
|
= note: the `aarch64-linux-android` target may not be installed
= help: consider downloading the target with `rustup target add aarch64-linux-android`

error: could not compile `stable_deref_trait` (lib) due to 1 previous error
error: could not compile `once_cell` (lib) due to 1 previous error
error: could not compile `itoa` (lib) due to 1 previous error
error: could not compile `zerofrom` (lib) due to 1 previous error
error: could not compile `memchr` (lib) due to 1 previous error
error: could not compile `writeable` (lib) due to 1 previous error
error: could not compile `siphasher` (lib) due to 1 previous error
error: could not compile `litemap` (lib) due to 1 previous error
error: could not compile `fnv` (lib) due to 1 previous error
error: could not compile `icu_locid_transform_data` (lib) due to 1 previous error
error: could not compile `log` (lib) due to 1 previous error
error: could not compile `icu_properties_data` (lib) due to 1 previous error
error: could not compile `percent-encoding` (lib) due to 1 previous error
error: could not compile `serde` (lib) due to 1 previous error
`Failed to run `cargo build`: command ["cargo", "build", "--package", "project-graph", "--manifest-path", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\Cargo.toml", "--target", "aarch64-linux-android", "--features", "tauri/custom-protocol tauri/rustls-tls", "--lib", "--release"] exited with code 101
Error `Failed to run `cargo build`: command ["cargo", "build", "--package", "project-graph", "--manifest-path", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\Cargo.toml", "--target", "aarch64-linux-android", "--features", "tauri/custom-protocol tauri/rustls-tls", "--lib", "--release"] exited with code 101
 ELIFECYCLE  Command failed with exit code 1.

解决方法是:

添加 aarch64-linux-android 目标

1
rustup target add aarch64-linux-android

build了一下发现 tauri.conf.json 里的版本不能是 0.0.0-dev,起码是0.0.1

改了一下又报错了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
warning: `project-graph` (lib) generated 3 warnings (run `cargo fix --lib -p project-graph` to apply 2 suggestions)
Finished `release` profile [optimized] target(s) in 1m 35s
Info symlinking lib "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\target\\aarch64-linux-android\\release\\libproject_graph_lib.so" in jniLibs dir "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\app/src/main/jniLibs/arm64-v8a"
Failed to create a symbolic link from "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\target\\aarch64-linux-android\\release\\libproject_graph_lib.so" to file "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\app/src/main/jniLibs/arm64-v8a\\libproject_graph_lib.so" (file clobbering enabled):
Creation symbolic link is not allowed for this system.

For Windows 10 or newer:
You should use developer mode.
See https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development

For Window 8.1 or older:
You need `SeCreateSymbolicLinkPrivilege` security policy.
See https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links
Error Failed to create a symbolic link from "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\target\\aarch64-linux-android\\release\\libproject_graph_lib.so" to file "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\app/src/main/jniLibs/arm64-v8a\\libproject_graph_lib.so" (file clobbering enabled):
Creation symbolic link is not allowed for this system.

For Windows 10 or newer:
You should use developer mode.
See https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development

For Window 8.1 or older:
You need `SeCreateSymbolicLinkPrivilege` security policy.
See https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links
 ELIFECYCLE  Command failed with exit code 1.

按照提示打开网站

1
https://learn.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development

原来说是win10及以上要在设置里打开开发者模式

然后再试一次,发现构建的时间挺长,等了大概快6分钟了,终于……报错了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':buildSrc'.
> Could not resolve all artifacts for configuration ':buildSrc:classpath'.
> Could not resolve org.gradle.kotlin:gradle-kotlin-dsl-plugins:4.4.0.
Required by:
project :buildSrc > org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin:4.4.0
> Could not resolve org.gradle.kotlin:gradle-kotlin-dsl-plugins:4.4.0.
> Could not get resource 'https://plugins.gradle.org/m2/org/gradle/kotlin/gradle-kotlin-dsl-plugins/4.4.0/gradle-kotlin-dsl-plugins-4.4.0.pom'.
> Could not GET 'https://plugins.gradle.org/m2/org/gradle/kotlin/gradle-kotlin-dsl-plugins/4.4.0/gradle-kotlin-dsl-plugins-4.4.0.pom'.
> Read timed out

* Try:
> 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.

BUILD FAILED in 7m 14s
Failed to assemble APK: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1
Error Failed to assemble APK: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1
 ELIFECYCLE  Command failed with exit code 1.

看样子好像是下载某个东西发送GET请求超时了?

首先,确保你的网络连接稳定,并且可以访问外部资源。你可以尝试在浏览器中打开 https://plugins.gradle.org/m2/org/gradle/kotlin/gradle-kotlin-dsl-plugins/4.4.0/gradle-kotlin-dsl-plugins-4.4.0.pom,看看是否能够正常加载。

试了一下发现可以加载,一瞬间就下载了一个东西。

等了20分钟,太漫长了,又报错了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Kotlin does not yet support 22 JDK target, falling back to Kotlin JVM_21 JVM target
Kotlin does not yet support 22 JDK target, falling back to Kotlin JVM_21 JVM target
w: Inconsistent JVM-target compatibility detected for tasks 'compileJava' (22) and 'compileKotlin' (21).
This will become an error in Gradle 8.0.
Consider using JVM Toolchain: https://kotl.in/gradle/jvm/toolchain
Learn more about JVM-target validation: https://kotl.in/gradle/jvm/target-validation


FAILURE: Build failed with an exception.

* Where:
Build file 'D:\Projects\Project-Tools\project-graph\src-tauri\gen\android\app\build.gradle.kts' line: 37

* What went wrong:
null cannot be cast to non-null type kotlin.String

* Try:
> 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.

BUILD FAILED in 20m 2s
Failed to assemble APK: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1
Error Failed to assemble APK: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1
 ELIFECYCLE  Command failed with exit code 1.

发现Kotlin 和 Java 版本不一致

1
src-tauri\gen\android\app\build.gradle.kts

又不一样了,下载java11版本。

不对,17版本,于是又下了个jdk17,并把javaHome环境变量切换一下。

然后再打包一下,又报错了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
w: file:///D:/Rust/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.1.1/mobile/android/src/main/java/app/tauri/plugin/PluginMethodData.kt:11:23 Parameter 'methodDecorator' is never used
error[E0463]: can't find crate for `core`
|DLE
= note: the `armv7-linux-androideabi` target may not be installed
= help: consider downloading the target with `rustup target add armv7-linux-androideabi`

For more information about this error, try `rustc --explain E0463`.
error: could not compile `cfg-if` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `smallvec` (lib) due to 1 previous error
error[E0463]: can't find crate for `std`
|DLE
= note: the `armv7-linux-androideabi` target may not be installed
= help: consider downloading the target with `rustup target add armv7-linux-androideabi`

error: could not compile `stable_deref_trait` (lib) due to 1 previous error
error: could not compile `once_cell` (lib) due to 1 previous error
error: could not compile `zerofrom` (lib) due to 1 previous error
error: could not compile `itoa` (lib) due to 1 previous error
error: could not compile `writeable` (lib) due to 1 previous error
error: could not compile `litemap` (lib) due to 1 previous error
error: could not compile `memchr` (lib) due to 1 previous error
error: could not compile `siphasher` (lib) due to 1 previous error
error: could not compile `icu_locid_transform_data` (lib) due to 1 previous error
error: could not compile `fnv` (lib) due to 1 previous error
error: could not compile `log` (lib) due to 1 previous error
error: could not compile `icu_properties_data` (lib) due to 1 previous error
error: could not compile `write16` (lib) due to 1 previous error
error: could not compile `percent-encoding` (lib) due to 1 previous error
error: could not compile `libc` (lib) due to 1 previous error
`Failed to run `cargo build`: command ["cargo", "build", "--package", "project-graph", "--manifest-path", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\Cargo.toml", "--target", "armv7-linux-androideabi", "--features", "tauri/custom-protocol tauri/rustls-tls tauri/custom-protocol tauri/rustls-tls", "--lib", "--release"] exited with code 101
Error `Failed to run `cargo build`: command ["cargo", "build", "--package", "project-graph", "--manifest-path", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\Cargo.toml", "--target", "armv7-linux-androideabi", "--features", "tauri/custom-protocol tauri/rustls-tls tauri/custom-protocol tauri/rustls-tls", "--lib", "--release"] exited with code 101 IDLE
?ELIFECYCLE? Command failed with exit code 1.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:rustBuildArmRelease'.
> Process 'command 'pnpm.cmd'' finished with non-zero exit value 1

* Try:
> 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.

BUILD FAILED in 3m 20s
Failed to assemble APK: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1
Error Failed to assemble APK: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1: command ["D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android\\gradlew.bat", "--project-dir", "D:\\Projects\\Project-Tools\\project-graph\\src-tauri\\gen/android"] exited with code 1
 ELIFECYCLE  Command failed with exit code 1.

原来还要再装一个

1
rustup target add armv7-linux-androideabi

这一步安装好像等待时间挺长,等了20分钟好像

装了一下发现还有 i686-linux-android

1
rustup target add i686-linux-android

还有

1
rustup target add x86_64-linux-android

好像没了

但又报错了

1
2
3
* What went wrong:
Execution failed for task ':app:validateSigningUniversalRelease'.
> Keystore file 'D:\Projects\Project-Tools\project-graph\src-tauri\gen\android\app\D:ProjectsProject-Toolsproject-graphkeystore.jks' not found for signing config 'release'.

在windows下jks文件似乎应该放在 \src-tauri\gen\android\app\ 目录下才行

放对位置就没问题了,终于可以打包了。从早晨6点折腾到下午两点了。