Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Android Debugging Slides

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Android Debugging

Intro to debugging
“Debugging is twice as hard as writing the code in the
first place.”

Writing code seems like the hard part until your


program doesn’t seem to work like you hoped.

How do you identify what’s happening?


It happens to the best
Figuring out errors is part of writing the code.

Debugging is an essential skill usually not emphasized


when you first start learning how to code.

Our goal is to help provide you with a few approaches and


tools to make you faster at troubleshooting.
Compile vs. runtime errors
Compile-time errors are what you need to handle so your
program will run. You must fix them so that the program
can start.

Just because your program runs, doesn’t mean it’s doing


what you expected. We consider this class of issues to be
runtime errors.
Why?
Every unexpected scenario you
encounter has an explanation.

Identify possible reasons and


systematically rule them out.

Consider debugging as an opportunity to


fill knowledge gaps
(CodePath Cliff Notes, Android docs,
source code)
Repeatability
Step 1: Convince yourself that there is an issue in the code.

If you find a bug, start ruling out causes.

Take several tries to make the error happen again.

It’s OK to restart the app / emulator / build process!


Identifying the cause
How do you prove or disprove
what’s happening?

Try to isolate the simplest way to


reproduce what’s occurring.

You may follow several rabbit holes


before uncovering the real answer.
Experimentation
Runtime problems can seem intimidating.

If you’re developing a theory about what’s wrong, you may worry


about messing up your work by making more changes.

You can safely experiment by saving a backup copy of your file.

If you’re comfortable using version control, put your project in a


local repository. Use branches to try out experiments.
Experimental git branches
git checkout master
git checkout -b my_fix_branch master

… make changes on my_fix_branch…


git checkout master
When your changes fix the issue: git merge my_fix_branch

git checkout master


Not going so well? git branch -D my_fix_branch
Exceptions
Exceptions are caused by unexpected situations that occur during
runtime (when the program runs).

The code compiled OK, but produced data that it doesn’t know how to use.

Exception objects (i.e. NullPointerException) indicate what happened.


Using LogCat
Otherwise known as “print debugging”, from before there
were IDE debuggers like Android Studio.

The idea is to place log statements to watch the flow of the


code.
Where’s LogCat?
Android Studio:
⌘-6 or Ctrl-6
Stack traces
Stack traces help identify
where problems are
happening.

They list the chain of


events that triggered the
exception.
Stack traces
First, check the stack trace,
discerning for the area of
the code you may have
modified.

The real cause of the error


may be in the code on the
“Caused by:” line.
Interactive Debugging
Android Studio lets
you set breakpoints in
your code.

Breakpoints help you


inspect variables and
run the program very
slowly to understand
each detail.
Where’s the debugger?
Android Studio:
⌘-5 or Ctrl-5

A Logcat tab
is in this group!
Breakpoint Actions
“Step Over” and “Resume” are most commonly used.
“Resume” runs the program to the next breakpoint.

Step Into
Resume

Step Over

If the issue occurs between two breakpoints, you’re on the


right track!
Evaluating expressions
When the program stops on a breakpoint, you can evaluate
part of the line to understand every last detail.

Can you find out the result of firstRepeatedCharacter()?


Evaluating Expressions

Highlight an interesting
expression, right-click on it, and
select “Evaluate Expression”.

Now you can check whether the


code you highlighted behaves like
you wanted.
Console output
Android Studio also provides an extra Console
tab to see stack traces:
Tip: Clear your LogCat
This way, you can avoid confusing yourself with whether the results
belonged to a previous test run.
Tip: Turn off log filtering
Android Studio heavily filters LogCat by default.
At first, you may not see the message you were looking for.
Permissions

Missing permissions can cause code to fail


silently or with runtime errors

Solution: Check the Android Manifest


Source code tracing
Not sure what’s going on beneath the surface of
your code?
Other Resources and Tools
● Chrome Dev HTTP Client - to verify your remote
API calls are correct.

● https://medium.com/google-developers/developing-for
-android-ix-tools-375134af1098
- Intro to advanced debugging tools

● http://jsonformatter.curiousconcept.com
Summary
Recognize that debugging is a skill
that is an important part of the
coding process.

A lot of debugging is detective work


and unraveling the mystery.

We’ve equipped you with a few tools


-- find what works best for you!
Exercises
Setup
Is Android Studio 3.0+ installed?
Do you have an emulator ready to use?

1. Open Terminal.app and navigate to your working directory.


2. git clone https://github.com/rogerhu/codepath-debugging

(Assumes API version 18 and Build Tools 19.1.0 or later)

3. Open Android Studio and import the exercise1 project.


File… Import Project
Exercise 1: Word Tester
https://github.com/codepath/android-
debugging-exercises/tree/master/exe
rcise1

When does the program crash?

1. meet
2. hello
3. test

How can you fix it?


Exercise 2: ToDo app
https://github.com/codepath/android-
debugging-exercises/tree/master/exerc
ise2

Figure out why the items are not being


saved to disk when closing the app.

How do we verify what’s happening?


Exercise 3: API Client
Figure out why our Rotten Tomatoes client is broken.
There are at least 3 issues with it.

https://github.com/codepath/android-
debugging-exercises/tree/master/ex
ercise3

You might also like