android studio调试技巧

    科技2022-07-13  138

    Debugging is the key process of finding and fixing bugs or unexpected behavior in your code. All code has errors, from incorrect behavior in your app to behavior that highly consumes memory or network resources, to actual app freezing or crashing.

    调试是发现并修复代码中的错误或意外行为的关键过程。 所有代码都有错误,从应用程序中的错误行为到大量消耗内存或网络资源的行为,到实际的应用程序冻结或崩溃。

    Debugging is one of the crucial parts of a software developer’s everyday life. We always attempt to write bug-free applications but to do this, we have to fix every bug, which is sometimes hard to spot in the code. Debugging allows you to go through each line of code, evaluating your app’s variables, methods and how well your code is working. It’s easier to find small mistakes in large pieces of code. In this article, we will go through important tips and tricks on debugging an Android app.

    调试是软件开发人员日常生活的关键部分之一。 我们总是尝试编写无错误的应用程序,但是要做到这一点,我们必须修复每个错误,有时很难在代码中发现它们。 通过调试,您可以遍历代码的每一行,评估应用程序的变量,方法以及代码的运行状况。 在大型代码中发现小错误更容易。 在本文中,我们将介绍调试Android应用程序的重要提示和技巧。

    Log filtering/folding

    日志过滤/折叠

    Conditional breakpoints

    条件断点

    Dependent breakpoints

    相关断点

    Suspend thread

    挂起线程

    Evaluate and log

    评估并记录

    Breakpoint groups

    断点组

    Drop frame

    落架

    Evaluate expression

    评估表达

    Apply changes

    应用更改

    Analyze stack trace

    分析堆栈跟踪

    Log filtering / folding

    日志过滤/折叠

    If we open up Logcat in Android Studio, we are seeing these things on that Logcat surfaces.

    如果我们在Android Studio中打开Logcat ,则会在Logcat表面上看到这些东西。

    Show date and time with seconds: 1517955388.555 123–456, process and thread IDs: SampleTag, package name: com.android.sample and show tag: This is a sample message

    显示日期和时间与秒:1517955388.555 123-456,进程和线程ID:SampleTag,包名:com.android.sample和显示标签:这是一个示例消息

    In most cases, we don’t need date and time, package name and thread IDs. So to do that we can uncheck these options and we can view what are necessary.

    在大多数情况下,我们不需要日期和时间,程序包名称和线程ID。 因此,我们可以取消选中这些选项,然后查看所需的内容。

    To add Custom filter we can set through Filter Drop-Down ->Edit Filter Configuration->Create New Logcat Filter

    要添加自定义过滤器,我们可以通过过滤器下拉菜单->编辑过滤器配置->创建新的Logcat过滤器进行设置

    We can set any filter message on the Filter Name section and we can find easily on Logcat.

    我们可以在“ 过滤器名称”部分设置任何过滤器消息,并且可以在Logcat上轻松找到。

    Folding is managing the same kind of line of the message as doing a folding trick: Choose any message from Logcat and then right-click on that message ->Fold Line Like This->Double Click On Choose message (Can change message)->Then Ok.

    折叠管理与折叠技巧相同的消息行:从Logcat中选择任何一条消息 ,然后右键单击该消息->像这样折叠行->双击选择消息 (可以更改消息) ->那好吧

    Now we can fold and open the message of the same lines.

    现在,我们可以折叠并打开相同行的消息。

    Conditional breakpoints

    条件断点

    Add a breakpoint, as you normally do, and click with the right mouse button on it. You will be prompt with a dialog. In the condition, you can put some kotlin/java code.

    像通常一样添加一个断点,然后在其上单击鼠标右键。 系统将提示您一个对话框。 在这种情况下,您可以放置​​一些kotlin / java代码。

    The debugger can also stop execution on breakpoints only when some conditions are met. We can apply expressions and hit count conditions to any breakpoint and remove them without resetting the breakpoint. We’ll revisit the Breakpoints Window to add expressions and hit count conditionals.

    仅当满足某些条件时,调试器也可以停止在断点上执行。 我们可以将表达式和命中条件应用于任何断点,并在不重置断点的情况下将其删除。 我们将重新访问“断点窗口”以添加表达式和命中计数条件。

    Dependent breakpoints

    相关断点

    Dependent Breakpoints(Not a Conditional Bp) ( Breakpoint1 is enabled if B2 is enabled etc..)

    依赖断点(不是有条件的Bp)(如果启用了B2等,则启用Breakpoint1。)

    For this purpose, the debugger allows you to create dependent breakpoints.

    为此,调试器允许您创建相关的断点。

    要设置一个断点,当前的断点必须取决于 (To set a breakpoint the current one must depend on)

    Do one of the following:

    请执行以下任一操作:

    Right-click the corresponding breakpoint icon in the left gutter and click More in the opened breakpoint properties.

    右键单击左装订线中的相应断点图标,然后在打开的断点属性中单击“ 更多 ”。

    In the Breakpoints dialog (Ctrl+Alt+B or Run | View Breakpoints… ), select the desired breakpoint. Note that in this dialog, you can also set a dependant breakpoint.

    在“ 断点”对话框(Ctrl + Alt + B或“运行” |“查看断点...” )中,选择所需的断点。 请注意,在此对话框中,您还可以设置相关的断点。

    2. In Disabled until the selected breakpoint is hit, select a breakpoint the current one must depend on.

    2.在“ 已禁用”中,直到命中选定的断点为止 ,选择当前一个必须依赖的断点。

    3. After breakpoint was hit, select

    3.命中断点后,选择

    Disable again to disable the current breakpoint after the selected breakpoint is hit.

    选中所选断点后, 再次禁用以禁用当前断点。

    Leave enabled to keep the current breakpoint enabled after the selected breakpoint is hit.

    保持启用状态以在选中所选断点后保持当前断点处于启用状态。

    Suspend thread

    挂起线程

    If we are trying to debug multithread apps in Android Studio, we could have troubles with some of our breakpoints, so it’s better to enable an Android Studio option to pause all threads by default setting.

    如果我们尝试在Android Studio中调试多线程应用程序,则可能会遇到一些断点的问题,因此最好启用Android Studio选项以默认设置暂停所有线程。

    When the currently executing thread encounters the breakpoint, it will just stop every thread in the application.

    当当前执行的线程遇到断点时,它将停止应用程序中的每个线程。

    Evaluate and log

    评估并记录

    “Evaluate and Log” multiple variables or make a string expression at a breakpoint. In this case, it will not stop app execution further and will get debug strings.

    “评估并记录”多个变量或在断点处生成字符串表达式。 在这种情况下,它不会进一步停止应用程序执行,并且会获得调试字符串。

    Just unselect the Suspend checkbox -> select Evaluate and log checkbox and add any kotlin expression. Then it will evaluate the expression and log into the log section without suspending the thread.

    只需取消选中Suspend复选框->选择Evaluate and log复选框并添加任何kotlin表达式即可。 然后它将评估表达式并登录到log部分,而不会暂停线程。

    Breakpoint groups

    断点组

    If we are working on multiple bugs, sometimes we need to move on to another bug without completing the currently working bug. In this condition, most of the developers disable the previously added debugger. So, breakpoint groups are the feature to save breakpoints for future purposes.

    如果我们正在处理多个错误,有时我们需要移至另一个错误而不完成当前正在运行的错误。 在这种情况下,大多数开发人员都会禁用以前添加的调试器。 因此,断点组是保存断点以供将来使用的功能。

    Right-click on the breakpoint -> click on the More -> select multiple breakpoints we want to group -> Right-click -> Create new -> give group name

    右键单击断点 ->单击更多 ->选择要分组的多个断点 -> 右键单击 -> 创建新的 ->输入组名

    Then we can enable/disable them in a single click.

    然后,我们可以单击启用/禁用它们。

    Drop frame

    落架

    We can drop the frames until we reach the point where we want to be. This isn’t exactly stepping backward, but it is quite similar in a lot of respects.

    我们可以放下帧,直到到达想要的位置。 这并不是完全退后一步,但在很多方面都非常相似。

    For example, we step over a method called a() expecting to get 2 but after re-checking the value returned we notice it's 5 instead of 2. How can we step back to where we were before and step into the method to see what is going on? We know we can do this in Android Studio if targeting devices running with at least Android10.

    例如,我们跨步了一个预期会得到2名为a()的方法,但是在重新检查返回的值之后,我们注意到它是5而不是2 。 我们如何才能回到以前的状态,然后进入方法以查看发生了什么? 我们知道,如果定位至少运行Android10的设备,我们可以在Android Studio中执行此操作。

    Click on the Drop frame button: It will pull you out from the current method and put you right forward started and if you step into it again you will get a second chance.

    单击“ 放置帧”按钮:它会将您从当前方法中拉出,并使您立即开始,如果再次进入它,您将获得第二次机会。

    Evaluate expression

    评估表达

    When debugging an application, we can set a breakpoint and when that triggers, we can right-click in the editor and select ‘Evaluate Expression’ or just Evaluate Expression(Alt+F8). In this window, we can check any statement and data.

    在调试应用程序时,我们可以设置一个断点,然后触发该断点,我们可以在编辑器中单击鼠标右键,然后选择“评估表达式”或仅评估表达式(Alt + F8)。 在此窗口中,我们可以检查任何语句和数据。

    Apply changes

    应用更改

    Apply changes tools in Android Studio are to provide that allows us to quickly edit and validate code changes in our app. Without reinstalling the app we can change our block of code and meantime we can debug as well.

    Android Studio中提供的“应用更改工具”可让我们快速编辑和验证应用中的代码更改。 无需重新安装应用程序,我们就可以更改代码块,同时我们也可以调试。

    This feature is available for devices and emulators running on Android 8.0 or newer, Android Studio now features three buttons that control how much of the application Run, Apply Changes and Apply Code Changes.

    此功能适用于在Android 8.0或更高版本上运行的设备和仿真器,Android Studio现在具有三个按钮,用于控制应用程序运行 , 应用更改和应用代码更改的数量。

    To achieve this feature:

    要实现此功能:

    Add your debugger in any code of line

    在任何代码行中添加调试器 Change/Add any code of line or conditions

    更改/添加任何行代码或条件代码 Attach a debugger into the new code of line

    将调试器附加到该行的新代码中

    Click on Apply Code Changes

    单击“ 应用代码更改”

    After applying code changes again click on Drop frame

    再次应用代码更改后,单击“ 放置框架”

    And at last hit step in button and your debugger will be in the changes code of line

    而在去年按钮命中一步 ,你的调试器会在线路的变化码

    Analyze stack trace

    分析堆栈跟踪

    Finally, despite all of these tips and tricks, we are still going to write some codes, debug and will get crashes or bug reports as well.

    最后,尽管有所有这些技巧,我们仍将编写一些代码,进行调试,并且还会收到崩溃或错误报告。

    So, to track stack trace text:

    因此,要跟踪堆栈跟踪文本:

    Go to Analyze -> Analyze Stack Trace or Thread Dump… ->Put stack trace or complete thread dump into the box(It will find value on the clipboard) -> Ok

    转到分析 -> 分析堆栈跟踪或线程转储...- >将堆栈跟踪或完整的线程转储到框中(它将在剪贴板上找到值)-> 确定

    It will quickly jump into the codebase.

    它将Swift跳入代码库。

    Enjoy!! Happy Debugging 🙂

    请享用!! 调试愉快🙂

    翻译自: https://medium.com/@noowenz/android-studio-debugging-tips-n-tricks-85a7ab293d2f

    Processed: 0.010, SQL: 8