一.问题抛出

  • android运行的时候难免会有一些空指针(NullPointerException)或者下标越界(IndexOutOfBoundsException),用户使用的过程操作某一个按钮的时候,就发生了崩溃.这时候可能还没有到他感兴趣的部分,程序就Crash掉了,因此导致了用户流失
  • 在集成一些第三方库的时候,我们不能修改里面的实现(例如 Gson,FastJson,OkHttp,OkSocket,Glide)等,那么如果他们里面抛出异常如何解决,Glide加载图片之后填充图片时,如果Activity销毁就会造成崩溃,那么我们如何解决?

二.解决效果

%title插图%num

预防崩溃演示效果图.gif

三.解决思路

  • MainLooper一定要保证在崩溃的时候持续Loop
  • 子线程发生崩溃,保证主线程Looper继续Loop
  • 当绘制,测量,布局出现问题导致编舞者Crash时,应该及时处理(关闭当前异常的界面)
  • 生命周期方法发生了异常,在ActivityThread里面hook其中的Instrumentation.进行代理.
  • 对ActivityThread中的mH 变量添加Callback,对于不能再Instrumentation中处理的异常进行再次Catch.*大限度保证不崩溃.

四.成品Library

Github项目地址: https://github.com/xuuhaoo/DefenseCrash (欢迎Star)
集成方法:

  • Please add the code into your project gradle file
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

%title插图%num

  • maven { url 'https://dl.bintray.com/xuuhaoo/maven/'}
  • %title插图%num
  • Make sure you already done with above instructions in project gradle
    files, than you need put this into Module build.gradle file
  • //崩溃预防,可以避免空指针等崩溃错误
      compile 'com.tonystark.android:defense_crash:2.0.0'
  • %title插图%num

We provide you two options for choosing:

  • Options 1: You should manually install the defense as following code
  1. public class MyApp extends Application implements IExceptionHandler {
  2. @Override
  3. protected void attachBaseContext(Context base) {
  4. super.attachBaseContext(base);
  5. // step1: Initialize the lib.
  6. DefenseCrash.initialize();
  7. // setp2: Install the fire wall defense.
  8. DefenseCrash.install(this);
  9. }
  10. @Override
  11. public void onCaughtException(Thread thread, Throwable throwable, boolean isSafeMode) {
  12. // step3: Print the error when crashed during runtime.
  13. throwable.printStackTrace();
  14. // step4: Upload this throwable to your crash collection sdk.
  15. }
  16. @Override
  17. public void onEnterSafeMode() {
  18. // We enter the safe mode to keep the main looper loop after crashed.You’d better do nothing here,we just notify you.
  19. }
  20. @Override
  21. public void onMayBeBlackScreen(Throwable throwable) {
  22. // onLayout(),onMeasure() or onDraw() has breaks down,
  23. // it causes the drawing to be abnormal and the choreographer to break down.
  24. // We will notify you on this method,you’d better finish this activity or restart the application.
  25. }
  26. }
  • Options 2: To facilitate some users, we provide a DefenseCrashApplication super class for you to integrate,as following code
  1. public class MyApp extends DefenseCrashApplication {
  2. @Override
  3. public void onCaughtException(Thread thread, Throwable throwable, boolean isSafeMode) {
  4. // step1: Print the error when crashed during runtime.
  5. throwable.printStackTrace();
  6. // step2: Upload this throwable to your crash collection sdk.
  7. }
  8. @Override
  9. public void onEnterSafeMode() {
  10. // We enter the safe mode to keep the main looper loop after crashed.You’d better do nothing here,we just notify you.
  11. }
  12. @Override
  13. public void onMayBeBlackScreen(Throwable throwable) {
  14. // onLayout(),onMeasure() or onDraw() has breaks down,
  15. // it causes the drawing to be abnormal and the choreographer to break down.
  16. // We will notify you on this method,you’d better finish this activity or restart the application.
  17. }
  18. }