Skip to content

Commit 8a161c0

Browse files
cargo-apk: Print and follow adb logcat output after starting app (rust-mobile#332)
Co-authored-by: Marijn Suijten <[email protected]>
1 parent f1e0089 commit 8a161c0

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

.github/workflows/android_test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ adb uninstall rust.example.hello_world || true
1010

1111
if [ -z "$1" ];
1212
then
13-
cargo apk run -p ndk-examples --target x86_64-linux-android --example hello_world
13+
cargo apk run -p ndk-examples --target x86_64-linux-android --example hello_world --no-logcat
1414
else
1515
adb install -r "$1/hello_world.apk"
1616
adb shell am start -a android.intent.action.MAIN -n "rust.example.hello_world/android.app.NativeActivity"

cargo-apk/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- 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))
44
- Support `android:resizeableActivity`. ([#338](https://github.com/rust-windowing/android-ndk-rs/pull/338))
55
- 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))
6+
- Print and follow `adb logcat` output after starting app. ([#332](https://github.com/rust-windowing/android-ndk-rs/pull/332))
67

78
# 0.9.3 (2022-07-05)
89

cargo-apk/src/apk.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ pub struct ApkBuilder<'a> {
1818
build_dir: PathBuf,
1919
build_targets: Vec<Target>,
2020
device_serial: Option<String>,
21+
no_logcat: bool,
2122
}
2223

2324
impl<'a> ApkBuilder<'a> {
2425
pub fn from_subcommand(
2526
cmd: &'a Subcommand,
2627
device_serial: Option<String>,
28+
no_logcat: bool,
2729
) -> Result<Self, Error> {
2830
let ndk = Ndk::from_env()?;
2931
let mut manifest = Manifest::parse_from_toml(cmd.manifest())?;
@@ -100,6 +102,7 @@ impl<'a> ApkBuilder<'a> {
100102
build_dir,
101103
build_targets,
102104
device_serial,
105+
no_logcat,
103106
})
104107
}
105108

@@ -244,7 +247,19 @@ impl<'a> ApkBuilder<'a> {
244247
pub fn run(&self, artifact: &Artifact) -> Result<(), Error> {
245248
let apk = self.build(artifact)?;
246249
apk.install(self.device_serial.as_deref())?;
247-
apk.start(self.device_serial.as_deref())?;
250+
let pid = apk.start(self.device_serial.as_deref())?;
251+
252+
if !self.no_logcat {
253+
self.ndk
254+
.adb(self.device_serial.as_deref())?
255+
.arg("logcat")
256+
.arg("-v")
257+
.arg("color")
258+
.arg("--pid")
259+
.arg(pid.to_string())
260+
.status()?;
261+
}
262+
248263
Ok(())
249264
}
250265

cargo-apk/src/main.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@ fn main() -> anyhow::Result<()> {
66
env_logger::init();
77
let args = std::env::args();
88
let mut device_serial = None;
9-
let cmd = Subcommand::new(args, "apk", |name, value| {
10-
if name == "--device" {
9+
let mut no_logcat = false;
10+
let cmd = Subcommand::new(args, "apk", |name, value| match name {
11+
"--device" => {
1112
if let Some(value) = value {
1213
println!("Running on {}", value);
1314
device_serial = Some(value.to_owned());
1415
Ok(true)
1516
} else {
1617
Err(cargo_subcommand::Error::InvalidArgs)
1718
}
18-
} else {
19-
Ok(false)
2019
}
20+
"--no-logcat" => {
21+
no_logcat = true;
22+
Ok(true)
23+
}
24+
_ => Ok(false),
2125
})
2226
.map_err(Error::Subcommand)?;
23-
let builder = ApkBuilder::from_subcommand(&cmd, device_serial)?;
27+
let builder = ApkBuilder::from_subcommand(&cmd, device_serial, no_logcat)?;
2428

2529
match cmd.cmd() {
2630
"check" | "c" => builder.check()?,
@@ -79,15 +83,18 @@ USAGE:
7983
cargo apk [SUBCOMMAND]
8084
8185
SUBCOMMAND:
82-
check, c Checks that the current package builds without creating an apk
83-
build, b Compiles the current package and creates an apk
84-
run, r Run a binary or example of the local package
85-
gdb Start a gdb session attached to an adb device with symbols loaded
86-
version Print the version of cargo-apk
86+
check, c Checks that the current package builds without creating an apk
87+
build, b Compiles the current package and creates an apk
88+
run, r Run a binary or example of the local package
89+
gdb Start a gdb session attached to an adb device with symbols loaded
90+
version Print the version of cargo-apk
91+
92+
FLAGS:
93+
--no-logcat Don't print and follow `logcat` after running the application.
8794
8895
OPTIONS:
89-
--device Use device with the given serial. See `adb devices` for a list of
90-
connected Android devices.
96+
--device <serial> Use device with the given serial. See `adb devices` for a list of
97+
connected Android devices.
9198
"#
9299
);
93100
}

ndk-build/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- **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))
44
- Add `adb` device serial parameter to `detect_abi()` and `Apk::{install,start}()`. ([#329](https://github.com/rust-windowing/android-ndk-rs/pull/329))
55
- Fix missing `.exe` extension for `adb` on Windows inside `detect_abi()`. ([#339](https://github.com/rust-windowing/android-ndk-rs/pull/339))
6+
- `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))
67

78
# 0.7.0 (2022-07-05)
89

0 commit comments

Comments
 (0)