Skip to content

Commit

Permalink
cargo-apk: Print and follow adb logcat output after starting app (#332
Browse files Browse the repository at this point in the history
)

Co-authored-by: Marijn Suijten <[email protected]>
  • Loading branch information
Jasper-Bekkers and MarijnS95 authored Sep 8, 2022
1 parent f1e0089 commit 8a161c0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions cargo-apk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 16 additions & 1 deletion cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ pub struct ApkBuilder<'a> {
build_dir: PathBuf,
build_targets: Vec<Target>,
device_serial: Option<String>,
no_logcat: bool,
}

impl<'a> ApkBuilder<'a> {
pub fn from_subcommand(
cmd: &'a Subcommand,
device_serial: Option<String>,
no_logcat: bool,
) -> Result<Self, Error> {
let ndk = Ndk::from_env()?;
let mut manifest = Manifest::parse_from_toml(cmd.manifest())?;
Expand Down Expand Up @@ -100,6 +102,7 @@ impl<'a> ApkBuilder<'a> {
build_dir,
build_targets,
device_serial,
no_logcat,
})
}

Expand Down Expand Up @@ -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(())
}

Expand Down
31 changes: 19 additions & 12 deletions cargo-apk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ 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());
Ok(true)
} 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()?,
Expand Down Expand Up @@ -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 <serial> Use device with the given serial. See `adb devices` for a list of
connected Android devices.
"#
);
}
Expand Down
1 change: 1 addition & 0 deletions ndk-build/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 8a161c0

Please sign in to comment.