Skip to content

Commit

Permalink
fix(cli): android dev --open failing due to adb not finding device (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Aug 3, 2024
1 parent 699319d commit fb1933f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changes/android-dev-open-adb-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:bug
"@tauri-apps/cli": patch:bug
---

Fixes running `android dev --open`.
22 changes: 21 additions & 1 deletion tooling/cli/src/mobile/android/android_studio_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
use clap::{ArgAction, Parser};

use cargo_mobile2::{
android::target::Target,
android::{adb, target::Target},
opts::Profile,
target::{call_for_targets_with_fallback, TargetTrait},
};
Expand Down Expand Up @@ -58,6 +58,26 @@ pub fn command(options: Options) -> Result<()> {

let env = env()?;

if cli_options.dev {
let dev_url = tauri_config
.lock()
.unwrap()
.as_ref()
.unwrap()
.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
let _ = adb::adb(&env, ["reverse", &forward, &forward])
.stdin_file(os_pipe::dup_stdin().unwrap())
.stdout_file(os_pipe::dup_stdout().unwrap())
.stderr_capture()
.run();
}
}

call_for_targets_with_fallback(
options.targets.unwrap_or_default().iter(),
&detect_target_ok,
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ fn run_build(
let _lock = flock::open_rw(out_dir.join("lock").with_extension("android"), "Android")?;

let cli_options = CliOptions {
dev: false,
features: build_options.features.clone(),
args: build_options.args.clone(),
noise_level,
Expand Down
19 changes: 1 addition & 18 deletions tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use clap::{ArgAction, Parser};
use anyhow::Context;
use cargo_mobile2::{
android::{
adb,
config::{Config as AndroidConfig, Metadata as AndroidMetadata},
device::Device,
env::Env,
Expand Down Expand Up @@ -208,23 +207,6 @@ fn run_dev(
},
)?;

let dev_url = tauri_config
.lock()
.unwrap()
.as_ref()
.unwrap()
.build
.dev_url
.clone();
if let Some(port) = dev_url.and_then(|url| url.port_or_known_default()) {
let forward = format!("tcp:{port}");
adb::adb(&env, ["reverse", &forward, &forward])
.stdin_file(os_pipe::dup_stdin().unwrap())
.stdout_file(os_pipe::dup_stdout().unwrap())
.stderr_capture()
.run()?;
}

let open = options.open;
let exit_on_panic = options.exit_on_panic;
let no_watch = options.no_watch;
Expand All @@ -238,6 +220,7 @@ fn run_dev(
},
|options| {
let cli_options = CliOptions {
dev: true,
features: options.features.clone(),
args: options.args.clone(),
noise_level,
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/mobile/ios/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ fn run_build(
let _lock = flock::open_rw(out_dir.join("lock").with_extension("ios"), "iOS")?;

let cli_options = CliOptions {
dev: false,
features: build_options.features.clone(),
args: build_options.args.clone(),
noise_level,
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ fn use_network_address_for_dev_url(
.host
.unwrap_or_default()
.unwrap_or_else(|| *local_ip_address(options.force_ip_prompt));
dev_options.host.replace(ip.clone());
dev_options.host.replace(ip);
Some(ip)
} else {
None
Expand Down Expand Up @@ -375,6 +375,7 @@ fn run_dev(
},
|options| {
let cli_options = CliOptions {
dev: true,
features: options.features.clone(),
args: options.args.clone(),
noise_level,
Expand Down
2 changes: 2 additions & 0 deletions tooling/cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl Target {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliOptions {
pub dev: bool,
pub features: Option<Vec<String>>,
pub args: Vec<String>,
pub noise_level: NoiseLevel,
Expand All @@ -140,6 +141,7 @@ pub struct CliOptions {
impl Default for CliOptions {
fn default() -> Self {
Self {
dev: false,
features: None,
args: vec!["--lib".into()],
noise_level: Default::default(),
Expand Down

0 comments on commit fb1933f

Please sign in to comment.