From 8a161c0971388c7559c2169e30bc6840060fe1ce Mon Sep 17 00:00:00 2001 From: Jasper Bekkers Date: Thu, 8 Sep 2022 16:24:26 +0200 Subject: [PATCH] cargo-apk: Print and follow `adb logcat` output after starting app (#332) Co-authored-by: Marijn Suijten --- .github/workflows/android_test.sh | 2 +- cargo-apk/CHANGELOG.md | 1 + cargo-apk/src/apk.rs | 17 ++++++++++++++++- cargo-apk/src/main.rs | 31 +++++++++++++++++++------------ ndk-build/CHANGELOG.md | 1 + 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/.github/workflows/android_test.sh b/.github/workflows/android_test.sh index b782a650..6368cabc 100755 --- a/.github/workflows/android_test.sh +++ b/.github/workflows/android_test.sh @@ -10,7 +10,7 @@ adb uninstall rust.example.hello_world || true if [ -z "$1" ]; then - cargo apk run -p ndk-examples --target x86_64-linux-android --example hello_world + cargo apk run -p ndk-examples --target x86_64-linux-android --example hello_world --no-logcat else adb install -r "$1/hello_world.apk" adb shell am start -a android.intent.action.MAIN -n "rust.example.hello_world/android.app.NativeActivity" diff --git a/cargo-apk/CHANGELOG.md b/cargo-apk/CHANGELOG.md index 46a1df6d..072c6c73 100644 --- a/cargo-apk/CHANGELOG.md +++ b/cargo-apk/CHANGELOG.md @@ -3,6 +3,7 @@ - Upgrade to latest `ndk-build` to deduplicate libraries before packaging them into the APK. ([#333](https://github.com/rust-windowing/android-ndk-rs/pull/333)) - Support `android:resizeableActivity`. ([#338](https://github.com/rust-windowing/android-ndk-rs/pull/338)) - Add `--device` argument to select `adb` device by serial (see `adb devices` for connected devices and their serial). ([#329](https://github.com/rust-windowing/android-ndk-rs/pull/329)) +- Print and follow `adb logcat` output after starting app. ([#332](https://github.com/rust-windowing/android-ndk-rs/pull/332)) # 0.9.3 (2022-07-05) diff --git a/cargo-apk/src/apk.rs b/cargo-apk/src/apk.rs index fd06b0a1..bf4b5569 100644 --- a/cargo-apk/src/apk.rs +++ b/cargo-apk/src/apk.rs @@ -18,12 +18,14 @@ pub struct ApkBuilder<'a> { build_dir: PathBuf, build_targets: Vec, device_serial: Option, + no_logcat: bool, } impl<'a> ApkBuilder<'a> { pub fn from_subcommand( cmd: &'a Subcommand, device_serial: Option, + no_logcat: bool, ) -> Result { let ndk = Ndk::from_env()?; let mut manifest = Manifest::parse_from_toml(cmd.manifest())?; @@ -100,6 +102,7 @@ impl<'a> ApkBuilder<'a> { build_dir, build_targets, device_serial, + no_logcat, }) } @@ -244,7 +247,19 @@ impl<'a> ApkBuilder<'a> { pub fn run(&self, artifact: &Artifact) -> Result<(), Error> { let apk = self.build(artifact)?; apk.install(self.device_serial.as_deref())?; - apk.start(self.device_serial.as_deref())?; + let pid = apk.start(self.device_serial.as_deref())?; + + if !self.no_logcat { + self.ndk + .adb(self.device_serial.as_deref())? + .arg("logcat") + .arg("-v") + .arg("color") + .arg("--pid") + .arg(pid.to_string()) + .status()?; + } + Ok(()) } diff --git a/cargo-apk/src/main.rs b/cargo-apk/src/main.rs index c72207b1..c73f4356 100644 --- a/cargo-apk/src/main.rs +++ b/cargo-apk/src/main.rs @@ -6,8 +6,9 @@ fn main() -> anyhow::Result<()> { env_logger::init(); let args = std::env::args(); let mut device_serial = None; - let cmd = Subcommand::new(args, "apk", |name, value| { - if name == "--device" { + let mut no_logcat = false; + let cmd = Subcommand::new(args, "apk", |name, value| match name { + "--device" => { if let Some(value) = value { println!("Running on {}", value); device_serial = Some(value.to_owned()); @@ -15,12 +16,15 @@ fn main() -> anyhow::Result<()> { } else { Err(cargo_subcommand::Error::InvalidArgs) } - } else { - Ok(false) } + "--no-logcat" => { + no_logcat = true; + Ok(true) + } + _ => Ok(false), }) .map_err(Error::Subcommand)?; - let builder = ApkBuilder::from_subcommand(&cmd, device_serial)?; + let builder = ApkBuilder::from_subcommand(&cmd, device_serial, no_logcat)?; match cmd.cmd() { "check" | "c" => builder.check()?, @@ -79,15 +83,18 @@ USAGE: cargo apk [SUBCOMMAND] SUBCOMMAND: - check, c Checks that the current package builds without creating an apk - build, b Compiles the current package and creates an apk - run, r Run a binary or example of the local package - gdb Start a gdb session attached to an adb device with symbols loaded - version Print the version of cargo-apk + check, c Checks that the current package builds without creating an apk + build, b Compiles the current package and creates an apk + run, r Run a binary or example of the local package + gdb Start a gdb session attached to an adb device with symbols loaded + version Print the version of cargo-apk + +FLAGS: + --no-logcat Don't print and follow `logcat` after running the application. OPTIONS: - --device Use device with the given serial. See `adb devices` for a list of - connected Android devices. + --device Use device with the given serial. See `adb devices` for a list of + connected Android devices. "# ); } diff --git a/ndk-build/CHANGELOG.md b/ndk-build/CHANGELOG.md index 1d5bc033..de987d18 100644 --- a/ndk-build/CHANGELOG.md +++ b/ndk-build/CHANGELOG.md @@ -3,6 +3,7 @@ - **Breaking:** Postpone APK library packaging until before zip alignment, to deduplicate possibly overlapping entries. ([#333](https://github.com/rust-windowing/android-ndk-rs/pull/333)) - Add `adb` device serial parameter to `detect_abi()` and `Apk::{install,start}()`. ([#329](https://github.com/rust-windowing/android-ndk-rs/pull/329)) - Fix missing `.exe` extension for `adb` on Windows inside `detect_abi()`. ([#339](https://github.com/rust-windowing/android-ndk-rs/pull/339)) +- `start()` now returns the PID of the started app process (useful for passing to `adb logcat --pid`). ([#331](https://github.com/rust-windowing/android-ndk-rs/pull/331)) # 0.7.0 (2022-07-05)