Android development session 5 - Debug android studio
Embed Size (px)
Transcript of Android development session 5 - Debug android studio
Android DevelopmentSession 5 debugging with android studioAhmed ezz el - din
Session contentIntroductionRun your App in Debug Mode.Use system LogWork with BreakpointsTrack object AllocationCapture Screenshots and Videos
introductionfacebook.com/ahmed.e.hassan3Android 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.
Run your App in Debug Modefacebook.com/ahmed.e.hassan4To 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 emulatorTo debug your app in Android Studio:Open your project in Android Studio.Click Debug in the toolbar.On the Choose Device window, select a hardware device from the list or choose a virtual device.Click OK. Your app starts on the selected device.
facebook.com/ahmed.e.hassan5Run 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.
facebook.com/ahmed.e.hassan6Run 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:Click Attach debugger to Android process .In the Choose Process window, select the device and app you want to attach the debugger to.To open the Debug tool window, click Debug
Use the system logfacebook.com/ahmed.e.hassan7The 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.
facebook.com/ahmed.e.hassan8Use the system logDuring development, your code can also catch exceptions and write the stack trace to the system log:
facebook.com/ahmed.e.hassan9Use the system logView the system logTo view the system log on the Android DDMS tool window:Start your app .Click Android to open the Android DDMS tool window.If the system log is empty in the Logcat view, click Restart .
facebook.com/ahmed.e.hassan10Use 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.
Work with breakpointsfacebook.com/ahmed.e.hassan11Breakpoints 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:Open the source file in which you want to set a breakpoint.Locate the line where you want to set a breakpoint and click on it.Click on the yellow portion of the side bar to the left of this line Start your app
facebook.com/ahmed.e.hassan12Work with breakpointsView and configure breakpointsTo view all the breakpoints and configure breakpoint settings, click View Breakpoints on the left side of the Debug tool window. The Breakpoints window appears
facebook.com/ahmed.e.hassan13Work 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
Work with breakpoints
Track object allocationfacebook.com/ahmed.e.hassan15Android 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:Start your app.Click Android to open the Android DDMS tool window.On the Android DDMS tool window, select the Devices | logcat tab.Select your device from the dropdown list.Select your app by its package name from the list of running apps.Click Start Allocation Tracking Interact with your app on the device.Click Stop Allocation Tracking
facebook.com/ahmed.e.hassan16Track object allocationAndroid Studio shows the objects that the system allocated with the following information:Allocation orderAllocated classAllocation sizeThread IDAllocation method, class, and line numberStack trace at the point of allocation
Capture screenshots and videosfacebook.com/ahmed.e.hassan17Android 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:Start your app.Click Android to open the Android DDMS tool window.Click Screen Capture on the left side of the Android DDMS tool window.Optional: To add a device frame around your screenshot, enable the Frame screenshot option.Click Save.
facebook.com/ahmed.e.hassan18Capture screenshots and videosTo take a video recording of your app:Start your app.Click Android to open the Android DDMS tool window.Click Screen Record on the left side of the Android DDMS tool window.Click Start Recording.Interact with your app.Click Stop Recording.Enter a file name for the recording and click OK.