Android Studio使你可以调试运行在模拟器或Android设备上的应用。通过Android Studio,你可以:

选择调试应用的设备

查看系统日志

在代码中设置断点

在运行中检查变量和对表达式求值

运行Android SDK中的调试工具

捕获应用屏幕快照和视频

为了调试应用,Android Studio构建一个应用的调试版本,连接到设备或模拟器,安装应用并运行。IDE在你的应用运行时显示系统日志,并且提供调试工具去过滤日志信息,使用断点,控制执行流程。

Run your App in Debug Mode

以调试模式运行应用,构建一个debug key签名的APK并且安装到物理Android设备或Android模拟器。

在Android Studio调试应用的步骤:

在Android Studio打开项目

在工具栏点击Debug

新版android studio运行不出现debug目录 android studio debug apk_系统日志

在Choose Device视图,从列表中选择一个物理设备或一个虚拟设备

点击OK,应用就会在选择的设备上运行

新版android studio运行不出现debug目录 android studio debug apk_系统日志

注:Choose Device窗口使你可以选择一个物理Android设备或一个虚拟设备来调试你的应用

当你调试应用时,Android Studio打开Debug工具视图。如果需要手动打开Debug工具视图,点击Debug

新版android studio运行不出现debug目录 android studio debug apk_系统日志

。这个视图在Debugger标签页显示线程和变量,在Console标签页中显示设备状态,在Logcat标签页显示系统日志。Debug工具视图同时提供其他的调试工具,在下面的部分介绍。

新版android studio运行不出现debug目录 android studio debug apk_系统日志

注:Android Studio中的调试工具视图显示了当前的线程和变量的对象树

Attach the debugger to a running process

你不需要总是因为调试而重启你的应用,调试一个已经运行的应用步骤如下:

点击Attach debugger to Android Process

新版android studio运行不出现debug目录 android studio debug apk_系统日志

在Choose Process视图中,选择设备以及你想绑定到调试器的应用

点击Debug

新版android studio运行不出现debug目录 android studio debug apk_系统日志

,打开Debug工具视图

Use the System Log

系统日志在你调试应用时显示系统消息。这些消息包括运行在设备上的应用信息。如果你想使用系统日志调试你的应用,确保你的应用在开发阶段代码中写了日志信息以及打印了异常的堆栈跟踪。

Write log messages in your code

使用Log类在代码中写日志消息。日志消息通过在你与应用交互时收集系统调试输出信息来帮助你理解执行流程。日志消息可以告诉你应用的哪部分失败了。

下面的例子展示了你可能在activity启动时添加日志消息来检查是否之前的状态信息可用:

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 */
}
}
}

在开发阶段,你的代码同样可以捕捉异常并将堆栈跟踪信息写到系统日志中:

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

Note:当你准备发布你的应用时,从你的代码中移除调试日志消息和堆栈跟踪打印。你可以通过设置一个DEBUG标记并将调试日志消息放置到条件语句中来做这件事情。

View the system log

Android DDMS和Debug工具视图都可以显示系统日志。但是Android DDMS工具视图可以让你只查看一个特定进程的日志消息。在Android DDMS工具视图查看系统日志的步骤如下:

以调试模式启动应用

点击Android

新版android studio运行不出现debug目录 android studio debug apk_系统日志

来打开Android DDMS工具视图

如果Logcat视图中的系统日志为空,点击Restart

新版android studio运行不出现debug目录 android studio debug apk_系统日志

新版android studio运行不出现debug目录 android studio debug apk_系统日志

注:在Android DDMS工具视图中的系统日志

Android DDMS工具视图让你在Android Studio使用一些DDMS特性。

系统日志显示了Android Services和其他Android应用的消息。使用Android DDMS视图中的工具,过滤日志消息从而只显示你所感兴趣的:

要显示指定进程的日志消息,在Devices视图选择进程然后点击Only Show Logcat from Selected Process

新版android studio运行不出现debug目录 android studio debug apk_系统日志

。如果Device视图不可用,点击Android DDMS工具视图的右侧的Restore Devices View

新版android studio运行不出现debug目录 android studio debug apk_系统日志

。这个按钮只会在你隐藏Devices视图时显示。

要通过日志等级过滤日志消息,在Android DDMS视图的顶部的Log Level选择一个等级。

要只显示包含特定字符串的日志时,在搜索框输入字符串然后按Enter。

Work with Breakpoints

断点可以让你的应用在特定的一行代码处暂停执行,检查变量,对表达式求值,以及一行一行地继续执行。使用断点检查那些不能只通过查看代码解决的运行时错误的造成原因。使用断点调试应用的步骤:

打开你想设置断点的源文件

定位到设置断点的行并点击

点击这行左边边栏的黄色部分,如下图

以调试模式启动你的应用

新版android studio运行不出现debug目录 android studio debug apk_系统日志

注:当你设置一个断点,红点出现在行的旁边

Android Studio会在执行到断点处暂停执行,之后你可以使用Debug工具视图的工具来确定错误原因。

View and configure breakpoints

要查看所有断点并配置,点击Debug工具视图左边的View Breakpoints

新版android studio运行不出现debug目录 android studio debug apk_系统日志

