diff --git a/.changes/fix-cli-dev-server-android.md b/.changes/fix-cli-dev-server-android.md new file mode 100644 index 000000000000..f7b17db546b5 --- /dev/null +++ b/.changes/fix-cli-dev-server-android.md @@ -0,0 +1,6 @@ +--- +"tauri-cli": patch:bug +"@tauri-apps/cli": patch:bug +--- + +Fixes `android dev` not working when using the builtin dev server. diff --git a/tooling/cli/src/helpers/config.rs b/tooling/cli/src/helpers/config.rs index 414977cb4995..52a4585b5031 100644 --- a/tooling/cli/src/helpers/config.rs +++ b/tooling/cli/src/helpers/config.rs @@ -215,3 +215,20 @@ pub fn reload(merge_config: Option<&serde_json::Value>) -> crate::Result crate::Result { + let handle = config_handle(); + if let Some(config_metadata) = &mut *handle.lock().unwrap() { + let merge_config_str = serde_json::to_string(merge_config).unwrap(); + set_var("TAURI_CONFIG", merge_config_str); + + let mut value = serde_json::to_value(config_metadata.inner.clone())?; + merge(&mut value, merge_config); + config_metadata.inner = serde_json::from_value(value)?; + + Ok(handle.clone()) + } else { + Err(anyhow::anyhow!("config not loaded")) + } +} diff --git a/tooling/cli/src/lib.rs b/tooling/cli/src/lib.rs index 88ff4a05a4c0..7fcf9cce3ace 100644 --- a/tooling/cli/src/lib.rs +++ b/tooling/cli/src/lib.rs @@ -34,7 +34,7 @@ use clap::{ArgAction, CommandFactory, FromArgMatches, Parser, Subcommand, ValueE use env_logger::fmt::style::{AnsiColor, Style}; use env_logger::Builder; use log::Level; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::io::{BufReader, Write}; use std::process::{exit, Command, ExitStatus, Output, Stdio}; use std::{ @@ -48,7 +48,7 @@ use std::{ }; /// Tauri configuration argument option. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConfigValue(pub(crate) serde_json::Value); impl FromStr for ConfigValue { diff --git a/tooling/cli/src/mobile/android/android_studio_script.rs b/tooling/cli/src/mobile/android/android_studio_script.rs index 76df42fca375..bd6e02797836 100644 --- a/tooling/cli/src/mobile/android/android_studio_script.rs +++ b/tooling/cli/src/mobile/android/android_studio_script.rs @@ -59,6 +59,7 @@ pub fn command(options: Options) -> Result<()> { ); (config, metadata, cli_options) }; + ensure_init( &tauri_config, config.app(), @@ -66,6 +67,10 @@ pub fn command(options: Options) -> Result<()> { MobileTarget::Android, )?; + if let Some(config) = &cli_options.config { + crate::helpers::config::merge_with(&config.0)?; + } + let env = env()?; if cli_options.dev { @@ -77,6 +82,7 @@ pub fn command(options: Options) -> Result<()> { .build .dev_url .clone(); + if let Some(port) = dev_url.and_then(|url| url.port_or_known_default()) { let forward = format!("tcp:{port}"); // ignore errors in case we do not have a device available diff --git a/tooling/cli/src/mobile/android/build.rs b/tooling/cli/src/mobile/android/build.rs index 590ef6536ec6..8962a51dd2c3 100644 --- a/tooling/cli/src/mobile/android/build.rs +++ b/tooling/cli/src/mobile/android/build.rs @@ -201,6 +201,7 @@ fn run_build( args: build_options.args.clone(), noise_level, vars: Default::default(), + config: build_options.config.clone(), }; let handle = write_options( &tauri_config.lock().unwrap().as_ref().unwrap().identifier, diff --git a/tooling/cli/src/mobile/android/dev.rs b/tooling/cli/src/mobile/android/dev.rs index 28eb7f19ce68..6400314ec83a 100644 --- a/tooling/cli/src/mobile/android/dev.rs +++ b/tooling/cli/src/mobile/android/dev.rs @@ -222,7 +222,7 @@ fn run_dev( debug: !options.release_mode, features: options.features, args: Vec::new(), - config: options.config, + config: dev_options.config.clone(), no_watch: options.no_watch, }, |options| { @@ -232,6 +232,7 @@ fn run_dev( args: options.args.clone(), noise_level, vars: Default::default(), + config: dev_options.config.clone(), }; let _handle = write_options( diff --git a/tooling/cli/src/mobile/ios/build.rs b/tooling/cli/src/mobile/ios/build.rs index 175fa5919fec..f53cb9205313 100644 --- a/tooling/cli/src/mobile/ios/build.rs +++ b/tooling/cli/src/mobile/ios/build.rs @@ -283,6 +283,7 @@ fn run_build( args: build_options.args.clone(), noise_level, vars: Default::default(), + config: build_options.config.clone(), }; let handle = write_options( &tauri_config.lock().unwrap().as_ref().unwrap().identifier, diff --git a/tooling/cli/src/mobile/ios/dev.rs b/tooling/cli/src/mobile/ios/dev.rs index cde4b8a7d804..b11637c80e5a 100644 --- a/tooling/cli/src/mobile/ios/dev.rs +++ b/tooling/cli/src/mobile/ios/dev.rs @@ -36,6 +36,8 @@ use std::{ sync::OnceLock, }; +const PHYSICAL_IPHONE_DEV_WARNING: &str = "To develop on physical phones you need the `--host` option (not required for Simulators). See the documentation for more information: https://v2.tauri.app/develop/#development-server"; + #[derive(Debug, Clone, Parser)] #[clap( about = "Run your app in development mode on iOS", @@ -367,6 +369,8 @@ fn run_dev( let out_dir = bin_path.parent().unwrap(); let _lock = flock::open_rw(out_dir.join("lock").with_extension("ios"), "iOS")?; + let set_host = options.host.is_some(); + configure_cargo(app, None)?; let open = options.open; @@ -377,7 +381,7 @@ fn run_dev( debug: true, features: options.features, args: Vec::new(), - config: options.config, + config: dev_options.config.clone(), no_watch: options.no_watch, }, |options| { @@ -387,6 +391,7 @@ fn run_dev( args: options.args.clone(), noise_level, vars: Default::default(), + config: dev_options.config.clone(), }; let _handle = write_options( &tauri_config.lock().unwrap().as_ref().unwrap().identifier, @@ -394,6 +399,9 @@ fn run_dev( )?; if open { + if !set_host { + log::warn!("{PHYSICAL_IPHONE_DEV_WARNING}"); + } open_and_wait(config, &env) } else if let Some(device) = &device { match run(device, options, config, &env) { @@ -409,6 +417,9 @@ fn run_dev( } } } else { + if !set_host { + log::warn!("{PHYSICAL_IPHONE_DEV_WARNING}"); + } open_and_wait(config, &env) } }, diff --git a/tooling/cli/src/mobile/mod.rs b/tooling/cli/src/mobile/mod.rs index bf4788498e26..b1c746e51cd1 100644 --- a/tooling/cli/src/mobile/mod.rs +++ b/tooling/cli/src/mobile/mod.rs @@ -8,6 +8,7 @@ use crate::{ config::{Config as TauriConfig, ConfigHandle}, }, interface::{AppInterface, AppSettings, DevProcess, Interface, Options as InterfaceOptions}, + ConfigValue, }; #[cfg(target_os = "macos")] use anyhow::Context; @@ -141,6 +142,7 @@ pub struct CliOptions { pub args: Vec, pub noise_level: NoiseLevel, pub vars: HashMap, + pub config: Option, } impl Default for CliOptions { @@ -151,6 +153,7 @@ impl Default for CliOptions { args: vec!["--lib".into()], noise_level: Default::default(), vars: Default::default(), + config: None, } } }