Android ANR的解决方案

1、使用monkey进行压测,命令如下:

bash 复制代码
adb shell monkey -p com.toycloud.alphaegg.launcher --hprof --ignore-crashes --ignore-timeouts --ignore-security-exceptions --pct-syskeys 0 --throttle 100 -v -v -v 10000 > C:\Users\jjyang31\Desktop\public_apk\Monkeytest_20251103\"monkeytest_%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%-%time:~3,2%-%time:~6,2%.log"
// 100 是间隔时间 10000模拟触摸次数

2、执行完毕后通过adb bugreport 自动把anr相关的日志打包下载。

3、通过关键字搜索日志:

复制代码
	ANR in
	Reason
	"main" prio=
	Input dispatching timed out
	Broadcast of Intent
	executing service
	ContentProvider not responding
	你的包名

4、使用爱奇艺监听ANR/JAVA崩溃,添加依赖

bash 复制代码
    implementation "com.iqiyi.xcrash:xcrash-android-lib:3.1.0"
bash 复制代码
//	在Application进行初始化工作
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        String filePath = getCacheDir().getAbsolutePath() + "/xcrash";
        Log.d("MyApplication", "filePath=" + filePath);

        XCrash.InitParameters params = new XCrash.InitParameters()
                .setLogDir(filePath)
                .setJavaCallback((logPath, emergency) -> {
                    // Java 崩溃回调
                    Log.d("xCrash", "Java crash captured: " + emergency);
                    // 可以在这里上传日志到服务器
                })
                .setNativeCallback((logPath, emergency) -> {
                    // Native 崩溃回调
                    Log.d("xCrash", "Native crash captured: " + emergency);
                })
                .setAnrCallback((logPath, emergency) -> {
                    // ANR 回调
                    Log.d("xCrash", "ANR captured: " + emergency);
                });

        XCrash.init(this, params);
    }
}
相关推荐
随遇丿而安2 小时前
第2周:`EditText` 不只是输入框,它是 Android 输入体验的第一道门
android
我命由我123452 小时前
Kotlin 开发 - lateinit 关键字
android·java·开发语言·kotlin·android studio·android-studio·android runtime
一起搞IT吧2 小时前
Android性能系列专题理论之十:systrace/perfetto相关指标知识点细节含义总结
android·嵌入式硬件·智能手机·性能优化
小书房7 小时前
Kotlin的by
android·开发语言·kotlin·委托·by
jinanwuhuaguo7 小时前
(第二十八篇)OpenClaw成本与感知的奇点——从“Token封建制”到“全民养虾”的本体论地基
android·人工智能·kotlin·拓扑学·openclaw
xxjj998a8 小时前
Laravel4.x核心特性全解析
android·mysql·laravel
JoshRen8 小时前
2026教程:在Android Termux中集成Gemini 3镜像站实现移动端文档自动处理与摘要生成(附国内免费方案)
android
诸神黄昏EX9 小时前
Android Google KEY
android
一起搞IT吧9 小时前
Android性能系列专题理论之十一:block IO问题分析思路
android·嵌入式硬件·智能手机·性能优化
小妖66610 小时前
怎么用 tauri 创建编译 android 应用程序
android·tauri