Logcat and Debugger
Android Studio 2: The New UI
Course 2 ยท Chapter 7 ยท Logcat and the Debugger
When an app misbehaves you have two tools: read what it says (Logcat) and stop it mid-run to look inside (the debugger). Both work on a running device from Chapter 6. This chapter covers the Logcat window and its query-style filter box, then breakpoints, stepping and the Debug window.
The Logcat Window
Logcat shows the messages the device's system and your app write. On the install it is a tool window (View > Tool Windows > Logcat; it has no shortcut) with these parts:
| Part | What it does |
|---|---|
| Tab strip, with a + button | Several Logcat tabs, each with its own device and filter. |
| Device drop-down | Which device's log to show; it names the emulator and its Android version. |
| Filter box | Defaults to package:mine: only your project's app. |
| Cc toggle | Match case in the filter. |
| Star, help icon | Save the current filter as a favourite; open help. |
| Vertical button strip | Clear, pause, restart, wrap lines, scroll to end and others (I could not read every icon's name). |
The Query Filter
(docs) The filter takes key:value terms for tag, package, process, message and level. package:mine is special: it matches
any package in the open project. level:INFO matches INFO and everything more serious (WARN, ERROR, ASSERT). A leading minus negates a term, and terms combine with &, | and
parentheses. A ~ after the key (tag~:) switches that term to a regular expression.
The docs' own example is (tag:foo | level:ERROR) & package:mine; the other lines apply the same rules (Choreographer and Catalogue are example tags). To write a message yourself,
use Log.d("Catalogue", "saved item") in Kotlin (general knowledge) and filter on that tag.
The Debugger
(docs) Run the app with Debug instead of Run. When execution reaches a breakpoint (click in the editor's left gutter next to a line) the app pauses, and you can examine variables, evaluate expressions and continue line by line. The docs also describe conditional breakpoints (stop only when a condition is true) and logging breakpoints (write a message to Logcat without stopping).
| Action | What it does |
|---|---|
| Step over | Run the current line, stop at the next one (general knowledge). |
| Step into | Go inside the function the line calls (general knowledge). |
| Step out | Finish the current function and stop in its caller (general knowledge). |
| Resume | Run until the next breakpoint (general knowledge). |
| Evaluate expression | Run a Kotlin expression in the paused frame; Alt+F8 on Windows and Linux (docs). The Variables pane also lets you evaluate ad hoc expressions. |
The Debug tool window is Alt+5 and Run is Alt+4 (Chapter 1's tool-window list, checked on the install).
package:mine
replaces the old "Show only selected application" idea (my reading of the two, general knowledge). The debugger's core ideas are unchanged.
Try It Yourself
Run the scratch project on a device, open Logcat and describe each control you can see. Clear the log, then switch the filter from package:mine to level:ERROR and say what changes.
Add Log.d("Demo", "hello") to onCreate in MainActivity.kt and run. Write filters that show only that message by tag, and by message text. Save one as a favourite with the star.
Set a breakpoint on a line that runs at start-up, start the app with Debug, and when it pauses step over twice, then evaluate an expression with Alt+F8. Finally change the breakpoint into a logging breakpoint and check the message reaches Logcat.
๐ View solutionpackage:mine default, Logcat having no shortcut, and the Run and Debug shortcuts (Alt+4, Alt+5).Checked in the docs (summaries only): the key names,
package:mine, level: semantics, negation, combining, the ~ regex form, conditional and logging breakpoints, and Alt+F8.Not verified: the exact names of the Logcat button-strip icons; the Debug window's layout and button names; the step actions' shortcuts; how Logcat's old and new filters map to each other; my example queries (not run); the exercises (not run by me). A screenshot of the Debug window while paused would verify the debugger half.
Sources: View logs with Logcat (developer.android.com), Debug your app (developer.android.com).