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

adb: Explicitly match package name in pm list package output #135

Merged
merged 1 commit into from
Oct 10, 2023

Conversation

MarijnS95
Copy link
Member

The positional FILTER argument to pm list package works as a substring match: if you have a package named foo.bar.baz and foo.bar.baz_debug for example, and try to run foo.bar.baz, both packages will be returned, the _debug-suffixed one likely first, and the wrong UID ends up being used as logcat filter.

To counter that we could use the very slow and extremely verbose (thousands of lines) pm dump PACKAGE, or look for the right explicit text match in the line-based package:foo.bar.baz uid:1234 output from pm list package: the latter approach is chosen here.

The positional `FILTER` argument to `pm list package` works as a
substring match: if you have a package named `foo.bar.baz` and
`foo.bar.baz_debug` for example, and try to run `foo.bar.baz`, both
packages will be returned, the `_debug`-suffixed one likely first,
and the wrong UID ends up being used as `logcat` filter.

To counter that we could use the very slow and extremely verbose
(thousands of lines) `pm dump PACKAGE`, _or_ look for the right explicit
text match in the line-based `package:foo.bar.baz uid:1234` output from
`pm list package`: the latter approach is chosen here.
MarijnS95 added a commit to rust-mobile/cargo-apk that referenced this pull request Sep 11, 2023
This is a port of:
rust-mobile/xbuild#131
rust-mobile/xbuild#135

Having never really understood how Android Studio does it, I just
stumbled upon this very new [stackoverflow answer] that has a rather
beatiful solution to the current problems with `pidof`, without
drawbacks.  Pidof has always been flaky as it relies on the app to be
running, which may either take some time or never happen if the app
crashed before `pidof` is first run.  This results in silly workarounds
such as loops that induce extra delay and need to have an upper bound.
And this `pid` changes every time the app is restarted, making it a
tedious process that also doesn't react to manual app restarts on the
device.  Retrieving the `uid` via `pm list packages -U` on the other
hand, and passing that to `logcat --uid` has the following advantages:

- Always available immediately after the app has been installed, no need
  to check it in a loop (no extra delay);
- Doesn't change after the app is (re!)installed, unless the user fully
  deletes and installs the app again;
- Is resilient against app crashes because of that, and allows the user
  to see any error/crash related messages straight away;
- Still includes logs printed by other system components that run or are
  invoked within an app, as before.

The only downside is that `pm list package` possibly returns multiple
packages if there is a substring match; for this reason the code
searches for an explicit match in the output.

[stackoverflow answer]: https://stackoverflow.com/a/76551835
@MarijnS95 MarijnS95 merged commit f58cf19 into master Oct 10, 2023
34 checks passed
@MarijnS95 MarijnS95 deleted the adb-select-current-package-uid branch October 10, 2023 12:48
MarijnS95 added a commit to rust-mobile/cargo-apk that referenced this pull request Nov 21, 2023
This is a port of:
rust-mobile/xbuild#131
rust-mobile/xbuild#135

Having never really understood how Android Studio does it, I just
stumbled upon this very new [stackoverflow answer] that has a rather
beatiful solution to the current problems with `pidof`, without
drawbacks.  Pidof has always been flaky as it relies on the app to be
running, which may either take some time or never happen if the app
crashed before `pidof` is first run.  This results in silly workarounds
such as loops that induce extra delay and need to have an upper bound.
And this `pid` changes every time the app is restarted, making it a
tedious process that also doesn't react to manual app restarts on the
device.  Retrieving the `uid` via `pm list packages -U` on the other
hand, and passing that to `logcat --uid` has the following advantages:

- Always available immediately after the app has been installed, no need
  to check it in a loop (no extra delay);
- Doesn't change after the app is (re!)installed, unless the user fully
  deletes and installs the app again;
- Is resilient against app crashes because of that, and allows the user
  to see any error/crash related messages straight away;
- Still includes logs printed by other system components that run or are
  invoked within an app, as before.

The only downside is that `pm list package` possibly returns multiple
packages if there is a substring match; for this reason the code
searches for an explicit match in the output.

[stackoverflow answer]: https://stackoverflow.com/a/76551835
MarijnS95 added a commit to rust-mobile/cargo-apk that referenced this pull request Nov 22, 2023
This is a port of:
rust-mobile/xbuild#131
rust-mobile/xbuild#135

Having never really understood how Android Studio does it, I just
stumbled upon this very new [stackoverflow answer] that has a rather
beatiful solution to the current problems with `pidof`, without
drawbacks.  Pidof has always been flaky as it relies on the app to be
running, which may either take some time or never happen if the app
crashed before `pidof` is first run.  This results in silly workarounds
such as loops that induce extra delay and need to have an upper bound.
And this `pid` changes every time the app is restarted, making it a
tedious process that also doesn't react to manual app restarts on the
device.  Retrieving the `uid` via `pm list packages -U` on the other
hand, and passing that to `logcat --uid` has the following advantages:

- Always available immediately after the app has been installed, no need
  to check it in a loop (no extra delay);
- Doesn't change after the app is (re!)installed, unless the user fully
  deletes and installs the app again;
- Is resilient against app crashes because of that, and allows the user
  to see any error/crash related messages straight away;
- Still includes logs printed by other system components that run or are
  invoked within an app, as before.

The only downside is that `pm list package` possibly returns multiple
packages if there is a substring match; for this reason the code
searches for an explicit match in the output.

[stackoverflow answer]: https://stackoverflow.com/a/76551835
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant