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