,之后会弹出Breakpoints视图,如下图所示:

新版android studio运行不出现debug目录 android studio debug apk_系统日志

注:Breakpoints视图列出了当前所有的断点以及每个的行为设置

Breakpoints视图让你可以在左边的列表启用或禁用每一个断点。如果一个断点被禁用,Android Studio就不会在应用命中这个断点时暂停它。选择列表中的一个断点去设置它。你可以配置这个断点使其在第一次禁用,而当另外一个断点命中时,再让系统启用它。你同样可以配置一个断点是否在命中后禁用。要设置异常的断点,在断点的列表中选择Exception Breakpoints。

Debug your app with breakpoints

当你在代码中设置断点后,点击Rerun

新版android studio运行不出现debug目录 android studio debug apk_系统日志

重新启动应用。当一个断点命中,Android Studio暂停应用,并且在源代码中高亮断点。Debug工具视图让你可以检查变量以及一步步地控制执行:

要检查变量的对象树,在Variables视图中展开。如果Variables视图不可见,点击Restore Variables View

新版android studio运行不出现debug目录 android studio debug apk_系统日志

在当前执行点对表达式求值,点击Evaluate Expression

新版android studio运行不出现debug目录 android studio debug apk_系统日志

要到达代码的下一行(不进入方法),点击Step Over

新版android studio运行不出现debug目录 android studio debug apk_系统日志

要到达调用方法内部的第一行,点击Step Into

新版android studio运行不出现debug目录 android studio debug apk_系统日志

要到达当前方法外部的下一行,点击Step Out

新版android studio运行不出现debug目录 android studio debug apk_系统日志

要继续正常地执行应用,点击Resume Program

新版android studio运行不出现debug目录 android studio debug apk_系统日志

新版android studio运行不出现debug目录 android studio debug apk_系统日志

注:Debug工具视图中的Variables视图

Track Object Allocation

Android Studio可以让你跟踪Java堆分配的对象以及哪个类和线程分配的这些对象。这允许你查看某个感兴趣时期分配的对象列表。这个信息对于分析能够影响应用性能的内存使用情况非常有用。

跟踪对象内存分配的步骤:

以调试模式启动引用

点击Android

新版android studio运行不出现debug目录 android studio debug apk_系统日志

打开Android DDMS工具视图

在Android DDMS工具视图,选择Devices|logcat标签页

在下拉列表选择你的设备

在运行着的应用列表通过包名选择你的应用

点击Start Allocation Tracking

新版android studio运行不出现debug目录 android studio debug apk_系统日志

在设备上与应用进行交互

点击Stop Allocation Tracking

新版android studio运行不出现debug目录 android studio debug apk_系统日志

Android Studio提供如下信息显示系统分配的内存

分配顺序

分配类

分配大小

线程id

分配方法、类和代码行数

分配点的堆栈跟踪

新版android studio运行不出现debug目录 android studio debug apk_系统日志

注:在Android Studio的对象分配跟踪

Analyze Runtime Metrics to Optimize your App

即使你的应用程序不会产生运行时错误,但是这并不意味着没有任何问题。你应该考虑以下问题:

你的应用高效使用内存了么?

你的应用产生了不必要的网络流量了么?

你需要将注意力集中到什么方法来提升app的性能?

当用户接到一个电话或短信时,你的应用是否表现正常?

Android Device Monitor是一个提供用户界面的独立工具,有一些Android应用调试和分析工具,包含DDMS。你可以使用Android Device Monitor来分析内存使用情况,剖析方法,监控网络流量以及模拟来电和短信。

要在Android Studio打开Android Device Monitor,点击工具栏中的Monitor

新版android studio运行不出现debug目录 android studio debug apk_系统日志

,Android Device Monitor会在一个新的窗口打开。

Capture Screenshots and Videos

Android Studio让你可以在应用运行时捕获设备屏幕的屏幕快照或者短视频。屏幕快照和视频是对应用非常有用的附加资料,你可以添加到发送给开发团队的错误报告中。

获取应用屏幕快照的步骤:

以调试模式启动应用

点击Android

新版android studio运行不出现debug目录 android studio debug apk_系统日志

打开Android DDMS工具视图

点击Android DDMS工具视图左侧的Screen Capture

新版android studio运行不出现debug目录 android studio debug apk_系统日志

你可以启动Frame screenshot选项,来给屏幕快照添加一个设备框

点击Save

获取应用录像的步骤:

以调试模式启动应用

点击Android

新版android studio运行不出现debug目录 android studio debug apk_系统日志

打开Android DDMS工具视图

点击Android DDMS工具视图左侧的*Screen Record

新版android studio运行不出现debug目录 android studio debug apk_系统日志

点击Start Recording

与应用进行交互

点击Stop Recording

输入录像的文件名,然后点击OK

本博客会持续对Debug系列的以下文章进行翻译,后面追加链接的为已翻译的:

Debugging with Android Studio——在Android Studio中调试

http://www.voidcn.com/article/p-otfgqghs-mx.html

Profiling with Traceview and dmtracedump

Improving Code Inspection with Annotations

Analyzing Display and Performance

Investigating Your RAM Usage

Using the Dev Tools App