<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nazarii</title>
    <description>The latest articles on Forem by Nazarii (@moshenskyi_n).</description>
    <link>https://forem.com/moshenskyi_n</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1224481%2F4238e8c1-9002-4c19-99aa-c8297641b927.png</url>
      <title>Forem: Nazarii</title>
      <link>https://forem.com/moshenskyi_n</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/moshenskyi_n"/>
    <language>en</language>
    <item>
      <title>Improve debugging Android apps with lista</title>
      <dc:creator>Nazarii</dc:creator>
      <pubDate>Mon, 15 Jan 2024 22:25:20 +0000</pubDate>
      <link>https://forem.com/moshenskyi_n/improve-debugging-android-apps-with-lista-4oa5</link>
      <guid>https://forem.com/moshenskyi_n/improve-debugging-android-apps-with-lista-4oa5</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Alright, to locate the Activity where this bug occurs, simply navigate to the corresponding screen on your device and execute the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ adb shell dumpsys activity activities \
    | grep com.huawei.smarthome | grep Hist \
    | awk -F '[{}]' '{print $2}' | cut -d ' ' -f3

# Output: com.huawei.smarthome/.activity.MainActivity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quite long, isn’t it? Let’s break it down.&lt;/p&gt;

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

&lt;p&gt;It’s hard enough to remember and enter each time by hand. But you can simplify it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create an alias&lt;/li&gt;
&lt;li&gt;find by reverse-i-search each time&lt;/li&gt;
&lt;li&gt;use the autocomplete feature of your terminal&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;That’s how to fix it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;List IDs of all connected devices&lt;/li&gt;
&lt;li&gt;Copy needed device ID&lt;/li&gt;
&lt;li&gt;Pass it with the -s parameter to the adb command&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The main problem with writing a chain of commands is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It’s big and hard to remember&lt;/li&gt;
&lt;li&gt;You always need to run an additional command to find a list of connected devices (if there is more than 1)&lt;/li&gt;
&lt;li&gt;No output if nothing found&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I propose a handy version with only 1 required option and out-of-the-box autocompletion to choose between available devices:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9KUKmiqm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fyuxn1gq1qxir928m5v.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9KUKmiqm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fyuxn1gq1qxir928m5v.gif" alt="Demo" width="600" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Compare this chain of commands with lista:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Old approach&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;New approach&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It has only 3 options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-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]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s the output from the command manual. If it’s not quite self-explanatory, let’s see examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get acquainted with this tool by exploring its &lt;a href="https://github.com/moshenskyi/lista"&gt;Github repo&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>androiddev</category>
      <category>bash</category>
      <category>adb</category>
    </item>
  </channel>
</rss>
