如何分析各种ANR第二篇?Google官方文档详细教你

关注Google Play应用崩溃与ANR,增加用户的好评率提升应用转化 Google Play应用崩溃与ANR的检测十分重要,崩溃与ANR的高低会影响应用在商店的排名、用户的评分、留存等,另外2019 Google I/O后,该数据纳入了官方推荐的审核标准中,数据高于平均线将无法获得官方推荐,因此降低应用的崩溃与ANR势在必行。 什么是崩溃与ANR? “崩溃”就是应用出现异常报错,应用自动退出弹回桌面。崩溃可能出现在进应用的瞬间,也可能在应用使用过程中出现。崩溃率高的... 阅读详情

背景:

在实际开发工作各种类型的ANR层出不穷,之前一直也想google官方开发人员是否有解决各种ANR问题的一些文档等,主要是想看看自己平时自己分析套路是否也和google的人一样,还有就是想看看goole是否有啥分析ANR的新套路方法等,今天刚好找到了google关于各种ANR问题的详细指导文档,本来想用翻译成中文的版本,但是感觉自动翻译的其实并不是太好,所以直接上英文,马哥搬运google的干货。
本文主要摘抄以下几个类型的ANR详细分析指导和案例:
1、Execute service timeout
2、Content provider not responding
3、Slow job response
4、Mystery ANRs
下面是google官方文档,底部也有原文链接。

Execute service timeout(Service超时ANR)

An execute service ANR happens when the app’s main thread doesn’t start a service in time. Specifically, a service doesn’t finish executing onCreate() and onStartCommand() or onBind() within the timeout period.

Default timeout period:
20 seconds for foreground service; 200 seconds for background service. The ANR timeout period includes the app cold start, if necessary, and calls to onCreate(), onBind(), or onStartCommand().

To avoid execute service ANRs, follow these general best practices:

Make sure that app startup is fast, since it’s counted in the ANR timeout if the app is started to run the service component.

Make sure that the service’s onCreate(), onStartCommand(), and onBind() methods are fast.

Avoid running any slow or blocking operations on the main thread from other components; these operations can prevent a service from starting quickly.

Common causes(常见案例)

The following table lists common causes of execute service ANRs and suggested fixes.
在这里插入图片描述

How to debug (如何调试ANR)

From the cluster signature and ANR report in Google Play Console or Firebase Crashlytics, you can often determine the cause of the ANR based on what the main thread is doing.

Note: Ignore execute service ANR clusters that say “nativePollOnce” or “main thread idle.” These usually correspond to ANRs where the stack dump is taken too late, and are generally not actionable. The actual ANR issues are usually present in other clusters, so real issues aren’t being hidden. See nativePollOnce for more details.
The following flow chart describes how to debug an execute service ANR.

在这里插入图片描述

Figure 6. How to debug an execute service ANR.

If you’ve determined that the execute service ANR is actionable, follow these steps to help resolve the issue:

Find the service component class in the ANR signature. In Google Play Console, the service component class is shown in the ANR signature. In the following example ANR details, it’s com.example.app/MyService.

com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly
Executing service com.example.app/com.example.app.MyService

Determine whether the slow or block operation is part of app startup, the service component, or elsewhere by checking for the following important function call(s) in the main threads.
在这里插入图片描述

For example, if the onStartCommand() method in the MyService class is slow, the main threads will look like this:

at com.example.app.MyService.onStartCommand(FooService.java:25)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4820)
at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(unavailable:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2289)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8176)
at java.lang.reflect.Method.invoke(Native method:0)

If you can’t see any of the important function calls, there are a couple other possibilities:

The service is running or shutting down, which means that the stacks are taken too late. In this case, you can ignore the ANR as a false positive.
A different app component is running, such as a broadcast receiver. In this case the main thread is likely blocked in this component, preventing the service from starting.

If you do see a key function call and can determine where the ANR is happening generally, check the rest of the main thread stacks to find the slow operation and optimize it or move it off the critical path.

For more information about services, see the following pages:

Services overview
Foreground services
Service

Content provider not responding(ContentProvider无响应ANR)

