diff --git a/docs/html/images/tools/as-android.png b/docs/html/images/tools/as-android.png new file mode 100644 index 0000000000000..7808ed14418a0 Binary files /dev/null and b/docs/html/images/tools/as-android.png differ diff --git a/docs/html/images/tools/as-breakpointline.png b/docs/html/images/tools/as-breakpointline.png new file mode 100644 index 0000000000000..9aea880a54188 Binary files /dev/null and b/docs/html/images/tools/as-breakpointline.png differ diff --git a/docs/html/images/tools/as-breakpointswindow.png b/docs/html/images/tools/as-breakpointswindow.png new file mode 100644 index 0000000000000..a40b45987b96e Binary files /dev/null and b/docs/html/images/tools/as-breakpointswindow.png differ diff --git a/docs/html/images/tools/as-capture.png b/docs/html/images/tools/as-capture.png new file mode 100644 index 0000000000000..02f9f6f8b24a6 Binary files /dev/null and b/docs/html/images/tools/as-capture.png differ diff --git a/docs/html/images/tools/as-currentproc.png b/docs/html/images/tools/as-currentproc.png new file mode 100644 index 0000000000000..4be83052f4120 Binary files /dev/null and b/docs/html/images/tools/as-currentproc.png differ diff --git a/docs/html/images/tools/as-ddmslog.png b/docs/html/images/tools/as-ddmslog.png new file mode 100644 index 0000000000000..b53b8fe2f7405 Binary files /dev/null and b/docs/html/images/tools/as-ddmslog.png differ diff --git a/docs/html/images/tools/as-debugbutton.png b/docs/html/images/tools/as-debugbutton.png new file mode 100644 index 0000000000000..55e95d1b6668d Binary files /dev/null and b/docs/html/images/tools/as-debugbutton.png differ diff --git a/docs/html/images/tools/as-debugdevices.png b/docs/html/images/tools/as-debugdevices.png new file mode 100644 index 0000000000000..09a9d0e448cbb Binary files /dev/null and b/docs/html/images/tools/as-debugdevices.png differ diff --git a/docs/html/images/tools/as-debugview.png b/docs/html/images/tools/as-debugview.png new file mode 100644 index 0000000000000..0349147956ba2 Binary files /dev/null and b/docs/html/images/tools/as-debugview.png differ diff --git a/docs/html/images/tools/as-debugwindowbutton.png b/docs/html/images/tools/as-debugwindowbutton.png new file mode 100644 index 0000000000000..9016778b54d0c Binary files /dev/null and b/docs/html/images/tools/as-debugwindowbutton.png differ diff --git a/docs/html/images/tools/as-devicecapture.png b/docs/html/images/tools/as-devicecapture.png new file mode 100644 index 0000000000000..3236a892fef49 Binary files /dev/null and b/docs/html/images/tools/as-devicecapture.png differ diff --git a/docs/html/images/tools/as-evalexpbutton.png b/docs/html/images/tools/as-evalexpbutton.png new file mode 100644 index 0000000000000..85b3c74350dd3 Binary files /dev/null and b/docs/html/images/tools/as-evalexpbutton.png differ diff --git a/docs/html/images/tools/as-launchavdm.png b/docs/html/images/tools/as-launchavdm.png new file mode 100644 index 0000000000000..bf159810adf52 Binary files /dev/null and b/docs/html/images/tools/as-launchavdm.png differ diff --git a/docs/html/images/tools/as-monitorbutton.png b/docs/html/images/tools/as-monitorbutton.png new file mode 100644 index 0000000000000..6bdc3a54af4e5 Binary files /dev/null and b/docs/html/images/tools/as-monitorbutton.png differ diff --git a/docs/html/images/tools/as-record.png b/docs/html/images/tools/as-record.png new file mode 100644 index 0000000000000..5f7fa99b4a366 Binary files /dev/null and b/docs/html/images/tools/as-record.png differ diff --git a/docs/html/images/tools/as-restart.png b/docs/html/images/tools/as-restart.png new file mode 100644 index 0000000000000..12d2923708e7f Binary files /dev/null and b/docs/html/images/tools/as-restart.png differ diff --git a/docs/html/images/tools/as-resumeprogrambutton.png b/docs/html/images/tools/as-resumeprogrambutton.png new file mode 100644 index 0000000000000..8096937d86d2d Binary files /dev/null and b/docs/html/images/tools/as-resumeprogrambutton.png differ diff --git a/docs/html/images/tools/as-showdevview.png b/docs/html/images/tools/as-showdevview.png new file mode 100644 index 0000000000000..602a6adee1476 Binary files /dev/null and b/docs/html/images/tools/as-showdevview.png differ diff --git a/docs/html/images/tools/as-stepintobutton.png b/docs/html/images/tools/as-stepintobutton.png new file mode 100644 index 0000000000000..569d4ed242e90 Binary files /dev/null and b/docs/html/images/tools/as-stepintobutton.png differ diff --git a/docs/html/images/tools/as-stepoutbutton.png b/docs/html/images/tools/as-stepoutbutton.png new file mode 100644 index 0000000000000..ef8871fd839c3 Binary files /dev/null and b/docs/html/images/tools/as-stepoutbutton.png differ diff --git a/docs/html/images/tools/as-stepoverbutton.png b/docs/html/images/tools/as-stepoverbutton.png new file mode 100644 index 0000000000000..1c487df3e4bf7 Binary files /dev/null and b/docs/html/images/tools/as-stepoverbutton.png differ diff --git a/docs/html/images/tools/as-variablesview.png b/docs/html/images/tools/as-variablesview.png new file mode 100644 index 0000000000000..6a0b987b4fd99 Binary files /dev/null and b/docs/html/images/tools/as-variablesview.png differ diff --git a/docs/html/images/tools/as-varviewbutton.png b/docs/html/images/tools/as-varviewbutton.png new file mode 100644 index 0000000000000..2ad4c585219a6 Binary files /dev/null and b/docs/html/images/tools/as-varviewbutton.png differ diff --git a/docs/html/images/tools/as-viewbreakbutton.png b/docs/html/images/tools/as-viewbreakbutton.png new file mode 100644 index 0000000000000..22723d4f6d4d2 Binary files /dev/null and b/docs/html/images/tools/as-viewbreakbutton.png differ diff --git a/docs/html/sdk/installing/studio-debug.jd b/docs/html/sdk/installing/studio-debug.jd new file mode 100644 index 0000000000000..7e2efe3673640 --- /dev/null +++ b/docs/html/sdk/installing/studio-debug.jd @@ -0,0 +1,346 @@ +page.title=Debugging with Android Studio + +@jd:body + +
+
+

In this document

+
    +
  1. Run your App in Debug Mode
  2. +
  3. Use the System Log +
      +
    1. Write log messages in your code
    2. +
    3. View the system log
    4. +
    +
  4. +
  5. Work with Breakpoints +
      +
    1. View and configure breakpoints
    2. +
    3. Debug your app with breakpoints
    4. +
    +
  6. +
  7. Analyze Runtime Metrics to Optimize your App
  8. +
  9. Capture Screenshots and Videos
  10. +
+

See also

+
+
+ +

Android Studio enables you to debug apps running on the emulator or on an Android device. +With Android Studio, you can:

+ + + +

To debug your app, Android Studio builds a debuggable version of your app, connects +to a device or to the emulator, installs the app and runs it. The IDE shows the system log +while your app is running and provides debugging tools to filter log messages, work with +breakpoints, and control the execution flow.

+ + +

Run your App in Debug Mode

+ +
+ +

Figure 1. The Choose Device window enables you to + select a physical Android device or a virtual device to debug your app.

+
+ +

To 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 set up an Android device for development, see Using +Hardware Devices. For more information about the emulator provided by the Android SDK, see +Using the Emulator.

+ +

To debug your app in Android Studio:

+ +
    +
  1. Open your project in Android Studio.
  2. +
  3. Click Debug in the toolbar.
  4. +
  5. On the Choose Device window, select a hardware device from the list or + choose a virtual device.
  6. +
  7. Click OK. Your app starts on the selected device.
  8. +
+ +

Figure 1 shows the Choose Device window. The list shows all the Android devices +connected to your computer. Select Launch Emulator to use an Android virtual device +instead. Click the ellipsis to open the +Android Virtual Device Manager.

+ +

Android 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. The Debug tool +window also provides other debugging tools covered in the following sections.

+ + +

Figure 2. The Debug tool window in Android Studio showing +the current thread and the object tree for a variable.

+ + +

Use the System Log

+ +

The 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.

+ +

Write log messages in your code

+ +

To write log messages in your code, use the {@link android.util.Log} class. Log messages +help you understand the execution flow by collecting the system debug output while you interact +with your app. Log messages can tell you what part of your application failed. For more +information about logging, see +Reading and Writing Logs.

+ +

The following example shows how you might add log messages to determine if previous state +information is available when your activity starts:

+ +
+import android.util.Log;
+...
+public class MyActivity extends Activity {
+    private static final String TAG = MyActivity.class.getSimpleName();
+    ...
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        if (savedInstanceState != null) {
+            Log.d(TAG, "onCreate() Restoring previous state");
+            /* restore state */
+        } else {
+            Log.d(TAG, "onCreate() No saved state available");
+            /* initialize app */
+        }
+    }
+}
+
+ +

