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);
    }
}
相关推荐
王泰虎1 小时前
安卓开发日记,因为JCenter 关闭导致加载不了三方库应该怎么办
android
2601_949543015 小时前
Flutter for OpenHarmony垃圾分类指南App实战:主题配置实现
android·flutter
2601_949833396 小时前
flutter_for_openharmony口腔护理app实战+知识实现
android·javascript·flutter
晚霞的不甘6 小时前
Flutter for OpenHarmony从基础到专业:深度解析新版番茄钟的倒计时优化
android·flutter·ui·正则表达式·前端框架·鸿蒙
鸟儿不吃草6 小时前
android的Retrofit请求https://192.168.43.73:8080/报错:Handshake failed
android·retrofit
Minilinux20186 小时前
Android音频系列(09)-AudioPolicyManager代码解析
android·音视频·apm·audiopolicy·音频策略
李子红了时7 小时前
【无标题】
android
Android系统攻城狮8 小时前
Android tinyalsa深度解析之pcm_close调用流程与实战(一百零四)
android·pcm·tinyalsa·音频进阶·音频性能实战·android hal
weixin_411191848 小时前
LifecycleEventObserver和DefaultLifecycleObserver使用
android
、BeYourself8 小时前
Intent :跳转与数据传递的正确打开方式
android·android-studio