Allow to connect to the dev server from a physical Android device (#3634)

* fix(android): now dev server just works on devices

* fix: allow custom port for Android app development

* refactor: simplified URL creation in LaunchBuilder
This commit is contained in:
Andrew Voynov authored and GitHub committed 2025-01-31 14:29:54 -08:00
1 parent d7e64d5d24
commit 67515ce812
4 files changed
+55 -14

No files matched your search

+25 -4
View File
@@ -86,8 +86,12 @@ impl AppHandle {
),
("RUST_BACKTRACE", "1".to_string()),
(
dioxus_cli_config::DEVSERVER_RAW_ADDR_ENV,
devserver_ip.to_string(),
dioxus_cli_config::DEVSERVER_IP_ENV,
devserver_ip.ip().to_string(),
),
(
dioxus_cli_config::DEVSERVER_PORT_ENV,
devserver_ip.port().to_string(),
),
// unset the cargo dirs in the event we're running `dx` locally
// since the child process will inherit the env vars, we don't want to confuse the downstream process
@@ -145,7 +149,7 @@ impl AppHandle {
// https://developer.android.com/studio/run/emulator-commandline
Platform::Android => {
self.open_android_sim(envs).await;
self.open_android_sim(devserver_ip, envs).await;
None
}
@@ -697,13 +701,30 @@ We checked the folder: {}
Ok(())
}
async fn open_android_sim(&self, envs: Vec<(&'static str, String)>) {
async fn open_android_sim(
&self,
devserver_socket: SocketAddr,
envs: Vec<(&'static str, String)>,
) {
let apk_path = self.app.apk_path();
let session_cache = self.app.build.krate.session_cache_dir();
let full_mobile_app_name = self.app.build.krate.full_mobile_app_name();
// Start backgrounded since .open() is called while in the arm of the top-level match
tokio::task::spawn(async move {
let port = devserver_socket.port();
if let Err(e) = Command::new("adb")
.arg("reverse")
.arg(format!("tcp:{}", port))
.arg(format!("tcp:{}", port))
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.output()
.await
{
tracing::error!("failed to forward port {port}: {e}");
}
// Install
// adb install -r app-debug.apk
if let Err(e) = Command::new(DioxusCrate::android_adb())
+17
View File
@@ -193,6 +193,23 @@ impl WebServer {
}
pub(crate) async fn shutdown(&mut self) {
if matches!(self.platform, Platform::Android) {
use std::process::{Command, Stdio};
if let Err(err) = Command::new("adb")
.arg("reverse")
.arg("--remove")
.arg(format!("tcp:{}", self.devserver_port))
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.output()
{
tracing::error!(
"failed to remove forwarded port {}: {err}",
self.devserver_port
);
}
}
self.send_shutdown().await;
for socket in self.hot_reload_sockets.drain(..) {
_ = socket.close().await;