Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hand-made Crash Report of Android Library

petitviolet
October 13, 2015

Hand-made Crash Report of Android Library

http://connpass.com/event/20240/
How to implement crash report of Android-Library

petitviolet

October 13, 2015
Tweet

More Decks by petitviolet

Other Decks in Technology

Transcript

  1. جຊ෦෼ 
 class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
 private static final

    String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
 
 private final Thread.UncaughtExceptionHandler mDefaultHandler;
 private WeakReference<Context> mContext;
 volatile private static boolean sCrashing = false;
 
 public MyUncaughtExceptionHandler(Context context) {
 mContext = new WeakReference<>(context.getApplicationContext());
 mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
 }
 }
  2. جຊ෦෼ 
 class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
 private static final

    String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
 
 private final Thread.UncaughtExceptionHandler mDefaultHandler;
 private WeakReference<Context> mContext;
 volatile private static boolean sCrashing = false;
 
 public MyUncaughtExceptionHandler(Context context) {
 mContext = new WeakReference<>(context.getApplicationContext());
 mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
 }
 } UncaughtExceptionHandlerΛܧঝ
  3. جຊ෦෼ 
 class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
 private static final

    String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
 
 private final Thread.UncaughtExceptionHandler mDefaultHandler;
 private WeakReference<Context> mContext;
 volatile private static boolean sCrashing = false;
 
 public MyUncaughtExceptionHandler(Context context) {
 mContext = new WeakReference<>(context.getApplicationContext());
 mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
 }
 } DefaultͷUncaughtExceptionHandlerΛอଘ UncaughtExceptionHandlerΛܧঝ
  4. جຊ෦෼ @Override
 public void uncaughtException(final Thread thread, final Throwable ex)

    {
 if (sCrashing) {
 mDefaultHandler.uncaughtException(thread, ex);
 return;
 }
 sCrashing = true;
 StackTraceElement[] stackTraceElements = ex.getStackTrace();
 
 boolean inLibraryException = false;
 for (StackTraceElement stackTraceElement : stackTraceElements) {
 if (stackTraceElement.getClassName().contains(PACKAGE_NAME)) {
 inLibraryException = true;
 break;
 }
 }
 
 // ϥΠϒϥϦ಺ͰͷΫϥογϡͩͬͨ৔߹͸ϩάΛૹΔ
 if (inLibraryException) {
 try {
 sendCrashReport(ex);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 
 // ϝΠϯεϨουͩͬͨ৔߹͸ࢮ͵͔͠ແ͍
 if (thread.getId() == Looper.getMainLooper().getThread().getId() || !inLibraryException) {
 mDefaultHandler.uncaughtException(thread, ex);
 }
 }
  5. @Override
 public void uncaughtException(final Thread thread, final Throwable ex) {


    if (sCrashing) {
 mDefaultHandler.uncaughtException(thread, ex);
 return;
 }
 sCrashing = true;
 StackTraceElement[] stackTraceElements = ex.getStackTrace();
 
 boolean inLibraryException = false;
 for (StackTraceElement stackTraceElement : stackTraceElements) {
 if (stackTraceElement.getClassName().contains(PACKAGE_NAME)) {
 inLibraryException = true;
 break;
 }
 } ͢ͰʹΫϥογϡͨ͠ޙͳΒࢮ͵
  6. @Override
 public void uncaughtException(final Thread thread, final Throwable ex) {


    if (sCrashing) {
 mDefaultHandler.uncaughtException(thread, ex);
 return;
 }
 sCrashing = true;
 StackTraceElement[] stackTraceElements = ex.getStackTrace();
 
 boolean inLibraryException = false;
 for (StackTraceElement stackTraceElement : stackTraceElements) {
 if (stackTraceElement.getClassName().contains(PACKAGE_NAME)) {
 inLibraryException = true;
 break;
 }
 } ͢ͰʹΫϥογϡͨ͠ޙͳΒࢮ͵ Library಺ͷΫϥογϡ͔Ͳ͏͔൑ఆ
  7. // ϥΠϒϥϦ಺ͰͷΫϥογϡͩͬͨ৔߹͸ϩάΛૹΔ
 if (inLibraryException) {
 try {
 sendCrashReport(ex);
 } catch

    (Exception e) {
 e.printStackTrace();
 }
 }
 
 // ϝΠϯεϨουͩͬͨ৔߹͸ࢮ͵͔͠ແ͍
 if (thread.getId() == Looper.getMainLooper().getThread().getId() || !inLibraryException) {
 mDefaultHandler.uncaughtException(thread, ex);
 }
 }
  8. // ϥΠϒϥϦ಺ͰͷΫϥογϡͩͬͨ৔߹͸ϩάΛૹΔ
 if (inLibraryException) {
 try {
 sendCrashReport(ex);
 } catch

    (Exception e) {
 e.printStackTrace();
 }
 }
 
 // ϝΠϯεϨουͩͬͨ৔߹͸ࢮ͵͔͠ແ͍
 if (thread.getId() == Looper.getMainLooper().getThread().getId() || !inLibraryException) {
 mDefaultHandler.uncaughtException(thread, ex);
 }
 } Library಺ͷΫϥογϡͳΒϩάૹ৴
  9. // ϥΠϒϥϦ಺ͰͷΫϥογϡͩͬͨ৔߹͸ϩάΛૹΔ
 if (inLibraryException) {
 try {
 sendCrashReport(ex);
 } catch

    (Exception e) {
 e.printStackTrace();
 }
 }
 
 // ϝΠϯεϨουͩͬͨ৔߹͸ࢮ͵͔͠ແ͍
 if (thread.getId() == Looper.getMainLooper().getThread().getId() || !inLibraryException) {
 mDefaultHandler.uncaughtException(thread, ex);
 }
 } Library֎ͷΫϥογϡ͔UIεϨουͳΒࢮ͵ Library಺ͷΫϥογϡͳΒϩάૹ৴
  10. جຊ෦෼ @Override
 public void uncaughtException(final Thread thread, final Throwable ex)

    {
 if (sCrashing) {
 mDefaultHandler.uncaughtException(thread, ex);
 return;
 }
 sCrashing = true;
 StackTraceElement[] stackTraceElements = ex.getStackTrace();
 
 boolean inLibraryException = false;
 for (StackTraceElement stackTraceElement : stackTraceElements) {
 if (stackTraceElement.getClassName().contains(PACKAGE_NAME)) {
 inLibraryException = true;
 break;
 }
 }
 
 // ϥΠϒϥϦ಺ͰͷΫϥογϡͩͬͨ৔߹͸ϩάΛૹΔ
 if (inLibraryException) {
 try {
 sendCrashReport(ex);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 
 // ϝΠϯεϨουͩͬͨ৔߹͸ࢮ͵͔͠ແ͍
 if (thread.getId() == Looper.getMainLooper().getThread().getId() || !inLibraryException) {
 mDefaultHandler.uncaughtException(thread, ex);
 }
 } Library಺ͷΫϥογϡ͔Ͳ͏͔൑ఆ Library಺ͷΫϥογϡͳΒϩάૹ৴ Library֎ͷΫϥογϡ͔UIεϨουͳΒࢮ͵ ͢ͰʹΫϥογϡͨ͠ޙͳΒࢮ͵