DEV Community

Cover image for Improve debugging Android apps with lista
Nazarii
Nazarii

Posted on

Improve debugging Android apps with lista

Everybody faced this on a new or large project: You’ve been given a bug to fix and you have no idea where to start and what Activity/Fragment it belongs to. You need to know it to find a piece of evidence and sometimes it might be a complicated process.

Alright, to locate the Activity where this bug occurs, simply navigate to the corresponding screen on your device and execute the command:

$ adb shell dumpsys activity activities \
    | grep com.huawei.smarthome | grep Hist \
    | awk -F '[{}]' '{print $2}' | cut -d ' ' -f3

# Output: com.huawei.smarthome/.activity.MainActivity
Enter fullscreen mode Exit fullscreen mode

Quite long, isn’t it? Let’s break it down.

  1. dumpsys — a tool that provides information about system services
  2. adb shell dumpsys activity — provides information about the state of the activity manager (tasks, processes, activities). We are interested in activities currently. The output of this is enormously big. We need to filter it.
  3. grep com.huawei.smarthome | grep Hist - used to extract information for the concrete package (com.huawei.smarthome in our case) and show only the activity stack.
  4. awk -F ‘[{}]’ ‘{print $2}’ | cut -d ‘ ‘ -f3 — tricky command to extract only the Activity name from the output.

It’s hard enough to remember and enter each time by hand. But you can simplify it:

  • create an alias
  • find by reverse-i-search each time
  • use the autocomplete feature of your terminal

So, you can simplify it a bit. But what if you have more than 1 device connected? adb will ask you to specify a device ID to run the command on.

That’s how to fix it:

❯ adb devices
List of devices attached
78f2ba2b    device

❯ adb -s 78f2ba2b shell dumpsys activity activities \
    | grep com.huawei.smarthome | grep Hist \
    | awk -F '[{}]' '{print $2}' | cut -d ' ' -f3

com.huawei.smarthome/.activity.MainActivity
Enter fullscreen mode Exit fullscreen mode
  1. List IDs of all connected devices
  2. Copy needed device ID
  3. Pass it with the -s parameter to the adb command

The main problem with writing a chain of commands is:

  1. It’s big and hard to remember
  2. You always need to run an additional command to find a list of connected devices (if there is more than 1)
  3. No output if nothing found

I propose a handy version with only 1 required option and out-of-the-box autocompletion to choose between available devices:

Demo

Compare this chain of commands with lista:

Old approach:

❯ adb devices
List of devices attached
78f2ba2b    device

❯ adb -s 78f2ba2b shell dumpsys activity activities \
    | grep com.huawei.smarthome | grep Hist \
    | awk -F '[{}]' '{print $2}' | cut -d ' ' -f3

com.huawei.smarthome/.activity.MainActivity
Enter fullscreen mode Exit fullscreen mode

New approach:

❯ lista -p com.huawei.smarthome -s 78f2ba2b

Serial ID: 78f2ba2b
Package name: com.huawei.smarthome

Activity stack:
com.huawei.smarthome/.activity.MainActivity
com.huawei.smarthome/.login.LauncherActivity
Enter fullscreen mode Exit fullscreen mode

As observed, this approach results in less typing yet provides more information. Although the command appears somewhat lengthy, the autocomplete feature effectively compensates for its length.

Usage

It has only 3 options:

-s serial id - device serial ID. Autocompletable field.
It should be used if more than one Android device is connected to your machine.

-p package name (required unless -h is used). 
Example: -p "com.google.mail"

-h get help

Usage: lista [options]
Enter fullscreen mode Exit fullscreen mode

It’s the output from the command manual. If it’s not quite self-explanatory, let’s see examples.

Examples

# If only one device is attached
lista -p "com.google.mail"

# If multiple devices are connected, add serial ID with the -s option. Autocompletable field
lista -p "com.google.mail" -s 78f2ba2b

# Help menu
lista -h
Enter fullscreen mode Exit fullscreen mode

You can get acquainted with this tool by exploring its Github repo.

Top comments (0)