Application Programming Hend Alkittawi Debugging How To Debug Java Code and Android Applications DEBUGGING ANDROID APPLICATIONS - Some of the problems you may encounter while building android applications. The application may crash at runtime or not function as expected! - As an IDE, Android Studio has some tools to help developers debug their code. These tools are: - The Logcat - Android Lint - The Debugger THE LOGCAT - When an application is running, the Logcat tool window provides access to diagnostic messages. - The log output displays messages which are created using the Log class - The syntax to create a log diagnostic message is Log.[logLevelInitial]("TAG", "Message") - Some of the available log levels: - Information, Verbose, Debug, Error - the log level and the tag helps with filtering messages - Example: Log.d("PeekActivity", "Button Clicked!") THE LOGCAT ANDROID LINT - Android Lint is a static analyzer for Android code - a program that examines your code to find defects without running it - Android Lint is aware of the Android framework components and and can find problems that the compiler cannot find. - For example, it is good for finding issues in XML files. - To run Android Lint: Code > Inspect Code the click Analyze ANDROID LINT THE DEBUGGER - The debugger is a tool mainly used to run the application under controlled conditions that permit the developer to track its execution and monitor changes in computer resources that may indicate malfunctioning code. - To control the application execution, add breakpoints to your code lines. - A breakpoint next to a line of code pauses the execution before the line executes and allows you to examine what happens next. - To run the application in debug-mode, add breakpoint(s) then Run > Debug THE DEBUGGER - When using the debugger, the developer can control the app execution: - Step Into - A method is about to be invoked, and you want to debug into the code of that method, so the next step is to go into that method and continue debugging step-by-step. - Step Over - A method is about to be invoked, but you're not interested in debugging this particular invocation, so you want the debugger to execute that method completely as one entire step. - Step Return - You're done debugging this method step-by-step, and you just want the debugger to run the entire method until it returns as one entire step. - Resume - You want the debugger to resume "normal" execution instead of step-by-step p THE DEBUGGER 1. k ea r b g 2. bu e D nt i po 3. Control execution Tra ck exe cut ion ap DEBUGGING ANDROID APPLICATIONS - When errors persist, some things you can try - Recheck the xml in resource files - Clean the project build - - Build > Clean Project - Android studio will rebuild the project from scratch Clean the project - File > Invalidate Caches/Restart - Android Studio will perform some maintenance on the project and restart itself - Check online :) CODE DEMO - Show the different tools in Android Studio. DO YOU HAVE ANY QUESTIONS? THANK YOU! @ hend.alkittawi@utsa.edu By Appointment Online