Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ndk-build: Return PID after launching the app #331

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions ndk-build/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,37 @@ impl Apk {
Ok(())
}

pub fn start(&self, device_serial: Option<&str>) -> Result<(), NdkError> {
let mut adb = self.ndk.adb(device_serial)?;

adb.arg("shell")
pub fn start(&self, device_serial: Option<&str>) -> Result<u32, NdkError> {
let mut am_start = self.ndk.adb(device_serial)?;
am_start
.arg("shell")
.arg("am")
.arg("start")
.arg("-W")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's neat, I was wondering how this was implemented by @dvc94ch in x, turns out to be a sleep-loop: https://github.com/cloudpeers/xbuild/blob/e0ebb50bfa78cfad61931bb30c70a624b85fdddd/xbuild/src/devices/adb.rs#L175-L186

Copy link
Member

@MarijnS95 MarijnS95 Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably semi-smart to keep a loop so that duplicate PIDs for yet-to-exit app instances are not causing issues:

https://github.com/cloudpeers/xbuild/blob/e58cc09b4f2cb07c1fa0591f9d65cea435dd5646/xbuild/src/devices/adb.rs#L179

But IMO am start should be able to emit the pid instead of this hackery. Android Studio must also have a smart yet concise way of dealing with this, that isn't error-prone. Perhaps just outputting all the PIDs (--pid is probably a coma-separated list or can be stacked), and we can combine that with a last timestamp filter.

.arg("-a")
.arg("android.intent.action.MAIN")
.arg("-n")
.arg(format!("{}/android.app.NativeActivity", &self.package_name));
if !adb.status()?.success() {
return Err(NdkError::CmdFailed(adb));
if !am_start.status()?.success() {
return Err(NdkError::CmdFailed(am_start));
}
Ok(())

let pid_vec = self
.ndk
.adb(device_serial)?
.arg("shell")
.arg("pidof")
.arg(&self.package_name)
.output()?
.stdout;

let pid = std::str::from_utf8(&pid_vec).unwrap().trim();
let pid: u32 = pid
.parse()
.map_err(|e| NdkError::NotAPid(e, pid.to_owned()))?;

println!("Launched with PID {}", pid);

Ok(pid)
}
}
3 changes: 3 additions & 0 deletions ndk-build/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Error as IoError;
use std::num::ParseIntError;
use std::path::PathBuf;
use std::process::Command;
use thiserror::Error;
Expand Down Expand Up @@ -47,4 +48,6 @@ pub enum NdkError {
CmdFailed(Command),
#[error(transparent)]
Serialize(#[from] quick_xml::de::DeError),
#[error("String `{1}` is not a PID")]
NotAPid(#[source] ParseIntError, String),
}