Download - Android development session 5 - Debug android studio

Transcript
Page 1: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 1

ANDROID DEVELOPMENT

SESSION 5 – DEBUGGING WITH ANDROID STUDIOAHMED EZZ EL - DIN

Page 2: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 2

SESSION CONTENT• Introduction• Run your App in Debug Mode.• Use system Log• Work with Breakpoints• Track object Allocation• Capture Screenshots and Videos

Page 3: Android development   session 5 - Debug android studio

3

INTRODUCTION

facebook.com/ahmed.e.hassan

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.

Page 4: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 4

RUN YOUR APP IN DEBUG MODETo 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.

Page 5: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 5

RUN YOUR APP IN DEBUG MODEAndroid 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.

Page 6: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 6

RUN YOUR APP IN DEBUG MODEAttach the debugger to a running processYou 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

Page 7: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 7

USE THE SYSTEM LOGThe 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.

Page 8: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 8

USE THE SYSTEM LOGDuring development, your code can also catch exceptions and write the stack trace to the system log:

Page 9: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 9

USE THE SYSTEM LOGView 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 .

Page 10: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 10

USE THE SYSTEM LOGThe 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.

Page 11: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 11

WORK WITH BREAKPOINTSBreakpoints 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

Page 12: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 12

WORK WITH BREAKPOINTSView and configure breakpoints

To view all the breakpoints and configure breakpoint settings, click View Breakpoints on the left side of the Debug tool window. The Breakpoints window appears

Page 13: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 13

WORK WITH BREAKPOINTSDebug your app with breakpointsAfter 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

Page 14: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 14

WORK WITH BREAKPOINTS

Page 15: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 15

TRACK OBJECT ALLOCATIONAndroid 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

Page 16: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 16

TRACK OBJECT ALLOCATIONAndroid 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

Page 17: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 17

CAPTURE SCREENSHOTS AND VIDEOSAndroid 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.

Page 18: Android development   session 5 - Debug android studio

facebook.com/ahmed.e.hassan 18

CAPTURE SCREENSHOTS AND VIDEOSTo 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.