前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android自定义崩溃收集器捕获java层和native层崩溃异常日志

Android自定义崩溃收集器捕获java层和native层崩溃异常日志

作者头像
北洋
发布2021-12-08 10:10:45
1.2K0
发布2021-12-08 10:10:45
举报
文章被收录于专栏:北洋csdn北洋csdn
  1. 在 Applicaiton中进行初始化崩溃收集器
代码语言:javascript
复制
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化崩溃收集器
        CollectCrashUtils.initColleteCrash();
    }
}

2.收集java层崩溃和native层崩溃

代码语言:javascript
复制
public class CollectCrashUtils {
    public static void initColleteCrash() {
        //初始化Handler,收集java层崩溃
        MyJavaCrashHandler handler = new MyJavaCrashHandler();
        Thread.setDefaultUncaughtExceptionHandler(handler);

        //收集native层崩溃
        File file = new File("sdcard/Crashlog");
        if (!file.exists()) {
            file.mkdirs();
        }
        NativeBreakpad.init(file.getAbsolutePath());
    }
}

3.native层的崩溃收集可以使用编译好的breakpad.so。(参考 https://github.com/yinyinnie/breakpad-for-android.git)

4.java层崩溃实现Thread.UncaughtExceptionHandler接口进行收集

代码语言:javascript
复制
public class MyJavaCrashHandler implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        Log.e("程序出现异常了", "Thread = " + t.getName() + "\nThrowable = " + e.getMessage());
        String stackTraceInfo = getStackTraceInfo(e);
        Log.e("stackTraceInfo", stackTraceInfo);
        saveThrowableMessage(stackTraceInfo);
    }

    /**
     * 获取错误的信息
     *
     * @param throwable
     * @return
     */
    private String getStackTraceInfo(final Throwable throwable) {
        PrintWriter pw = null;
        Writer writer = new StringWriter();
        try {
            pw = new PrintWriter(writer);
            throwable.printStackTrace(pw);
        } catch (Exception e) {
            return "";
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
        return writer.toString();
    }

    private String logFilePath = "sdcard/Crashlog";

    private void saveThrowableMessage(String errorMessage) {
        if (TextUtils.isEmpty(errorMessage)) {
            return;
        }
        File file = new File(logFilePath);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (mkdirs) {
                writeStringToFile(errorMessage, file);
            }
        } else {
            writeStringToFile(errorMessage, file);
        }
    }

    private void writeStringToFile(final String errorMessage, final File file) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FileOutputStream outputStream = null;
                try {
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(errorMessage.getBytes());
                    outputStream = new FileOutputStream(new File(file, System.currentTimeMillis() + ".txt"));
                    int len = 0;
                    byte[] bytes = new byte[1024];
                    while ((len = inputStream.read(bytes)) != -1) {
                        outputStream.write(bytes, 0, len);
                    }
                    outputStream.flush();
                    Log.e("程序出异常了", "写入本地文件成功:" + file.getAbsolutePath());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/02/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档