Every Android developer begins their journey by enabling the developer mode on a device. Without this option, you cannot install your application on your phone.
每个Android开发人员都通过在设备上启用开发人员模式来开始他们的旅程。 没有此选项,您将无法在手机上安装应用程序。
Although this menu can unlock your device for deployment, it also contains many debugging tools. Some day, one of them may pull you out of a dire situation.
尽管此菜单可以解锁您的设备以进行部署,但它还包含许多调试工具。 有一天,其中一个可能使您摆脱困境。
So before diving into the code, let’s take a moment and peek at the available tools inside the developer options. I won’t present them all. Instead, I will focus on four that have helped me a lot over the years.
因此,在深入研究代码之前,让我们花点时间看一下开发人员选项中的可用工具。 我不会全部介绍。 取而代之的是,我将专注于在过去几年中对我有很大帮助的四个方面。
When building a layout, you have countless possibilities to declare your view’s structure. Don’t expect your teammates to code it the same way you would.
构建布局时,您可以无数种方式声明视图的结构。 不要指望您的队友以相同的方式编写代码。
However, you must keep in mind how your view performs. We all enjoy smooth applications, and view rendering depends on how efficient your structure is.
但是,您必须记住视图的执行方式。 我们所有人都喜欢流畅的应用程序,并且视图渲染取决于您的结构的效率。
During my first few years as an Android developer, I didn’t know how to optimize my layouts. One day, one of my customers wasn’t thrilled about how laggy his application was. I had to investigate the source of the problem.
在担任Android开发人员的最初几年中,我不知道如何优化布局。 有一天,我的一位客户对他的应用程序有多慢并不感到兴奋。 我不得不调查问题的根源。
And then I came across this article.
然后我遇到了这篇文章 。
It helped me to optimize my view rendering drastically. By removing overdraws and flattening my layout hierarchies, my application became way smoother.
它帮助我极大地优化了视图渲染。 通过消除透支图和展平布局层次结构,我的应用程序变得更加平滑。
Without further ado, go to your developer options. Inside “Debug GPU overdraw,” select “Show overdraw areas.” Open your application and inspect all the reddish areas.
我必须承认,这些天我不经常使用此功能。 自从引入以来,构建平面视图从未如此简单。
I still happen to toggle this option on occasion. It could help you identify:
我仍然碰巧偶尔会切换此选项。 它可以帮助您识别:
- Overly complex views, no matter how flat subviews are. 无论子视图多么平坦,视图都过于复杂。
- Unnecessary background overdraw. 不必要的背景透支。
When your application is doing too much work on the main thread, the system will try to compensate by skipping some frames. Your user will end up with a hanging interface, which can only lead to frustration.
当您的应用程序在主线程上执行过多工作时,系统将尝试通过跳过一些帧来进行补偿。 您的用户最终将获得一个挂起的界面,这只会导致沮丧。
In the worst-case scenario, your application may not respond during a certain amount of time. The system then presents an Application Not Responsive (ANR) dialogue on the user’s screen. You can find more information in this article.
在最坏的情况下,您的应用程序可能在一定时间内没有响应。 然后,系统在用户屏幕上显示“应用程序无响应”(ANR)对话框。 您可以在本文中找到更多信息。
Preventing ANR seems obvious, yet it’s often overlooked. When you’re developing an Android application, you tend to focus on crashes for two reasons:
防止ANR似乎很明显,但是却经常被忽略。 在开发Android应用程序时,您倾向于关注崩溃的原因有两个:
- They symbolize the worst user experience. 它们象征着最糟糕的用户体验。
There exist several tools to track them, such as Firebase Crashlytics.
有几种工具可以跟踪它们,例如Firebase Crashlytics 。
The absence of metrics for detecting ANR doesn’t help either. They’re also harder to debug, as optimization rather than a code fix is required to prevent a crash.
缺乏用于检测ANR的指标也无济于事。 它们也更难以调试,因为需要优化而不是代码修复来防止崩溃。
With that said, you should know that there is a tool inside the developer options to track them. It’s called the StrictMode.
如此说来,您应该知道开发人员选项中有一个工具可以跟踪它们。 它称为StrictMode。
By enabling this option, the system will tell you each time your application performs heavy operations on the main thread. You can detect thread and VM policies. When you violate one of them, you’ll get red borders around your screen flashing at you.
通过启用此选项,系统将在您的应用程序每次在主线程上执行繁重的操作时告诉您。 您可以检测线程和VM策略。 违反其中一项时,屏幕周围会出现红色边框,向您闪烁。
Both this cue and logs in the console can help you pinpoint what you’re doing wrong. To go deeper into this topic, I recommend having a look at this article:
此提示和控制台中的日志都可以帮助您查明您在做什么错。 为了更深入地讨论这个主题,建议您看一下这篇文章:
You could combine StrictMode with Profile HWUI Rendering. This option displays bars with a threshold you should not exceed. Each time you do, frames are being skipped.
您可以将StrictMode与Profile HWUI Rendering结合使用。 此选项显示的阈值不应超过阈值。 每次这样做,帧都会被跳过。
I like to say we can sort applications out from the way they handle memory pressure. We develop for an environment with a limited amount of memory, so we should expect the system to shut down any part of our application. You must anticipate how your components will behave when the system recreates them.
我想说的是,我们可以按照处理内存压力的方式来整理应用程序。 我们是针对内存量有限的环境开发的,因此我们应该期望系统关闭应用程序的任何部分。 您必须预料系统重新创建组件时的行为。
Because we have no control over the system, you’ll find this hard to simulate. Fortunately, we have a convenient tool called “Don’t keep activity.”
由于我们无法控制系统,因此您将很难进行模拟。 幸运的是,我们有一个方便的工具,叫做“不要保持活动”。
With this option enabled, go from background to foreground and discover how your activity behaves when recreated.
启用此选项后,从后台转到前台并发现您的活动在重新创建时的行为。
Not only will you detect improper behaviors, but this option could help you debug crashes as well. I cannot count how many times this tool helped me debug an irreproducible crash. From all the developer options I have tried, it’s probably my favorite tool!
您不仅会检测到不当行为,而且此选项还可以帮助您调试崩溃。 我无法计算这个工具帮助我调试了不可复制的崩溃的次数。 在我尝试过的所有开发人员选项中,它可能是我最喜欢的工具!
Now you may forget to turn this option off. Since you can have weird behaviors, I recommend letting your testers know when it’s active. You could, for instance, display a in Debug mode when launching your application. You can detect it with this extension method:
现在您可能会忘记关闭此选项。 由于您可能会有奇怪的行为,因此我建议让测试人员知道它何时处于活动状态。 例如,您可以在启动应用程序时以调试模式显示 。 您可以使用以下扩展方法进行检测:
We found a way to test memory pressure on our activities. What about our background processes?
我们找到了一种测试活动压力的方法。 那我们的后台流程呢?
Thanks to StrictMode, we saw how vital leveraging threads is. You should focus on striving to lighten the main thread. You may want to create background processes via a , for instance.
感谢StrictMode,我们看到了利用线程的重要性。 您应该集中精力减轻主螺纹。 例如,您可能想通过创建后台进程。
Yet in a multi-threaded application, you cannot expect how many processors your users’ phones can handle.
但是,在多线程应用程序中,您无法期望用户的手机可以处理多少个处理器。
Also, you’re not the only application creating background processes. And the Android system has to make room sometimes. Some of them will stay in memory for a while. Some will be killed.
此外,您不是创建后台进程的唯一应用程序。 Android系统有时必须腾出空间。 其中一些将在内存中保留一段时间。 一些将被杀死。
You must ensure that your application handles your processes’ recreation well.
您必须确保您的应用程序能够很好地处理流程的重新创建。
Right below the “Don’t keep activity” option, you can find the “Limit background processes” field. It lets you restrict how many of them you accept to run simultaneously.
在“不要继续活动”的正下方 选项,您可以找到“限制后台进程”字段。 它允许您限制要接受的数量以同时运行。
Be aware that this option has gone beyond the developer’s grasp. Several users are enabling it on their phone for:
请注意,此选项已超出开发人员的控制范围。 有几位用户在其手机上启用了以下功能:
- Performance reasons 性能原因
- Battery-saving 节电
Not everyone owns the latest flagship. The Android ecosystem is vast, and you must always consider how your app performs in it.
不是每个人都拥有最新的旗舰。 Android生态系统非常庞大,您必须始终考虑应用程序在其中的性能。
Don’t be shy and try them out. Don’t neglect these tools. They could be your best chance to debug your application.
不要害羞,尝试一下。 不要忽视这些工具。 它们可能是您调试应用程序的最佳机会。
And the good thing is you don’t even have to install anything! You only need to visit them. They suffer from weak exposure inside this hidden menu.
好处是您甚至不必安装任何东西! 您只需要访问它们。 他们在此隐藏菜单中的曝光不足。
I’ve only highlighted four debugging tools, but you should investigate the others as well. Depending on what you’re working on, some of them may benefit you more than they did me.
我仅强调了四个调试工具,但您也应该研究其他工具。 根据您所从事的工作,其中有些人可能会比我受益。
So grab your phone, play around with them, and give your app a treat!