A content provider ANR happens when a remote content provider takes longer than the timeout period to respond to a query, and is killed.

Default timeout period: specified by content provider using ContentProviderClient.setDetectNotResponding. The ANR timeout period includes the total time for a remote content provider query to run, which includes cold-starting the remote app if it wasn’t already running.

To avoid content provider ANRs, follow these best practices:

Make sure that app startup is fast, since it’s counted in the ANR timeout if the app is started to run the content provider.
Make sure that the content provider queries are fast.
Don’t perform lots of concurrent blocking binder calls that can block all the app’s binder threads.

Common causes

The following table lists common causes of content provider ANRs and suggested fixes.
在这里插入图片描述

How to debug

To debug a content provider ANR using the cluster signature and ANR report in Google Play Console or Firebase Crashlytics, look at what the main thread and binder thread(s) are doing.

The following flow chart describes how to debug a content provider ANR:

在这里插入图片描述

Figure 7. How to debug a content provider ANR.
The following code snippet shows what the binder thread looks like when it’s blocked due to a slow content provider query. In this case, the content provider query is waiting for lock when opening a database.

binder:11300_2 (tid=13) Blocked

Waiting for osm (0x01ab5df9) held by at com.google.common.base.Suppliers$NonSerializableMemoizingSupplier.get(Suppliers:182)
at com.example.app.MyClass.blockingGetOpenDatabase(FooClass:171)
[...]
at com.example.app.MyContentProvider.query(MyContentProvider.java:915)
at android.content.ContentProvider$Transport.query(ContentProvider.java:292)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:107)
at android.os.Binder.execTransactInternal(Binder.java:1339)
at android.os.Binder.execTransact(Binder.java:1275)

The following code snippet shows what the main thread looks like when it’s blocked due to slow app startup. In this case, the app startup is slow due to lock contention during dagger initialization.

main (tid=1) Blocked

[...]
at dagger.internal.DoubleCheck.get(DoubleCheck:51)
- locked 0x0e33cd2c (a qsn)at dagger.internal.SetFactory.get(SetFactory:126)
at com.myapp.Bar_Factory.get(Bar_Factory:38)
[...]
at com.example.app.MyApplication.onCreate(DocsApplication:203)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6991)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(unavailable:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2235)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8170)
at java.lang.reflect.Method.invoke(Native method:0)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

Slow job response(JobService相关anr)

A slow job response ANR happens when the app takes too long to respond to JobService.onStartJob() or JobService.onStopJob(), or takes too long to provide a notification using JobService.setNotification(). This suggests that the app’s main thread is blocked doing something else.

If it’s an issue with JobService.onStartJob() or JobService.onStopJob(), check what’s happening on the main thread. If it’s an issue with JobService.setNotification(), make sure to call it as quickly as possible. Don’t do a lot of work before providing the notification.

Mystery ANRs(神秘ANR)

Sometimes it’s unclear why an ANR is occurring, or there is insufficient information to debug it in the cluster signature and ANR report. In these cases, there are still some steps you can take to determine whether the ANR is actionable.

Message queue idle or nativePollOnce

If you see the frame android.os.MessageQueue.nativePollOnce in the stacks, it often indicates that the suspected unresponsive thread was actually idle and waiting for looper messages. In Google Play Console, the ANR details look like this:

Native method - android.os.MessageQueue.nativePollOnce

Executing service com.example.app/com.example.app.MyService
For example, if the main thread is idle the stacks look like this:
"main" tid=1 NativeMain threadIdle

#00  pc 0x00000000000d8b38  /apex/com.android.runtime/lib64/bionic/libc.so (__epoll_pwait+8)
#01  pc 0x0000000000019d88  /system/lib64/libutils.so (android::Looper::pollInner(int)+184)
#02  pc 0x0000000000019c68  /system/lib64/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+112)
#03  pc 0x000000000011409c  /system/lib64/libandroid_runtime.so (android::android_os_MessageQueue_nativePollOnce(_JNIEnv*, _jobject*, long, int)+44)
at android.os.MessageQueue.nativePollOnce (Native method)
at android.os.MessageQueue.next (MessageQueue.java:339)  at android.os.Looper.loop (Looper.java:208)
at android.app.ActivityThread.main (ActivityThread.java:8192)
at java.lang.reflect.Method.invoke (Native method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:626)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1015)

