This document discusses debugging Android apps in Android Studio. It covers running apps in debug mode, using breakpoints, examining the system log, tracking object allocation, and capturing screenshots and videos. Debugging tools in Android Studio allow developers to pause execution at breakpoints, evaluate variables, and fix runtime errors.
2. SESSION CONTENT
• Introduction
• Run your App in Debug Mode.
• Use system Log
• Work with Breakpoints
• Track object Allocation
• Capture Screenshots and Videos
facebook.com/ahmed.e.hassan
2
3. INTRODUCTION
facebook.com/ahmed.e.hassan
3
Android Studio enables you to debug apps running on the emulator or on an
Android device. With Android Studio, you can:
• Select a device to debug your app on.
• View the system log.
• Set breakpoints in your code.
• Examine variables and evaluate expressions at run time.
• Run the debugging tools from the Android SDK.
• Capture screenshots and videos of your app.
4. RUN YOUR APP IN
DEBUG MODE
facebook.com/ahmed.e.hassan
4
To run your app in debug mode, you build an APK signed with a debug key and install it on
a physical Android device or on the Android emulator
To debug your app in Android Studio:
1. Open your project in Android Studio.
2. Click Debug in the toolbar.
3. On the Choose Device window, select a hardware device from the list or choose a
virtual device.
4. Click OK. Your app starts on the selected device.
5. facebook.com/ahmed.e.hassan
5
RUN YOUR APP IN
DEBUG MODE
Android Studio opens the Debug tool window when you debug your app.
To open the Debug window manually, click Debug
This window shows threads and variables in the Debugger tab, the device status
in the Console tab, and the system log in the Logcat tab.
6. facebook.com/ahmed.e.hassan
6
RUN YOUR APP IN
DEBUG MODE
Attach the debugger to a running process
You don't always have to restart your app to debug it. To debug an app that you're
already running:
1. Click Attach debugger to Android process .
2. In the Choose Process window, select the device and app you want to attach the
debugger to.
3. To open the Debug tool window, click Debug
7. USE THE SYSTEM LOG
facebook.com/ahmed.e.hassan
7
The system log shows system messages while you debug your app.
These messages include information from apps running on the device.
If you want to use the system log to debug your app, make sure your code writes log
messages and prints the stack trace for exceptions while your app is in the
development phase.
9. facebook.com/ahmed.e.hassan
9
USE THE SYSTEM LOG
View the system log
To view the system log on the Android DDMS tool window:
1. Start your app .
2. Click Android to open the Android DDMS tool window.
3. If the system log is empty in the Logcat view, click Restart .
10. facebook.com/ahmed.e.hassan
10
USE THE SYSTEM LOG
The system log shows messages from Android services and other Android apps. To
filter the log messages to view only the ones you are interested in, use the tools in the
Android DDMS window:
• To show only log messages for a particular process, select the process in the Devices view and
then click Only Show Logcat from Selected Process . If the Devices view is not available,
click Restore Devices View on the right of the Android DDMS tool window. This button is
only visible when you hide the Devices window.
• To filter log messages by log level, select a level under Log Level on the top of the Android
DDMS window.
• To show only log messages that contain a particular string, enter the string in the search box
and press Enter.
11. WORK WITH
BREAKPOINTS
facebook.com/ahmed.e.hassan
11
Breakpoints enable you to pause the execution of your app at a particular line of
code, examine variables, evaluate expressions, and continue the execution line
by line.
Use breakpoints to determine the causes of run-time errors that you can't fix
by looking at your code only.
To debug your app using breakpoints:
1. Open the source file in which you want to set a breakpoint.
2. Locate the line where you want to set a breakpoint and click on it.
3. Click on the yellow portion of the side bar to the left of this line
4. Start your app
13. facebook.com/ahmed.e.hassan
13
WORK WITH
BREAKPOINTS
Debug your app with breakpoints
After you set breakpoints in your code, click Rerun to start the app again.
When a breakpoint is hit, Android Studio pauses the app and highlights the breakpoint in
the source code.
• To examine the object tree for a variable, expand it in the Variables view. If the Variables view is not
visible, click Restore Variables View .
• To evaluate an expression at the current execution point, click Evaluate Expression .
• To advance to the next line in the code (without entering a method), click Step Over .
• To advance to the first line inside a method call, click Step Into .
• To advance to the next line outside the current method, click Step Out .
• To continue running the app normally, click Resume Program
15. TRACK OBJECT
ALLOCATION
facebook.com/ahmed.e.hassan
15
Android Studio lets you track objects that are being allocated on the Java heap and see
which classes and threads are allocating these objects. This allows you to see the list of
objects allocated during a period of interest. This information is valuable for assessing
memory usage that can affect application performance.
To track memory allocation of objects:
1. Start your app.
2. Click Android to open the Android DDMS tool window.
3. On the Android DDMS tool window, select the Devices | logcat tab.
4. Select your device from the dropdown list.
5. Select your app by its package name from the list of running apps.
6. Click Start Allocation Tracking
7. Interact with your app on the device.
8. Click Stop Allocation Tracking
16. facebook.com/ahmed.e.hassan
16
TRACK OBJECT
ALLOCATION
Android Studio shows the objects that the system allocated with the following information:
• Allocation order
• Allocated class
• Allocation size
• Thread ID
• Allocation method, class, and line number
• Stack trace at the point of allocation
17. CAPTURE SCREENSHOTS
AND VIDEOS
facebook.com/ahmed.e.hassan
17
Android Studio enables you to capture a screenshot or a short video of the device
screen while your app is running.
Screenshots and videos are useful as promotional materials for your app, and you can
also attach them to bug reports that you send to your development team.
To take a screenshot of your app:
1. Start your app.
2. Click Android to open the Android DDMS tool window.
3. Click Screen Capture on the left side of the Android DDMS tool window.
4. Optional: To add a device frame around your screenshot, enable the Frame screenshot option.
5. Click Save.
18. facebook.com/ahmed.e.hassan
18
CAPTURE SCREENSHOTS
AND VIDEOS
To take a video recording of your app:
1. Start your app.
2. Click Android to open the Android DDMS tool window.
3. Click Screen Record on the left side of the Android DDMS tool window.
4. Click Start Recording.
5. Interact with your app.
6. Click Stop Recording.
7. Enter a file name for the recording and click OK.