Dumpsys-like functionality for apk app
Asked Answered
B

1

2

I am looking for a solution to access the internal state of our app from adb for debugging purposes. I have used dumpsys a lot to get the internal state of system services so now I wonder if I can expose an interface from our app to adb. The only thing I have thought of so far is to log the information periodically and then just read logcat but if possible I would like to be able to interact directly with the app over adb so question is how to implement this in our app.

Brendon answered 1/5, 2018 at 7:42 Comment(13)
sure: adb shell dumpsys activity top and override Activity#dump method, the same for Service, ContentProvider etcPunctual
@Punctual this sounds awesome. Can I filter on the bundle id? Can I also interact in some way? Like sending commands to the app? If so please add an answer since then you covered my question in fullBrendon
ok if you want to use some commands use: adb shell dumpsys activity top any command line can be entered here and get them from a last param passed to Activity#dump method, what bundle id?Punctual
so if the bundle id is se.jensolsson.testapp ? can I filter on this without using grep or something? Dont want to overload the device.Brendon
is your Activity started and visible? or you want to dump a custom Service running in a background? or ContentProvider? or something else?Punctual
It seem like targeting the server is probably best, got this to work but I wonder what the limits are. I tried on an Android 5.0 device and it did not work. If I run adb shell dumpsys activity -p se.jensolsson.app service se.jensolsson.app.service, it gives me Unknown argument: -p; use -h for helpBrendon
yes indeed: run adb shell dumpsys activity -h or see ar-g.github.io/ADB-Shell-Part-3 and ^F adb shell dumpsys activity -hPunctual
@Punctual I cannot see how I can dumpsys activity on a service on android 5.0. -h returns the followingBrendon
service [COMP_SPEC]: service client-side state - see adb shell dumpsys activity -hPunctual
Thanks found it! It worked!Brendon
but remember: your service has to be started and COMP_SPEC does not have to be full service class name - in most cases you can simply use just class name, like MySuperServicePunctual
from dumpsys activity -h: "COMP_SPEC may be a component name (com.foo/.myApp), a partial substring in a component name, a hex object identifier."Punctual
@Punctual can you please add this as an answer since it is really the correct answer.Brendon
P
5

in your Activity override dump() method, for example:

@Override
public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
    //super.dump(prefix, fd, writer, args);
    writer.println();
    writer.println("command line arguments:");
    writer.format("length: %d, args: %s", args.length, Arrays.toString(args)).println();
}

then start your Activity and type in the terminal:

adb shell dumpsys activity top your cmd line arguments

or:

adb shell dumpsys activity top

if you don't want to pass any arguments to dump() method

the same method exists in other components like Service or ContentProvider - see their official documentation on how to invoke them by using adb shell dumpsys activity command, also there are classes that have their dump() methods that can be used to dump their internal state - for example ApplicationInfo, ActivityInfo, Looper, Handler, Binder, Fragment and many others

Punctual answered 19/8, 2018 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.