There are several reasons why the suspected unresponsive thread can be idle:

Late stack dump.
The thread recovered during the short period between the ANR triggering and the stacks being dumped. The latency in Pixels on Android 13 is around 100ms, but can exceed 1s. The latency in Pixels on Android 14 is usually under 10ms.

Thread misattribution.
The thread used to build the ANR signature was not the actual unresponsive thread that caused the ANR. In this case, try to determine if the ANR is one of the following types:
Broadcast receiver timeout
Content provider not responding
No focused window

System-wide issue.

The process wasn’t scheduled due to heavy system load or an issue in the system server.

No stack frames

Some ANR reports don’t include the stacks with the ANR, which means that the stack dumping failed when generating the ANR report. There are a couple of possible reasons for missing stack frames:

Taking the stack takes too long and times out.
The process died or was killed before the stacks were taken.

[...]

--- CriticalEventLog ---
capacity: 20
timestamp_ms: 1666030897753
window_ms: 300000

libdebuggerd_client: failed to read status response from tombstoned: timeout reached?

----- Waiting Channels: pid 7068 at 2022-10-18 02:21:37.<US_SOCIAL_SECURITY_NUMBER>+0800 -----

[...]

ANRs without stack frames aren’t actionable from the cluster signature or ANR report. To debug, look at other clusters for the app, since if an issue is large enough it’ll usually have its own cluster where stack frames are present. Another option is to look at Perfetto traces.

Known issues

Keeping a timer in your app’s process for the purposes of finishing broadcast handling before an ANR triggers might not work correctly because of the asynchronous way the system monitors ANRs.

原文地址:
https://mp.weixin.qq.com/s/Bl2gV2ERghm2JZFF5LRfIA
如何分析各种ANR?Google官方文档详细教你

ANR问题解析(三):ANR问题解决方案 本篇文章主要介绍 Android 开发中的部分GMS 包相关APK ANR,闪退问题解决方案知识点,通过阅读本篇文章,您将收获以下内容: 一、开机向导时 Google DUO 概率ANR 二、开机向导时 Google Calendar 概率 ANR 三、开机向导时 ANR 弹框不show的解决方案 四、开机向导时 Google Music 概率 ANR 五、开机向导时 Google Play Store 概率 ANR 六、 Google play Store 下载apk 概率性闪退 一、开机向导时 DU 阅读详情

相关推荐

ANR问题解析(三):ANR问题解决方案,安卓开发自学书籍推荐

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。上面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,上面只是以图片的形式给大家展示一部分。【Android思维脑图(技能树)】知识不体系?

2401_84150154的博客 978

还在自己写Java懒加载代码吗?

Java懒加载工具类   com.google.common.base.Suppliers.memoize(Supplier&lt;T&gt;) com.google.common.base.Suppliers.memoizeWithExpiration(Supplier&lt;T&gt;, long, TimeUnit)     package tool; import co...

小恩 429

ANR实战案例1 - Google广告导致ANR解决

Google广告作为出海应用较为常见的接入SDK,其产生的ANR解决相对较为困难,可参考笔者开发中方案。

Android百晓生的博客 1076

Guava笔记之Supplier和Suppliers的使用

Supplier封装了获取值的功能,可以实现多种不同的获取值的方式,创建Supplier可以使用Suppliers类的静态方法来实现,该方法提供了多种获取值的方式,具体为: 一、Supplier接口 该接口提供了get方法用于获取值,注意其中get方法并不能传递参数信息,所以如果需要传递参数信息的话则不适用该接口。 public interface Supplier<T> exten...

Taoqigongzi_jie的博客 5994

LPDDR5/5X BG mode,8B mode和16B mode

LPDDR5/5X 器件,协议上支持多个bank架构可静态配置;配置在MR3:模式说明协议要求速率要求BLBG模式每CH 4BG, 4BALPDDR5/5X>3200MbpsBL16 & 328B模式每CH 8BALPDDR5任何速率BL3216B模式每CH 16BALPDDR5/5X<=3200MbpsBL16 & 32。

