- 在 Applicaiton中进行初始化崩溃收集器
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); //初始化崩溃收集器 CollectCrashUtils.initColleteCrash(); } }
2.收集java层崩溃和native层崩溃
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接口进行收集
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(); } }