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: Use uid to limit logcat to the current application #33

Merged
merged 1 commit into from
Nov 22, 2023
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
7 changes: 4 additions & 3 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,17 @@ impl<'a> ApkBuilder<'a> {
let apk = self.build(artifact)?;
apk.reverse_port_forwarding(self.device_serial.as_deref())?;
apk.install(self.device_serial.as_deref())?;
let pid = apk.start(self.device_serial.as_deref())?;
apk.start(self.device_serial.as_deref())?;
let uid = apk.uidof(self.device_serial.as_deref())?;

if !no_logcat {
self.ndk
.adb(self.device_serial.as_deref())?
.arg("logcat")
.arg("-v")
.arg("color")
.arg("--pid")
.arg(pid.to_string())
.arg("--uid")
.arg(uid.to_string())
.status()?;
}

Expand Down
3 changes: 2 additions & 1 deletion ndk-build/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Unreleased

- Add `android:extractNativeLibs`, `android:usesCleartextTraffic` attributes to the manifest's `Application` element, and `android:alwaysRetainTaskState` to the `Activity` element. ([#15](https://github.com/rust-mobile/cargo-apk/pull/15))
- Enable building from `android` host ([#29](https://github.com/rust-mobile/cargo-apk/pull/29))
- Enable building from `android` host. ([#29](https://github.com/rust-mobile/cargo-apk/pull/29))
- Use app `uid` instead of `pid` to limit `logcat` output to the current app. ([#33](https://github.com/rust-mobile/cargo-apk/pull/33))

# 0.9.0 (2022-11-23)

Expand Down
61 changes: 38 additions & 23 deletions ndk-build/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,37 +292,52 @@ impl Apk {
Ok(())
}

pub fn start(&self, device_serial: Option<&str>) -> Result<u32, NdkError> {
let mut am_start = self.ndk.adb(device_serial)?;
am_start
.arg("shell")
pub fn start(&self, device_serial: Option<&str>) -> Result<(), NdkError> {
let mut adb = self.ndk.adb(device_serial)?;
adb.arg("shell")
.arg("am")
.arg("start")
.arg("-W")
.arg("-a")
.arg("android.intent.action.MAIN")
.arg("-n")
.arg(format!("{}/android.app.NativeActivity", &self.package_name));
if !am_start.status()?.success() {
return Err(NdkError::CmdFailed(am_start));
}
.arg(format!("{}/android.app.NativeActivity", self.package_name));

let pid_vec = self
.ndk
.adb(device_serial)?
.arg("shell")
.arg("pidof")
.arg(&self.package_name)
.output()?
.stdout;
if !adb.status()?.success() {
return Err(NdkError::CmdFailed(adb));
}

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

println!("Launched with PID {}", pid);
pub fn uidof(&self, device_serial: Option<&str>) -> Result<u32, NdkError> {
let mut adb = self.ndk.adb(device_serial)?;
adb.arg("shell")
.arg("pm")
.arg("list")
.arg("package")
.arg("-U")
.arg(&self.package_name);
let output = adb.output()?;

if !output.status.success() {
return Err(NdkError::CmdFailed(adb));
}

Ok(pid)
let output = std::str::from_utf8(&output.stdout).unwrap();
let (_package, uid) = output
.lines()
.filter_map(|line| line.split_once(' '))
// `pm list package` uses the id as a substring filter; make sure
// we select the right package in case it returns multiple matches:
.find(|(package, _uid)| package.strip_prefix("package:") == Some(&self.package_name))
.ok_or(NdkError::PackageNotInOutput {
package: self.package_name.clone(),
output: output.to_owned(),
})?;
let uid = uid
.strip_prefix("uid:")
.ok_or(NdkError::UidNotInOutput(output.to_owned()))?;
uid.parse()
.map_err(|e| NdkError::NotAUid(e, uid.to_owned()))
}
}
8 changes: 6 additions & 2 deletions ndk-build/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ pub enum NdkError {
CmdFailed(Command),
#[error(transparent)]
Serialize(#[from] quick_xml::de::DeError),
#[error("String `{1}` is not a PID")]
NotAPid(#[source] ParseIntError, String),
#[error("String `{1}` is not a UID")]
NotAUid(#[source] ParseIntError, String),
#[error("Could not find `package:{package}` in output `{output}`")]
PackageNotInOutput { package: String, output: String },
#[error("Could not find `uid:` in output `{0}`")]
UidNotInOutput(String),
}
Loading