hemlok的博客 8869

Ubuntu的初步环境配置(VMware)

4.屏幕显示不全解决完后,在试用Ubuntu里面,直接双击“安装Ubuntu 20.04.6 LTS”。如果不存在“屏幕显示不全”的问题,直接点击安装即可,大致安装步骤与以下步骤类似。15.点击现在重启,重启成功后,会出现“清除安装介质”的问题。19.此时点开火狐,可以上网,但只可输入英文,不可以输入中文,具体解决方法见3.2。3.进入安装界面,此时可能会出现“屏幕显示不全”的问题,具体解决方法见3.1。8.选择“正常安装”、“安装Ubuntu时下载更新”,点击继续。最后,点击保留更改,设置成功。

森菌蛙诺的博客 2280

Android系统是如何检测到ANR- 应用无响应的?

最近debug应用时,发现只要看debug的变量内容太久了,app也会爆出anr问题,遂想对ANR做一个系统的整理。

夏目艾拉的学习之路 1362

告别手动画图!用Cadence SKILL写一个参数化电感PCell(附完整代码)

本文详细介绍了如何使用Cadence SKILL语言开发参数化电感PCell,实现电感版图的自动化生成。通过动态调整线宽、圈数等几何参数,自动创建端口并进行工艺规则校验,大幅提升高频电路设计效率。附完整代码示例,帮助工程师快速掌握PCell开发技巧。

weixin_30684743的博客 369

让你从此不再惧怕ANR

是安卓系统的核心基础通信机制,组件件间的通信,Intent,ContentResolver,应用与AMS,PMS和WMS等等之间的交互都是通过binder call来进行的,常规情况下大部分时候binder call都没有问题会很快问题,但如果binder另一头的某个服务发生了问题,即使是瞬时问题,也会导致binder call被阻塞或者变慢,这时就可能引发应用侧的ANR。调试的手段也都是从代码执行的角度,把线程的栈帧转储出来(stack trace dump),以查看是被哪 个函数阻塞了。

稀有猿诉 500

信创迁移实战:在银河麒麟V10-SP1上为Milvus离线部署Docker(附SP1专属避坑指南)

本文详细介绍了在银河麒麟V10-SP1 x86_64环境下离线部署Docker的全流程,特别针对该版本特有的自定义目录安装失败问题提供深度分析和解决方案。通过实战验证的步骤和避坑指南,帮助用户在信创项目中顺利完成Milvus等应用的容器化部署。

weixin_30687051的博客 534

Python pydub实现语音停顿切分

将pcm文件批量处理成wav文件 import wave import os filepath = "data/" # 添加路径 filename = os.listdir(filepath) # 得到文件夹下的所有文件名称 #f = wave.open(filepath + filename[1], 'rb') #print(filename) for i in range(len(fi...

wangqianqianya的博客 1万+

Android ANR分析总结

Android ANR分析总结 ANR(Application Not Responding)是Android系统检测到主线程长时间阻塞时触发的机制。常见原因包括主线程执行耗时操作(IO、网络请求等)、锁竞争和CPU资源不足。系统对不同场景设置了超时阈值(如输入事件5秒、前台广播10秒等)。 分析ANR的关键步骤: 查看logcat日志,搜索"ANR in"获取关键信息(如报错位置、超时原因) 检查/data/anr目录下的堆栈文件(需root权限),重点关注"main&quo

wenzhi的博客 3371

【PyTorch】CNN(一维卷积Conv1D)实现时间序列预测

本文介绍了一维卷积神经网络(Conv1D)在时间序列预测中的应用,展示了其基本原理及实现方式。通过合适的数据预处理和模型调参,Conv1D可以有效地捕捉时间序列数据中的模式,从而进行精准预测。

走向CTO的路上... 1174

Google Play的崩溃与ANR

