Android Developer Fundamentals V2
Testing,
debugging, and
using support
libraries
Lesson 3
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 1
Studio License.
V2
3.1 The Android Studio
debugger
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 2
Studio License.
V2
Contents
● All code has bugs
● Android Studio logging
● Android Studio debugger
● Working with breakpoints
● Changing variables
● Stepping through code
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 3
Studio debugger
V2 License.
All Code Has
Bugs
Android Developer Fundamentals V2 4
Bugs
● Incorrect or unexpected result, wrong values
● Crashes, exceptions, freezes, memory leaks
● Causes
○ Human Design or Implementation Error > Fix your code
○ Software fault, but in libraries > Work around limitation
○ Hardware fault or limitation -> Make it work with what's available
Origin of the term "bug" (it's not what you think)
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 5
Studio License.
V2
Debugging
● Find and fix errors
● Correct unexpected and undesirable behavior
● Unit tests help identify bugs and prevent regression
● User testing helps identify interaction bugs
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 6
Studio License.
V2
Android Studio debugging tools
Android Studio has tools that help you
● identify problems
● find where in the source code the problem is created
● so that you can fix it
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 7
Studio License.
V2
Logging with
Android Studio
Android Developer Fundamentals V2 8
Add Log messages to your code
import [Link];
// Use class variable with class name as tag
private static final String TAG =
[Link]();
// Show message in Logcat pane of Android Studio
// Log.<log-level>(TAG, "Message");
Log.d(TAG, “Hello World”);
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 9
Studio License.
V2
Open Logcat pane
Logcat
pane
Logcat
tab
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 10
Studio License.
V2
Inspect logging messages
Log.d("MainActivity", "Hello World");
09-12 [Link].971 4304 /[Link] D/MainActivity: Hello World
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 11
Studio License.
V2
Choose visible logging level
Displays logs with levels at
this level or higher
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 12
Studio License.
V2
Log Levels
● Verbose - All verbose log statements and comprehensive system
● Debug - All debug logs, variable values, debugging notes
● Info - Status info, such as database connection
● Warning - Unexpected behavior, non-fatal issues
● Error - Serious error conditions, exceptions, crashes only
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 13
Studio License.
V2
Debugging
with Android
Studio
Android Developer Fundamentals V2 14
What you can do
● Run in debug mode with attached debugger
● Set and configure breakpoints
● Halt execution at breakpoints
● Inspect execution stack frames and variable values
● Change variable values
● Step through code line by line
● Pause and resume a running program
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 15
Studio License.
V2
Run in debug mode
1
Debugger pane Menu:
opens
2 Run > Debug 'your
app'
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 16
Studio License.
V2
Set breakpoints
Click in the left margin next
to executable line of code
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 17
Studio License.
V2
Edit breakpoint properties
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 18
Studio License.
V2
Make breakpoints conditional
● In properties dialog or right -click existing breakpoint
● Any Java expression that returns a boolean
● Code completion helps you write conditions
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 19
Studio License.
V2
Run until app stops at breakpoint
First Breakpoint
Frames Variables in scope Watches (C/C++)
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 20
Studio License.
V2
Inspect frames
Top frame is where execution is
halted in your code
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 21
Studio License.
V2
Inspect and edit variables
● Right-click on variable for menu
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 22
Studio License.
V2
Basic Stepping Commands
Step Over F8 Step to the next line in current file
Step Into F7 Step to the next executed line
Force Step Into ⇧F7 Step into a method in a class that you wouldn't
normally step into, like a standard JDK class
Step Out ⇧F8 Step to first executed line after returning from
current method
Run to Cursor ⌥F9 Run to the line where the cursor is in the file
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 23
Studio License.
V2
Stepping through code
Show execution point Drop frame Run to cursor
Step over Step into Step out
Force step into
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 24
Studio License.
V2
Resume and Pause
Resum
e
Pause
Mute all
breakpoints Menu:
Run->Pause Program…
Run->Resume Program...
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 25
Studio License.
V2
Learn more
● Debug Your App (Android Studio User Guide)
● Debugging and Testing in Android Studio (video)
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 26
Studio License.
V2
What's Next?
● Concept Chapter: 3.1 The Android Studio debugger
● Practical: 3.1 The debugger
This work is licensed under a Creative
The Android
Android Developer Fundamentals Commons Attribution 4.0 International 27
Studio License.
V2
END
Android Developer Fundamentals V2 28