1 page.title=Debugging with Android Studio 2 3 @jd:body 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 <h2>In this document</h2> 8 <ol> 9 <li><a href="#runDebug">Run your App in Debug Mode</a></li> 10 <li><a href="#systemLog">Use the System Log</a> 11 <ol> 12 <li><a href="#systemLogWrite">Write log messages in your code</a></li> 13 <li><a href="#systemLogView">View the system log</a></li> 14 </ol> 15 </li> 16 <li><a href="#breakPoints">Work with Breakpoints</a> 17 <ol> 18 <li><a href="#breakPointsView">View and configure breakpoints</a></li> 19 <li><a href="#breakPointsDebug">Debug your app with breakpoints</a></li> 20 </ol> 21 </li> 22 <li><a href="#allocTracker">Track Object Allocation</a></li> 23 <li><a href="#deviceMonitor">Analyze Runtime Metrics to Optimize your App</a></li> 24 <li><a href="#screenCap">Capture Screenshots and Videos</a></li> 25 </ol> 26 <h2>See also</h2> 27 <ul> 28 <li><a href="{@docRoot}sdk/installing/studio-tips.html"> 29 Android Studio Tips and Tricks</a></li> 30 <li><a href="{@docRoot}tools/debugging/index.html">Debugging</a></li> 31 <li><a href="{@docRoot}tools/help/monitor.html">Device Monitor</a></li> 32 <li><a href="{@docRoot}tools/debugging/ddms.html">Using DDMS</a></li> 33 </div> 34 </div> 35 36 <p>Android Studio enables you to debug apps running on the emulator or on an Android device. 37 With Android Studio, you can:</p> 38 39 <ul> 40 <li>Select a device to debug your app on.</li> 41 <li>View the system log.</li> 42 <li>Set breakpoints in your code.</li> 43 <li>Examine variables and evaluate expressions at run time.</li> 44 <li>Run the debugging tools from the Android SDK.</li> 45 <li>Capture screenshots and videos of your app.</li> 46 </ul> 47 48 <p>To debug your app, Android Studio builds a debuggable version of your app, connects 49 to a device or to the emulator, installs the app and runs it. The IDE shows the system log 50 while your app is running and provides debugging tools to filter log messages, work with 51 breakpoints, and control the execution flow.</p> 52 53 54 <h2 id="runDebug">Run your App in Debug Mode</h2> 55 56 <div class="figure" style="width:419px"> 57 <img src="{@docRoot}images/tools/as-debugdevices.png" alt=""/> 58 <p class="img-caption"><strong>Figure 1.</strong> The Choose Device window enables you to 59 select a physical Android device or a virtual device to debug your app.</p> 60 </div> 61 62 <p>To run your app in debug mode, you build an APK signed with a debug key and install it on a 63 physical Android device or on the Android emulator. 64 To set up an Android device for development, see <a href="{@docRoot}tools/device.html">Using 65 Hardware Devices</a>. For more information about the emulator provided by the Android SDK, see 66 <a href="{@docRoot}tools/devices/emulator.html">Using the Emulator.</a></p> 67 68 <p>To debug your app in Android Studio:</p> 69 70 <ol> 71 <li>Open your project in Android Studio.</li> 72 <li>Click <strong>Debug</strong> <img src="{@docRoot}images/tools/as-debugbutton.png" 73 style="vertical-align:bottom;margin:0;height:22px" alt=""/> in the toolbar.</li> 74 <li>On the <em>Choose Device</em> window, select a hardware device from the list or 75 choose a virtual device.</li> 76 <li>Click <strong>OK</strong>. Your app starts on the selected device.</li> 77 </ol> 78 79 <p>Figure 1 shows the <em>Choose Device</em> window. The list shows all the Android devices 80 connected to your computer. Select <strong>Launch Emulator</strong> to use an Android virtual device 81 instead. Click the ellipsis <img src="{@docRoot}images/tools/as-launchavdm.png" 82 style="vertical-align:bottom;margin:0;height:19px" alt=""/> to open the 83 <a href="{@docRoot}tools/devices/managing-avds.html">Android Virtual Device Manager</a>.</p> 84 85 <p>Android Studio opens the <em>Debug</em> tool window when you debug your app. To open the 86 <em>Debug</em> window manually, click <strong>Debug</strong> 87 <img src="{@docRoot}images/tools/as-debugwindowbutton.png" 88 alt="" style="vertical-align:bottom;margin:0;height:20px"/>. 89 This window shows threads and variables in the <em>Debugger</em> tab, the device status in the 90 <em>Console</em> tab, and the system log in the <em>Logcat</em> tab. The <em>Debug</em> tool 91 window also provides other debugging tools covered in the following sections.</p> 92 93 <img src="{@docRoot}images/tools/as-debugview.png" alt="" /> 94 <p class="img-caption"><strong>Figure 2.</strong> The Debug tool window in Android Studio showing 95 the current thread and the object tree for a variable.</p> 96 97 98 <h2 id="systemLog">Use the System Log</h2> 99 100 <p>The system log shows system messages while you debug your app. These messages include 101 information from apps running on the device. If you want to use the 102 system log to debug your app, make sure your code writes log messages and prints the stack 103 trace for exceptions while your app is in the development phase.</p> 104 105 <h3 id="systemLogWrite">Write log messages in your code</h3> 106 107 <p>To write log messages in your code, use the {@link android.util.Log} class. Log messages 108 help you understand the execution flow by collecting the system debug output while you interact 109 with your app. Log messages can tell you what part of your application failed. For more 110 information about logging, see <a href="{@docRoot}tools/debugging/debugging-log.html"> 111 Reading and Writing Logs</a>.</p> 112 113 <p>The following example shows how you might add log messages to determine if previous state 114 information is available when your activity starts:</p> 115 116 <pre> 117 import android.util.Log; 118 ... 119 public class MyActivity extends Activity { 120 private static final String TAG = MyActivity.class.getSimpleName(); 121 ... 122 @Override 123 public void onCreate(Bundle savedInstanceState) { 124 if (savedInstanceState != null) { 125 Log.d(TAG, "onCreate() Restoring previous state"); 126 /* restore state */ 127 } else { 128 Log.d(TAG, "onCreate() No saved state available"); 129 /* initialize app */ 130 } 131 } 132 } 133 </pre> 134 135 <p>During development, your code can also catch exceptions and write the stack trace to the system 136 log:</p> 137 138 <pre> 139 void someOtherMethod() { 140 try { 141 ... 142 } catch (SomeException e) { 143 Log.d(TAG, "someOtherMethod()", e); 144 } 145 } 146 </pre> 147 148 <p class="note"><strong>Note:</strong> Remove debug log messages and stack trace print calls from 149 your code when you are ready to publish your app. You could do this by setting a <code>DEBUG</code> 150 flag and placing debug log messages inside conditional statements.</p> 151 152 153 <h3 id="systemLogView">View the system log</h3> 154 155 <p>Both the <em>Android DDMS</em> (Dalvik Debug Monitor Server) and the <em>Debug</em> tool windows 156 show the system log; however, the <em>Android DDMS</em> tool window lets you view only log messages 157 for a particular process. To view the system log on the <em>Android DDMS</em> tool window:</p> 158 159 <ol> 160 <li>Start your app as described in <a href="#runDebug">Run your App in Debug Mode</a>.</li> 161 <li>Click <strong>Android</strong> <img src="{@docRoot}images/tools/as-android.png" alt="" 162 style="vertical-align:bottom;margin:0;height:20px"/> to open the <em>Android DDMS</em> 163 tool window.</li> 164 <li>If the system log is empty in the <em>Logcat view</em>, click <strong>Restart</strong> 165 <img src="{@docRoot}images/tools/as-restart.png" alt="" 166 style="vertical-align:bottom;margin:0;height:22px"/>.</li> 167 </ol> 168 169 <img src="{@docRoot}images/tools/as-ddmslog.png" alt="" /> 170 <p class="img-caption"><strong>Figure 4.</strong> The system log in the Android DDMS tool 171 window.</p> 172 173 <p>The <em>Android DDMS</em> tool window gives you access to some DDMS features from Android Studio. 174 For more information about DDMS, see <a href="{@docRoot}tools/debugging/ddms.html">Using DDMS</a>. 175 </p> 176 177 <p>The system log shows messages from Android services and other Android apps. To filter the log 178 messages to view only the ones you are interested in, use the tools in the <em>Android DDMS</em> 179 window:</p> 180 181 <ul> 182 <li>To show only log messages for a particular process, select the process in the 183 <em>Devices</em> view and then click <strong>Only Show Logcat from Selected 184 Process</strong> <img src="{@docRoot}images/tools/as-currentproc.png" alt="" 185 style="vertical-align:bottom;margin:0;height:20px"/>. If the <em>Devices</em> view 186 is not available, click <strong>Restore Devices View</strong> 187 <img src="{@docRoot}images/tools/as-showdevview.png" alt="" 188 style="vertical-align:bottom;margin:0;height:20px"/> on the right of the <em>Android 189 DDMS</em> tool window. This button is only visible when you hide the <em>Devices</em> 190 window.</li> 191 <li>To filter log messages by log level, select a level under <em>Log Level</em> on the top 192 of the <em>Android DDMS</em> window.</li> 193 <li>To show only log messages that contain a particular string, enter the string in the search 194 box and press <strong>Enter</strong>.</li> 195 </ul> 196 197 198 <h2 id="breakPoints">Work with Breakpoints</h2> 199 200 <p>Breakpoints enable you to pause the execution of your app at a particular line of code, examine 201 variables, evaluate expressions, and continue the execution line by line. Use breakpoints to 202 determine the causes of run-time errors that you can't fix by looking at your code only. To debug 203 your app using breakpoints:</p> 204 205 <ol> 206 <li>Open the source file in which you want to set a breakpoint.</li> 207 <li>Locate the line where you want to set a breakpoint and click on it.</li> 208 <li>Click on the yellow portion of the side bar to the left of this line, as shown in figure 5.</li> 209 <li>Start your app as described in <a href="#runDebug">Run your App in Debug Mode</a>.</li> 210 </ol> 211 212 <p>Android Studio pauses the execution of your app when it reaches the breakpoint. You can then 213 use the tools in the <em>Debug</em> tool window to identify the cause of the error.</p> 214 215 <img src="{@docRoot}images/tools/as-breakpointline.png" alt="" /> 216 <p class="img-caption"><strong>Figure 5.</strong> A red dot appears next to the line when you set 217 a breakpoint.</p> 218 219 <h3 id="breakPointsView">View and configure breakpoints</h3> 220 221 <p>To view all the breakpoints and configure breakpoint settings, click <strong>View 222 Breakpoints</strong> <img src="{@docRoot}images/tools/as-viewbreakbutton.png" alt="" 223 style="vertical-align:bottom;margin:0;height:20px"/> on the left side of the <em>Debug</em> tool 224 window. The <em>Breakpoints</em> window appears, as shown in figure 6.</p> 225 226 <img src="{@docRoot}images/tools/as-breakpointswindow.png" alt="" /> 227 <p class="img-caption"><strong>Figure 6.</strong> The Breakpoints window lists all the current 228 breakpoints and includes behavior settings for each.</p> 229 230 <p>The <em>Breakpoints</em> window lets you enable or disable each breakpoint from the 231 list on the left. If a breakpoint is disabled, Android Studio does not pause your app when 232 it hits that breakpoint. Select a breakpoint from the list to configure its settings. 233 You can configure a breakpoint to be disabled at first and have the system enable it after a 234 different breakpoint is hit. You can also configure whether a breakpoint should be disabled after 235 it is hit. To set a breakpoint for any exception, select <strong>Exception Breakpoints</strong> 236 in the list of breakpoints.</p> 237 238 <h3 id="breakPointsDebug">Debug your app with breakpoints</h3> 239 240 <p>After you set breakpoints in your code, click <strong>Rerun</strong> 241 <img src="{@docRoot}images/tools/as-restart.png" alt="" 242 style="vertical-align:bottom;margin:0;height:20px"/> to start the app again. When a breakpoint is 243 hit, Android Studio pauses the app and highlights the breakpoint in the source code. The 244 <em>Debug</em> tool window lets you examine variables and control the execution step by 245 step:</p> 246 247 <ul> 248 <li> 249 <p>To examine the object tree for a variable, expand it in the <em>Variables</em> view. If 250 the <em>Variables</em> view is not visible, click <strong>Restore Variables View</strong> 251 <img src="{@docRoot}images/tools/as-varviewbutton.png" alt="" 252 style="vertical-align:bottom;margin:0;height:20px"/>.</p> 253 </li> 254 <li> 255 <p>To evaluate an expression at the current execution point, click <strong>Evaluate 256 Expression</strong> <img src="{@docRoot}images/tools/as-evalexpbutton.png" alt="" 257 style="vertical-align:bottom;margin:0;height:20px"/>.</p> 258 </li> 259 <li> 260 <p>To advance to the next line in the code (without entering a method), click <strong>Step 261 Over</strong> <img src="{@docRoot}images/tools/as-stepoverbutton.png" alt="" 262 style="vertical-align:bottom;margin:0;height:20px"/>.</p> 263 </li> 264 <li> 265 <p>To advance to the first line inside a method call, click <strong>Step 266 Into</strong> <img src="{@docRoot}images/tools/as-stepintobutton.png" alt="" 267 style="vertical-align:bottom;margin:0;height:20px"/>.</p> 268 </li> 269 <li> 270 <p>To advance to the next line outside the current method, click <strong>Step 271 Out</strong> <img src="{@docRoot}images/tools/as-stepoutbutton.png" alt="" 272 style="vertical-align:bottom;margin:0;height:20px"/>.</p> 273 </li> 274 <li> 275 <p>To continue running the app normally, click <strong>Resume Program</strong> 276 <img src="{@docRoot}images/tools/as-resumeprogrambutton.png" alt="" 277 style="vertical-align:bottom;margin:0;height:20px"/>.</p> 278 </li> 279 </ul> 280 281 <img src="{@docRoot}images/tools/as-variablesview.png" alt="" /> 282 <p class="img-caption"><strong>Figure 7.</strong> The Variables view in the Debug tool window.</p> 283 284 285 <h2 id="allocTracker">Track Object Allocation</h2> 286 287 <p>Android Studio lets you track objects that are being allocated on the Java heap and see which 288 classes and threads are allocating these objects. This allows you to see the list of objects 289 allocated during a period of interest. This information is valuable for assessing memory usage 290 that can affect application performance.</p> 291 292 <p>To track memory allocation of objects:</p> 293 294 <ol> 295 <li>Start your app as described in <a href="#runDebug">Run Your App in Debug Mode</a>.</li> 296 <li>Click <strong>Android</strong> <img src="{@docRoot}images/tools/as-android.png" alt="" 297 style="vertical-align:bottom;margin:0;height:20px"/> to open the <em>Android DDMS</em> 298 tool window.</li> 299 <li>On the <em>Android DDMS</em> tool window, select the <strong>Devices | logcat tab</strong>.</li> 300 <li>Select your device from the dropdown list.</li> 301 <li>Select your app by its package name from the list of running apps.</li> 302 <li>Click <strong>Start Allocation Tracking</strong> 303 <img src="{@docRoot}images/tools/as-allocstart.png" alt="" 304 style="vertical-align:bottom;margin:0;height:20px"/></li> 305 <li>Interact with your app on the device.</li> 306 <li>Click <strong>Stop Allocation Tracking</strong> 307 <img src="{@docRoot}images/tools/as-allocstop.png" alt="" 308 style="vertical-align:bottom;margin:0;height:20px"/></li> 309 </ol> 310 311 <p>Android Studio shows the objects that the system allocated with the following information:</p> 312 313 <ul> 314 <li>Allocation order</li> 315 <li>Allocated class</li> 316 <li>Allocation size</li> 317 <li>Thread ID</li> 318 <li>Allocation method, class, and line number</li> 319 <li>Stack trace at the point of allocation</li> 320 </ul> 321 322 <img src="{@docRoot}images/tools/as-alloctrack.png" alt="" width="750" height="252" /> 323 <p class="img-caption"><strong>Figure 8.</strong> Object allocation tracking in Android Studio.</p> 324 325 326 <h2 id="deviceMonitor">Analyze Runtime Metrics to Optimize your App</h2> 327 328 <p>Even if your application does not generate runtime errors, this does not mean it is free of 329 problems. You should also consider the following issues:</p> 330 331 <ul> 332 <li>Does your app use memory efficiently?</li> 333 <li>Does your app generate unnecessary network traffic?</li> 334 <li>What methods should you focus your attention on to improve the performance of your app?</li> 335 <li>Does your app behave properly when the user receives a phone call or a message?</li> 336 </ul> 337 338 <p>The Android Device Monitor is a stand-alone tool with a graphical user interface for serveral 339 Android application debugging and analysis tools, including the Dalvik Debug Monitor Server (DDMS). 340 You can use the Android Device Monitor to analyze memory usage, profile methods, 341 monitor network traffic and simulate incoming calls and messages.</p> 342 343 <p>To open the Android Device Monitor from Android Studio, click 344 <strong>Monitor</strong> <img src="{@docRoot}images/tools/as-monitorbutton.png" alt="" 345 style="vertical-align:bottom;margin:0;height:20px"/> on the toolbar. The Android Device Monitor 346 opens in a new window.</p> 347 348 <p>For more information about the Android Device Monitor and DDMS, see 349 <a href="{@docRoot}tools/help/monitor.html">Device Monitor</a> and 350 <a href="{@docRoot}tools/debugging/ddms.html">Using DDMS</a>.</p> 351 352 353 <h2 id="screenCap">Capture Screenshots and Videos</h2> 354 355 <p>Android Studio enables you to capture a screenshot or a short video of the device screen 356 while your app is running. Screenshots and videos are useful as promotional materials for your 357 app, and you can also attach them to bug reports that you send to your development team.</p> 358 359 <p>To take a screenshot of your app:</p> 360 361 <ol> 362 <li>Start your app as described in <a href="#runDebug">Run your App in Debug Mode</a>.</li> 363 <li>Click <strong>Android</strong> <img src="{@docRoot}images/tools/as-android.png" alt="" 364 style="vertical-align:bottom;margin:0;height:20px"/> to open the <em>Android DDMS</em> 365 tool window.</li> 366 <li>Click <strong>Screen Capture</strong> <img src="{@docRoot}images/tools/as-capture.png" 367 style="vertical-align:bottom;margin:0;height:22px" alt=""/> on the left side of the 368 <em>Android DDMS</em> tool window.</li> 369 <li>Optional: To add a device frame around your screenshot, enable the <em>Frame screenshot</em> 370 option.</li> 371 <li>Click <strong>Save</strong>.</li> 372 </ol> 373 374 <p>To take a video recording of your app:</p> 375 376 <ol> 377 <li>Start your app as described in <a href="#runDebug">Run your App in Debug Mode</a>.</li> 378 <li>Click <strong>Android</strong> <img src="{@docRoot}images/tools/as-android.png" alt="" 379 style="vertical-align:bottom;margin:0;height:20px"/> to open the <em>Android DDMS</em> 380 tool window.</li> 381 <li>Click <strong>Screen Record</strong> <img src="{@docRoot}images/tools/as-record.png" 382 style="vertical-align:bottom;margin:0;height:22px" alt=""/> on the left side of the 383 <em>Android DDMS</em> tool window.</li> 384 <li>Click <strong>Start Recording</strong>.</li> 385 <li>Interact with your app.</li> 386 <li>Click <strong>Stop Recording</strong>.</li> 387 <li>Enter a file name for the recording and click <strong>OK</strong>.</li> 388 </ol>