RuntimeException 在 Android 上导致 ANR

RuntimeException causes ANR on Android

我的 Android 应用程序有问题。当我抛出 RuntimeException 时,它会导致 ANR 而不是崩溃。

比如我有这样的代码:

button.setOnClickListener {
    throw RuntimeException("Test crash")
}

当我单击此按钮时,应用会冻结一段时间,然后显示有关 ANR 的警报。

我读过类似的问题,在此处加注:

但我不知道如何修复它。我尽我所能检查了一切:没有附加 UncaughtExceptionHandler-s,我没有使用过时的 google-play-services 库。我应该去哪里寻找问题的原因?

单击此按钮后,我可以在 logcat 中看到堆栈跟踪,这很有趣:

2021-08-04 09:05:39.464 8764-8764/wa.testapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: wa.testapp, PID: 8764
    java.lang.RuntimeException: Test crash
        at com.example.testapp.ui.main.FragmentMain.onViewCreated$lambda-7(FragmentMain.kt:102)
        at com.example.testapp.ui.main.FragmentMain.$r8$lambda$agRVJ2mByrrRLVPpioFH6VVBf9s(Unknown Source:0)
        at com.example.testapp.ui.main.FragmentMain$$ExternalSyntheticLambda8.onClick(Unknown Source:0)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access00(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-08-04 09:05:39.467 8764-8764/wa.testapp E/a: Exception:
    message: Test crash
    localized message: Test crash
    stack trace:
    java.lang.RuntimeException: Test crash
        at com.example.testapp.ui.main.FragmentMain.onViewCreated$lambda-7(FragmentMain.kt:102)
        at com.example.testapp.ui.main.FragmentMain.$r8$lambda$agRVJ2mByrrRLVPpioFH6VVBf9s(Unknown Source:0)
        at com.example.testapp.ui.main.FragmentMain$$ExternalSyntheticLambda8.onClick(Unknown Source:0)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access00(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

我找到了这种行为的原因。原因是 com.smartlook.recording:app:1.1.2-native 依赖。从项目中删除此库后一切正常。