Google Play的崩溃与ANR崩溃与ANR的定义崩溃与ANR的影响崩溃与ANR查找查看具体的崩溃ANR日志 崩溃与ANR的定义 崩溃指应用出现异常报错,自动退出弹回桌面 ANR表示应用无响应,会弹出对话框让用户选择关闭或等待 崩溃与ANR的影响 Google Play应用的崩溃与ANR数据高于平均线将无法获得官方推荐,进而导致应用的排名下降、评分降低、用户流失 崩溃率和ANR应小于...

luckygirl 2539

【时时三省】vector cast 常见错误解决/vcast常见错误解决

介绍在使用 vector cast/vcast 软件的过程中,遇到的异常问题。其它异常可以私信解决(购买专栏的前提下)。

时时三省的博客 451

inno setup自定义卸载程序和美化

也就是DisplayName,因为我们在第一步的时候,将卸载程序名称也写入了注册表,所以,我们直接从注册表读DisplayName字段即可。1.要实现inno setup自定义卸载程序,首先需要关闭自带的卸载程序。在[Setup]的字段关闭卸载程序步骤和页面。2.删除应用程序目录里能够删除的并且返回进度。3.自定义卸载程序相关的注册表信息。4.删除启动菜单栏和guid注册表。2.自定义快捷方式和程序菜单。

cs821984831的博客 2350

Android ANR问题基本分析方法

对于ANR问题的介绍,首先可以参考google的官方文档。

TangYuanTuoHai的博客 2463

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Google APK ANR 解决方案

极力推荐文章:欢迎收藏Android 干货分享阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android本篇文章主要介绍Android开发中的部分GMS...

程序员Android 918

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Android ANR问题原因分析

自己处理ANR问题单的一点经验,希望能对大家有帮助。

红旺永福 6万+

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Android 分析ANR和死锁

一 Google官网对分析ANR日志和死锁的阐述 ANRs and deadlocks (Reading Bug Reports章节) Bugreports can help you identify what's causing Application Not Responding (ANR) errors and deadlock events. Identifying unrespon...

c_z_w的博客 1431

ANR分析

1. ANR的来源 系统判定一个APP无响应的几个条件包括: 控制Service/Activity/Provider超时、接收广播超时、响应按键/触屏事件超时等。 从系统设计角度来分析,以上这些事件都是给APP主线程发消息,可见这些超时实际都是消息没有在APP的主线程及时处理导致的。 消息没有处理有三种可能: a. 消息没有发送到主线程的消息队列中 b. 主线程还在

xutao20170209的博客 3359

Google APK ANR 优化方案

极力推荐文章:欢迎收藏Android 干货分享阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android本篇文章主要介绍Android开发中的部分GMS...

程序员Android 258

Google App ANR 优化案例

和你一起终身学习,这里是程序员Android经典好文推荐,通过阅读本文,您将收获以下知识点:一、开机向导时 Google DUO 概率ANR二、开机向导时 Google Calendar...

程序员Android 717

Android ANR日志分析进行曲

定义 ANR(Application Not Responding)应用程序无响应。如果你应用程序在UI线程被阻塞太长时间,就会出现ANR,通常出现ANR,系统会弹出一个提示提示框,让用户知道,该程序正在被阻塞,是否继续等待还是关闭。 ANR出现的原因 1:主线程频繁进行耗时的IO操作:如数据库读写(UI线程等待其它线程释放某个锁,导致UI线程无法处理用户输入); 2:多线程操作的死锁,主线程被block; 3:主线程被Binder 对端block; 4:System Server中WatchDog.

qq_31481093的博客 894

Google App ANR 优化案例,android插件化

给大家分享一份移动架构大纲,包含了移动架构师需要掌握的所有的技术体系,大家可以对比一下自己不足或者欠缺的地方有方向的去学习提升;于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)[外链图片转存中…(img-WHGZMMHX-1711932261619)]

2301_76225520的博客 1165
上一篇: 安卓aosp 16新特性分屏收藏保存功能AppPairIcon剖析
下一篇: ANR高级经验2:No Focused Window类型ANR的各种案例汇总
千里马学框架
博客等级 码龄6年 1万+粉丝 652原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里马学框架

帮助你了,就请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值