Skip to content

Commit 55f2fdc

Browse files
committed
add prmopts and ci check
1 parent a39beab commit 55f2fdc

File tree

5 files changed

+93
-37
lines changed

5 files changed

+93
-37
lines changed

crates/tauri-cli/src/mobile/android/android_studio_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn command(options: Options) -> Result<()> {
103103
)?;
104104
}
105105

106-
let env = env()?;
106+
let env = env(std::env::var("CI").is_ok())?;
107107

108108
if cli_options.dev {
109109
let dev_url = tauri_config

crates/tauri-cli/src/mobile/android/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
162162
MobileTarget::Android,
163163
)?;
164164

165-
let mut env = env()?;
165+
let mut env = env(options.ci)?;
166166
configure_cargo(&mut env, &config)?;
167167

168168
crate::build::setup(&interface, &mut build_options, tauri_config.clone(), true)?;

crates/tauri-cli/src/mobile/android/dev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
158158
.collect::<Vec<_>>(),
159159
)?;
160160

161-
let env = env()?;
161+
let env = env(false)?;
162162
let device = if options.open {
163163
None
164164
} else {

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

Lines changed: 89 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod dev;
4545
pub(crate) mod project;
4646

4747
const NDK_VERSION: &str = "29.0.13846066";
48+
const SDK_VERSION: u8 = 36;
4849

4950
#[cfg(target_os = "macos")]
5051
const CMDLINE_TOOLS_URL: &str =
@@ -190,9 +191,9 @@ pub fn get_config(
190191
(config, metadata)
191192
}
192193

193-
pub fn env() -> Result<Env> {
194+
pub fn env(non_interactive: bool) -> Result<Env> {
194195
let env = super::env()?;
195-
ensure_env()?;
196+
ensure_env(non_interactive)?;
196197
cargo_mobile2::android::env::Env::from_env(env).map_err(Into::into)
197198
}
198199

@@ -206,7 +207,6 @@ fn download_cmdline_tools(extract_path: &Path) -> Result<()> {
206207
.limit(200 * 1024 * 1024 /* 200MB */)
207208
.read_to_vec()?;
208209

209-
log::info!("Extracting Android command line tools...");
210210
let mut zip = zip::ZipArchive::new(Cursor::new(body))?;
211211

212212
log::info!(
@@ -218,10 +218,10 @@ fn download_cmdline_tools(extract_path: &Path) -> Result<()> {
218218
Ok(())
219219
}
220220

221-
fn ensure_env() -> Result<()> {
221+
fn ensure_env(non_interactive: bool) -> Result<()> {
222222
ensure_java()?;
223-
ensure_sdk()?;
224-
ensure_ndk()?;
223+
ensure_sdk(non_interactive)?;
224+
ensure_ndk(non_interactive)?;
225225
Ok(())
226226
}
227227

@@ -245,7 +245,7 @@ fn ensure_java() -> Result<()> {
245245
Ok(())
246246
}
247247

248-
fn ensure_sdk() -> Result<()> {
248+
fn ensure_sdk(non_interactive: bool) -> Result<()> {
249249
let android_home = std::env::var_os("ANDROID_HOME").map(PathBuf::from);
250250
if !android_home.as_ref().is_some_and(|v| v.exists()) {
251251
log::info!(
@@ -269,6 +269,8 @@ fn ensure_sdk() -> Result<()> {
269269
"Using installed Android SDK: {}",
270270
default_android_home.display()
271271
);
272+
} else if non_interactive {
273+
anyhow::bail!("Android SDK not found. Make sure the SDK and NDK are installed and the ANDROID_HOME and NDK_HOME environment variables are set.");
272274
} else {
273275
log::error!(
274276
"Android SDK not found at {}",
@@ -281,20 +283,46 @@ fn ensure_sdk() -> Result<()> {
281283
std::env::current_dir()?
282284
};
283285

284-
download_cmdline_tools(&extract_path)?;
286+
let sdk_manager_path = extract_path
287+
.join("cmdline-tools/bin/sdkmanager")
288+
.with_extension(if cfg!(windows) { "bat" } else { "" });
285289

286-
log::info!("Running sdkmanager...");
287-
let status = Command::new(
288-
extract_path
289-
.join("cmdline-tools/bin/sdkmanager")
290-
.with_extension(if cfg!(windows) { "bat" } else { "" }),
291-
)
292-
.arg(format!("--sdk_root={}", default_android_home.display()))
293-
.arg("--install")
294-
.arg("platform-tools")
295-
.arg("platforms;android-36")
296-
.arg(format!("ndk;{NDK_VERSION}"))
297-
.status()?;
290+
let mut granted_permission_to_install = false;
291+
292+
if !sdk_manager_path.exists() {
293+
granted_permission_to_install = crate::helpers::prompts::confirm(
294+
"Do you want to install the Android Studio command line tools to setup the Android SDK?",
295+
Some(false),
296+
)
297+
.unwrap_or_default();
298+
299+
if !granted_permission_to_install {
300+
anyhow::bail!("Skipping Android Studio command line tools installation. Please go through the manual setup process described in the documentation: https://tauri.app/start/prerequisites/#android");
301+
}
302+
303+
download_cmdline_tools(&extract_path)?;
304+
}
305+
306+
if !granted_permission_to_install {
307+
granted_permission_to_install = crate::helpers::prompts::confirm(
308+
"Do you want to install the Android SDK using the command line tools?",
309+
Some(false),
310+
)
311+
.unwrap_or_default();
312+
313+
if !granted_permission_to_install {
314+
anyhow::bail!("Skipping Android Studio SDK installation. Please go through the manual setup process described in the documentation: https://tauri.app/start/prerequisites/#android");
315+
}
316+
}
317+
318+
log::info!("Running sdkmanager to install platform-tools, android-{SDK_VERSION} and ndk-{NDK_VERSION} on {}...", default_android_home.display());
319+
let status = Command::new(&sdk_manager_path)
320+
.arg(format!("--sdk_root={}", default_android_home.display()))
321+
.arg("--install")
322+
.arg("platform-tools")
323+
.arg(format!("platforms;android-{SDK_VERSION}"))
324+
.arg(format!("ndk;{NDK_VERSION}"))
325+
.status()?;
298326

299327
if !status.success() {
300328
anyhow::bail!("Failed to install Android SDK");
@@ -307,7 +335,7 @@ fn ensure_sdk() -> Result<()> {
307335
Ok(())
308336
}
309337

310-
fn ensure_ndk() -> Result<()> {
338+
fn ensure_ndk(non_interactive: bool) -> Result<()> {
311339
// re-evaluate ANDROID_HOME
312340
let android_home = std::env::var_os("ANDROID_HOME")
313341
.map(PathBuf::from)
@@ -325,22 +353,50 @@ fn ensure_ndk() -> Result<()> {
325353
if let Some(ndk) = installed_ndks.last() {
326354
log::info!("Using installed NDK: {}", ndk.display());
327355
std::env::set_var("NDK_HOME", ndk);
356+
} else if non_interactive {
357+
anyhow::bail!("Android NDK not found. Make sure the NDK is installed and the NDK_HOME environment variable is set.");
328358
} else {
329-
let cmdline_tools_path = android_home.join("cmdline-tools");
330-
if !cmdline_tools_path.exists() {
359+
let sdk_manager_path = android_home
360+
.join("cmdline-tools/bin/sdkmanager")
361+
.with_extension(if cfg!(windows) { "bat" } else { "" });
362+
363+
let mut granted_permission_to_install = false;
364+
365+
if !sdk_manager_path.exists() {
366+
granted_permission_to_install = crate::helpers::prompts::confirm(
367+
"Do you want to install the Android Studio command line tools to setup the Android NDK?",
368+
Some(false),
369+
)
370+
.unwrap_or_default();
371+
372+
if !granted_permission_to_install {
373+
anyhow::bail!("Skipping Android Studio command line tools installation. Please go through the manual setup process described in the documentation: https://tauri.app/start/prerequisites/#android");
374+
}
375+
331376
download_cmdline_tools(&android_home)?;
332377
}
333378

334-
log::info!("Installing NDK...");
335-
let status = Command::new(
336-
android_home
337-
.join("cmdline-tools/bin/sdkmanager")
338-
.with_extension(if cfg!(windows) { "bat" } else { "" }),
339-
)
340-
.arg(format!("--sdk_root={}", android_home.display()))
341-
.arg("--install")
342-
.arg(format!("ndk;{NDK_VERSION}"))
343-
.status()?;
379+
if !granted_permission_to_install {
380+
granted_permission_to_install = crate::helpers::prompts::confirm(
381+
"Do you want to install the Android NDK using the command line tools?",
382+
Some(false),
383+
)
384+
.unwrap_or_default();
385+
386+
if !granted_permission_to_install {
387+
anyhow::bail!("Skipping Android Studio NDK installation. Please go through the manual setup process described in the documentation: https://tauri.app/start/prerequisites/#android");
388+
}
389+
}
390+
391+
log::info!(
392+
"Running sdkmanager to install ndk-{NDK_VERSION} on {}...",
393+
android_home.display()
394+
);
395+
let status = Command::new(&sdk_manager_path)
396+
.arg(format!("--sdk_root={}", android_home.display()))
397+
.arg("--install")
398+
.arg(format!("ndk;{NDK_VERSION}"))
399+
.status()?;
344400

345401
if !status.success() {
346402
anyhow::bail!("Failed to install Android NDK");

crates/tauri-cli/src/mobile/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub fn exec(
123123
let app = match target {
124124
// Generate Android Studio project
125125
Target::Android => {
126-
let _env = super::android::env()?;
126+
let _env = super::android::env(non_interactive)?;
127127
let (config, metadata) =
128128
super::android::get_config(&app, tauri_config_, None, &Default::default());
129129
map.insert("android", &config);

0 commit comments

Comments
 (0)