Skip to content

Commit d89d8fa

Browse files
feat(cli): warn when Java is too new for the bundled Gradle (#15780)
* feat(cli): warn when Java is too new for the bundled Gradle Installing a very recent JDK (e.g. Java 25/26) while Tauri ships Gradle 8.14 makes Android builds fail with a cryptic 'Failed to assemble APK' error. Detect the active Java feature version during the Android env setup and warn upfront when it exceeds what the bundled Gradle can run, pointing at the Gradle/Java compatibility matrix. Closes #15385 * refactor(cli): detect Java version without spawning a subprocess Read the major version from the JDK's `release` metadata file instead of running `java -version` on every Android command, so the compatibility warning no longer adds process-spawn overhead to init/dev/build. * feat(cli): derive Java/Gradle compat from the project's Gradle version Read the Gradle version from the project's gradle-wrapper.properties instead of a hardcoded template constant, so the warning stays correct when users upgrade Gradle. Skip the warning when the project or Gradle version can't be determined (and for Gradle 9+, which tracks new Java quickly). * refactor(cli): move Android Java/Gradle checks to a dedicated module Extract the Java detection and Java/Gradle compatibility checks out of android/mod.rs into android/java.rs, and recommend the exact supported Java ceiling in the warning instead of a stale LTS list. Addresses @Legend-Master's feedback. * revert ensure java * move the check to get_config the project path is only available after it * check 9.1 and 9.4 --------- Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com> Co-authored-by: Tony <legendmastertony@gmail.com>
1 parent d727d63 commit d89d8fa

3 files changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:enhance
3+
"@tauri-apps/cli": patch:enhance
4+
---
5+
6+
Warn during Android commands (`init`/`dev`/`build`) when the active Java version is too new for the Gradle version Tauri ships (e.g. Java 25+ against Gradle 8.14), instead of letting the build fail later with a cryptic error. The warning points to the Gradle/Java compatibility matrix and suggests a supported JDK.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
//! Java detection and Java/Gradle compatibility checks for Android builds.
6+
7+
use std::path::{Path, PathBuf};
8+
9+
pub fn check_java_gradle_versions() -> Option<()> {
10+
let java_major_version = java_major_version()?;
11+
let project_gradle_version = project_gradle_version()?;
12+
let gradle_max_supported_java = gradle_max_supported_java(&project_gradle_version)?;
13+
// Read the Gradle version this project actually uses (the user may have upgraded it away from
14+
// the template default) and only warn when the detected Java is too new for that Gradle.
15+
if java_major_version > gradle_max_supported_java {
16+
log::warn!(
17+
"Detected Java {java_major_version}, but Gradle {project_gradle_version} used by this project can only run on Java up to {gradle_max_supported_java}. \
18+
Android builds will likely fail with a cryptic error. Install a JDK that Gradle {project_gradle_version} supports \
19+
(Java {gradle_max_supported_java} or older) and point JAVA_HOME at it. \
20+
See https://docs.gradle.org/current/userguide/compatibility.html"
21+
);
22+
}
23+
Some(())
24+
}
25+
26+
/// Reads the Gradle version this project is pinned to from its
27+
/// `gradle/wrapper/gradle-wrapper.properties` `distributionUrl` (e.g.
28+
/// `gradle-8.14-bin.zip` -> `8.14`). Returns `None` when the project isn't
29+
/// generated yet or the file can't be parsed, in which case no warning is shown.
30+
fn project_gradle_version() -> Option<String> {
31+
let project_path = std::env::var_os("TAURI_ANDROID_PROJECT_PATH")?;
32+
let properties = std::fs::read_to_string(
33+
Path::new(&project_path).join("gradle/wrapper/gradle-wrapper.properties"),
34+
)
35+
.ok()?;
36+
properties.lines().find_map(|line| {
37+
let url = line.trim().strip_prefix("distributionUrl=")?;
38+
let file = url.rsplit('/').next()?;
39+
let version = file.strip_prefix("gradle-")?.split('-').next()?;
40+
(!version.is_empty()).then(|| version.to_string())
41+
})
42+
}
43+
44+
/// Highest Java feature version a given Gradle release can *run* on.
45+
/// Known Gradle 7.x through 9.x releases are mapped. Unknown future major
46+
/// versions return `None` to avoid false warnings. See
47+
/// <https://docs.gradle.org/current/userguide/compatibility.html>.
48+
fn gradle_max_supported_java(version: &str) -> Option<u32> {
49+
let mut parts = version.split('.');
50+
let major: u32 = parts.next()?.parse().ok()?;
51+
let minor: u32 = parts.next().and_then(|m| m.parse().ok()).unwrap_or(0);
52+
53+
if major >= 10 {
54+
return None;
55+
}
56+
57+
Some(match (major, minor) {
58+
v if v >= (9, 4) => 26,
59+
v if v >= (9, 1) => 25,
60+
v if v >= (8, 14) => 24,
61+
v if v >= (8, 10) => 23,
62+
v if v >= (8, 8) => 22,
63+
v if v >= (8, 5) => 21,
64+
v if v >= (8, 3) => 20,
65+
v if v >= (7, 6) => 19,
66+
v if v >= (7, 5) => 18,
67+
v if v >= (7, 3) => 17,
68+
_ => return None,
69+
})
70+
}
71+
72+
/// Detects the major (feature) version of the active Java installation by
73+
/// reading the `release` metadata file shipped inside every JDK, preferring
74+
/// `JAVA_HOME` and falling back to the `java` binary on `PATH`. This avoids
75+
/// spawning a `java -version` subprocess on every Android command.
76+
fn java_major_version() -> Option<u32> {
77+
let release = std::fs::read_to_string(java_home_dir()?.join("release")).ok()?;
78+
release
79+
.lines()
80+
.find_map(|line| line.strip_prefix("JAVA_VERSION="))
81+
.and_then(parse_java_major)
82+
}
83+
84+
/// Resolves the active JDK home directory, preferring `JAVA_HOME` and otherwise
85+
/// deriving it from the `java` binary on `PATH` (`<home>/bin/java` -> `<home>`).
86+
fn java_home_dir() -> Option<PathBuf> {
87+
if let Some(home) = std::env::var_os("JAVA_HOME") {
88+
return Some(PathBuf::from(home));
89+
}
90+
let java = which::which("java").ok()?;
91+
java.parent()?.parent().map(Path::to_path_buf)
92+
}
93+
94+
/// Parses the major (feature) version out of a quoted Java version string (the
95+
/// `JAVA_VERSION="..."` line of a JDK `release` file, or a `java -version`
96+
/// banner), handling both the legacy `1.x` scheme (`1.8.0_292` -> 8) and the
97+
/// modern scheme (`21.0.1` -> 21).
98+
fn parse_java_major(version_output: &str) -> Option<u32> {
99+
let start = version_output.find('"')?;
100+
let rest = &version_output[start + 1..];
101+
let end = rest.find('"')?;
102+
let mut parts = rest[..end].split(['.', '_', '-']);
103+
104+
let first = parts.next()?;
105+
if first == "1" {
106+
parts.next()?.parse().ok()
107+
} else {
108+
first.parse().ok()
109+
}
110+
}
111+
112+
#[cfg(test)]
113+
mod tests {
114+
use super::{gradle_max_supported_java, parse_java_major};
115+
116+
#[test]
117+
fn parses_java_major_version() {
118+
// modern scheme
119+
assert_eq!(
120+
parse_java_major("openjdk version \"21.0.1\" 2023-10-17"),
121+
Some(21)
122+
);
123+
assert_eq!(
124+
parse_java_major("openjdk version \"26\" 2026-03-17"),
125+
Some(26)
126+
);
127+
assert_eq!(
128+
parse_java_major("java version \"17.0.9\" 2023-10-17 LTS"),
129+
Some(17)
130+
);
131+
// legacy 1.x scheme
132+
assert_eq!(parse_java_major("java version \"1.8.0_292\""), Some(8));
133+
// JDK `release` file line (the path java_major_version actually feeds in)
134+
assert_eq!(parse_java_major("\"21.0.1\""), Some(21));
135+
// garbage
136+
assert_eq!(parse_java_major("no version here"), None);
137+
}
138+
139+
#[test]
140+
fn maps_gradle_to_max_java() {
141+
assert_eq!(gradle_max_supported_java("9.6.1"), Some(26));
142+
assert_eq!(gradle_max_supported_java("9.4.0"), Some(26));
143+
assert_eq!(gradle_max_supported_java("9.3"), Some(25));
144+
assert_eq!(gradle_max_supported_java("9.1.0"), Some(25));
145+
assert_eq!(gradle_max_supported_java("9.0"), Some(24));
146+
assert_eq!(gradle_max_supported_java("8.14"), Some(24));
147+
assert_eq!(gradle_max_supported_java("8.5"), Some(21));
148+
assert_eq!(gradle_max_supported_java("7.3"), Some(17));
149+
// pre-7.3 is unmapped
150+
assert_eq!(gradle_max_supported_java("7.2"), None);
151+
// Unknown future Gradle majors are left unmapped to avoid false warnings.
152+
assert_eq!(gradle_max_supported_java("10.0"), None);
153+
// unparsable
154+
assert_eq!(gradle_max_supported_java("not-a-version"), None);
155+
}
156+
}

‎crates/tauri-cli/src/mobile/android/mod.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ use super::{
3939
use crate::{
4040
error::Context,
4141
helpers::config::{BundleResources, Config as TauriConfig},
42+
mobile::android::check_java_gradle_versions::check_java_gradle_versions,
4243
ConfigValue, Error, ErrorExt, Result,
4344
};
4445

4546
mod android_studio_script;
4647
mod build;
48+
mod check_java_gradle_versions;
4749
mod dev;
4850
pub(crate) mod project;
4951
mod run;
@@ -186,6 +188,8 @@ pub fn get_config(
186188
src_main_dir.join("generated"),
187189
);
188190

191+
check_java_gradle_versions();
192+
189193
(config, metadata)
190194
}
191195

0 commit comments

Comments
 (0)