Skip to content

Commit 4e2c8e9

Browse files
authored
Configure adb forward and adb reverse for x run (#123)
Just like a similar PR for `cargo-apk` we'd like to set up some port forwarding to/from a debug phone automatically while using `x run`. This allows configuring a mapping of ports under the `debug:` section of `android:` in `manifest.yaml`.
1 parent 9e58a8a commit 4e2c8e9

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

xbuild/src/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use apk::VersionCode;
66
use appbundle::InfoPlist;
77
use msix::AppxManifest;
88
use serde::Deserialize;
9+
use std::collections::HashMap;
910
use std::path::{Path, PathBuf};
1011

1112
#[derive(Clone, Debug, Default)]
@@ -330,6 +331,16 @@ pub struct GenericConfig {
330331
runtime_libs: Vec<PathBuf>,
331332
}
332333

334+
#[derive(Clone, Debug, Default, Deserialize)]
335+
pub struct AndroidDebugConfig {
336+
/// Forward remote (phone) socket connection to local (host)
337+
#[serde(default)]
338+
pub forward: HashMap<String, String>,
339+
/// Forward local (host) socket connection to remote (phone)
340+
#[serde(default)]
341+
pub reverse: HashMap<String, String>,
342+
}
343+
333344
#[derive(Clone, Debug, Default, Deserialize)]
334345
pub struct AndroidConfig {
335346
#[serde(flatten)]
@@ -342,6 +353,9 @@ pub struct AndroidConfig {
342353
pub gradle: bool,
343354
#[serde(default)]
344355
pub wry: bool,
356+
/// Debug configuration for `x run`
357+
#[serde(default)]
358+
pub debug: AndroidDebugConfig,
345359
}
346360

347361
#[derive(Clone, Debug, Default, Deserialize)]

xbuild/src/devices/adb.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::config::AndroidDebugConfig;
12
use crate::devices::{Backend, Device};
23
use crate::{Arch, Platform};
34
use anyhow::Result;
@@ -126,6 +127,36 @@ impl Adb {
126127
Ok(())
127128
}
128129

130+
fn forward_reverse(&self, device: &str, debug_config: &AndroidDebugConfig) -> Result<()> {
131+
for (local, remote) in &debug_config.forward {
132+
let status = self
133+
.adb(device)
134+
.arg("forward")
135+
.arg(local)
136+
.arg(remote)
137+
.status()?;
138+
anyhow::ensure!(
139+
status.success(),
140+
"adb forward exited with code {:?}",
141+
status.code()
142+
);
143+
}
144+
for (remote, local) in &debug_config.reverse {
145+
let status = self
146+
.adb(device)
147+
.arg("reverse")
148+
.arg(remote)
149+
.arg(local)
150+
.status()?;
151+
anyhow::ensure!(
152+
status.success(),
153+
"adb reverse exited with code {:?}",
154+
status.code()
155+
);
156+
}
157+
Ok(())
158+
}
159+
129160
fn set_debug_app(&self, device: &str, package: &str) -> Result<()> {
130161
let status = self
131162
.shell(device, None)
@@ -292,7 +323,13 @@ impl Adb {
292323
Ok(())
293324
}
294325

295-
pub fn run(&self, device: &str, path: &Path, debug: bool) -> Result<()> {
326+
pub fn run(
327+
&self,
328+
device: &str,
329+
path: &Path,
330+
debug_config: &AndroidDebugConfig,
331+
debug: bool,
332+
) -> Result<()> {
296333
let entry_point = Apk::entry_point(path)?;
297334
let package = &entry_point.package;
298335
let activity = &entry_point.activity;
@@ -303,6 +340,7 @@ impl Adb {
303340
self.clear_debug_app(device)?;
304341
}
305342
self.install(device, path)?;
343+
self.forward_reverse(device, debug_config)?;
306344
let last_timestamp = self.logcat_last_timestamp(device)?;
307345
self.start(device, package, activity)?;
308346
let pid = self.pidof(device, package)?;

xbuild/src/devices/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl Device {
112112

113113
pub fn run(&self, env: &BuildEnv, path: &Path) -> Result<()> {
114114
match &self.backend {
115-
Backend::Adb(adb) => adb.run(&self.id, path, false),
115+
Backend::Adb(adb) => adb.run(&self.id, path, &env.config.android().debug, false),
116116
Backend::Host(host) => host.run(path),
117117
Backend::Imd(imd) => imd.run(env, &self.id, path),
118118
}?;

0 commit comments

Comments
 (0)