During development, your code can also catch exceptions and write the stack trace to the system +log:

+ +
+void someOtherMethod() {
+    try {
+        ...
+    } catch (SomeException e) {
+        Log.d(TAG, "someOtherMethod()", e);
+    }
+}
+
+ +

Note: Remove debug log messages and stack trace print calls from +your code when you are ready to publish your app. You could do this by setting a DEBUG +flag and placing debug log messages inside conditional statements.

+ + +

View the system log

+ +

Both the Android DDMS (Dalvik Debug Monitor Server) and the Debug tool windows +show the system log; however, the Android DDMS tool window lets you view only log messages +for a particular process. To view the system log on the Android DDMS tool window:

+ +
    +
  1. Start your app as described in Run your App in Debug Mode.
  2. +
  3. Click Android to open the Android DDMS + tool window.
  4. +
  5. If the system log is empty in the Logcat view, click Restart + .
  6. +
+ + +

Figure 4. The system log in the Android DDMS tool +window.

+ +

The Android DDMS tool window gives you access to some DDMS features from Android Studio. +For more information about DDMS, see Using DDMS. +

+ +

The 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:

+ + + + +

Work with Breakpoints

+ +

Breakpoints 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. +
  3. Locate the line where you want to set a breakpoint and click on it.
  4. +
  5. Click on the yellow portion of the side bar to the left of this line, as shown in figure 5.
  6. +
  7. Start your app as described in Run your App in Debug Mode.
  8. +
+ +

Android Studio pauses the execution of your app when it reaches the breakpoint. You can then +use the tools in the Debug tool window to identify the cause of the error.

+ + +

Figure 5. A red dot appears next to the line when you set +a breakpoint.

+ +

View 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, as shown in figure 6.

+ + +

Figure 6. The Breakpoints window lists all the current +breakpoints and includes behavior settings for each.

+ +

The Breakpoints window lets you enable or disable each breakpoint from the +list on the left. If a breakpoint is disabled, Android Studio does not pause your app when +it hits that breakpoint. Select a breakpoint from the list to configure its settings. +You can configure a breakpoint to be disabled at first and have the system enable it after a +different breakpoint is hit. You can also configure whether a breakpoint should be disabled after +it is hit. To set a breakpoint for any exception, select Exception Breakpoints +in the list of breakpoints.

+ +

Debug your app with breakpoints

+ +

After 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. The +Debug tool window lets you examine variables and control the execution step by +step:

+ + + + +

Figure 7. The Variables view in the Debug tool window.

+ + +

Analyze Runtime Metrics to Optimize your App

+ +

Even if your application does not generate runtime errors, this does not mean it is free of +problems. You should also consider the following issues:

+ + + +

The Android Device Monitor is a stand-alone tool with a graphical user interface for serveral +Android application debugging and analysis tools, including the Dalvik Debug Monitor Server (DDMS). +You can use the Android Device Monitor to analyze memory usage, profile methods, +monitor network traffic and simulate incoming calls and messages.

+ +

To open the Android Device Monitor from Android Studio, click +Monitor on the toolbar. The Android Device Monitor +opens in a new window.

+ +

For more information about the Android Device Monitor and DDMS, see +Device Monitor and +Using DDMS.

+ + +

Capture Screenshots and Videos

+ +

Android 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 as described in Run your App in Debug Mode.
  2. +
  3. Click Android to open the Android DDMS + tool window.
  4. +
  5. Click Screen Capture on the left side of the + Android DDMS tool window.
  6. +
  7. Optional: To add a device frame around your screenshot, enable the Frame screenshot + option.
  8. +
  9. Click Save.
  10. +
+ +

To take a video recording of your app:

+ +
    +
  1. Start your app as described in Run your App in Debug Mode.
  2. +
  3. Click Android to open the Android DDMS + tool window.
  4. +
  5. Click Screen Record on the left side of the + Android DDMS tool window.
  6. +
  7. Click Start Recording.
  8. +
  9. Interact with your app.
  10. +
  11. Click Stop Recording.
  12. +
  13. Enter a file name for the recording and click OK.
  14. +
\ No newline at end of file diff --git a/docs/html/tools/tools_toc.cs b/docs/html/tools/tools_toc.cs index c2816446da8df..b29b87cc43d85 100644 --- a/docs/html/tools/tools_toc.cs +++ b/docs/html/tools/tools_toc.cs @@ -38,6 +38,8 @@ Using the Layout Editor
  • Building Your Project with Gradle
  • +
  • + Debugging with Android Studio