|
1 | 1 |
|
| 2 | +> Translate via google |
| 3 | +
|
| 4 | +## Cockroach |
| 5 | + |
| 6 | +> Never crash Android |
| 7 | +
|
| 8 | + |
| 9 | +### Manual |
| 10 | + |
| 11 | +Custom Application inherited from the android application, and in the Application load, the sooner the better, you can in the Aplication onCreate initialization, of course, can be anywhere in the need (not necessarily in the main thread) loading, in any place Unloading. Can be loaded and unloaded multiple times. |
| 12 | + |
| 13 | +E.g: |
| 14 | + |
| 15 | +```java |
| 16 | + |
| 17 | + |
| 18 | +import android.app.Application; |
| 19 | +import android.os.Handler; |
| 20 | +import android.os.Looper; |
| 21 | +import android.util.Log; |
| 22 | +import android.widget.Toast; |
| 23 | + |
| 24 | +/** |
| 25 | + * Created by wanjian on 2017/2/14. |
| 26 | + */ |
| 27 | + |
| 28 | +public class App extends Application { |
| 29 | + |
| 30 | + @Override |
| 31 | + public void onCreate() { |
| 32 | + super.onCreate(); |
| 33 | + |
| 34 | + Cockroach.install(new Cockroach.ExceptionHandler() { |
| 35 | + |
| 36 | + //HandlerException internal advice manually try {your exception handling logic} catch (throwable e) {}, in case handlerException internally throws an exception again, causing the loop to call handlerException |
| 37 | + |
| 38 | + @Override |
| 39 | + public void handlerException(final Thread thread, final Throwable throwable) { |
| 40 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 41 | + @Override |
| 42 | + public void run() { |
| 43 | + try { |
| 44 | + Log.e("AndroidRuntime","--->CockroachException:"+thread+"<---",throwable); |
| 45 | + Toast.makeText(App.this, "Exception Happend\n" + thread + "\n" + throwable.toString(), Toast.LENGTH_SHORT).show(); |
| 46 | +// throw new RuntimeException("..."+(i++)); |
| 47 | + } catch (Throwable e) { |
| 48 | + |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +``` |
| 60 | +uninstall Cockroach |
| 61 | + |
| 62 | +```java |
| 63 | + |
| 64 | + Cockroach.uninstall(); |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | +### Test |
| 70 | + |
| 71 | +After loading Cockroach, click on the view to throw the exception and throw an exception in the new Handler |
| 72 | + |
| 73 | +```java |
| 74 | + |
| 75 | + |
| 76 | + final TextView textView = (TextView) findViewById(R.id.text); |
| 77 | + findViewById(R.id.install).setOnClickListener(new View.OnClickListener() { |
| 78 | + @Override |
| 79 | + public void onClick(View v) { |
| 80 | + textView.setText("Installed Cockroach"); |
| 81 | + install(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + findViewById(R.id.uninstall).setOnClickListener(new View.OnClickListener() { |
| 86 | + @Override |
| 87 | + public void onClick(View v) { |
| 88 | + textView.setText("UnInstalled Cockroach"); |
| 89 | + Cockroach.uninstall(); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + findViewById(R.id.but1).setOnClickListener(new View.OnClickListener() { |
| 94 | + @Override |
| 95 | + public void onClick(View v) { |
| 96 | + throw new RuntimeException("click exception..."); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + findViewById(R.id.but2).setOnClickListener(new View.OnClickListener() { |
| 101 | + @Override |
| 102 | + public void onClick(View v) { |
| 103 | + new Handler().post(new Runnable() { |
| 104 | + @Override |
| 105 | + public void run() { |
| 106 | + throw new RuntimeException("handler exception..."); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + findViewById(R.id.but3).setOnClickListener(new View.OnClickListener() { |
| 113 | + @Override |
| 114 | + public void onClick(View v) { |
| 115 | + new Thread() { |
| 116 | + @Override |
| 117 | + public void run() { |
| 118 | + super.run(); |
| 119 | + throw new RuntimeException("new thread exception..."); |
| 120 | + } |
| 121 | + }.start(); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + findViewById(R.id.but4).setOnClickListener(new View.OnClickListener() { |
| 126 | + @Override |
| 127 | + public void onClick(View v) { |
| 128 | + startActivity(new Intent(getApplicationContext(), SecActivity.class)); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + private void install() { |
| 135 | + Cockroach.install(new Cockroach.ExceptionHandler() { |
| 136 | + @Override |
| 137 | + public void handlerException(final Thread thread, final Throwable throwable) { |
| 138 | + |
| 139 | + Log.d("Cockroach", "MainThread: " + Looper.getMainLooper().getThread() + " curThread: " + Thread.currentThread()); |
| 140 | + |
| 141 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 142 | + @Override |
| 143 | + public void run() { |
| 144 | + try { |
| 145 | + |
| 146 | + Log.e("AndroidRuntime","--->CockroachException:"+thread+"<---",throwable); |
| 147 | + Toast.makeText(getApplicationContext(), "Exception Happend\n" + thread + "\n" + throwable.toString(), Toast.LENGTH_SHORT).show(); |
| 148 | +// throw new RuntimeException("..."+(i++)); |
| 149 | + } catch (Throwable e) { |
| 150 | + |
| 151 | + } |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | +``` |
| 159 | + |
| 160 | +Capture the stack as follows, you can see that has been `at com.wanjian.cockroach.Cockroach$1.run(Cockroach.java:47)` Interception, APP no effect, no flashback, there is no restart process |
| 161 | + |
| 162 | +```java |
| 163 | + |
| 164 | +02-16 09:58:00.660 21199-21199/wj.com.fuck E/AndroidRuntime: --->CockroachException:Thread[main,5,main]<--- |
| 165 | + java.lang.RuntimeException: click exception... |
| 166 | + at wj.com.fuck.MainActivity$3.onClick(MainActivity.java:53) |
| 167 | + at android.view.View.performClick(View.java:4909) |
| 168 | + at android.view.View$PerformClick.run(View.java:20390) |
| 169 | + at android.os.Handler.handleCallback(Handler.java:815) |
| 170 | + at android.os.Handler.dispatchMessage(Handler.java:104) |
| 171 | + at android.os.Looper.loop(Looper.java:194) |
| 172 | + at com.wanjian.cockroach.Cockroach$1.run(Cockroach.java:47) |
| 173 | + at android.os.Handler.handleCallback(Handler.java:815) |
| 174 | + at android.os.Handler.dispatchMessage(Handler.java:104) |
| 175 | + at android.os.Looper.loop(Looper.java:194) |
| 176 | + at android.app.ActivityThread.main(ActivityThread.java:5826) |
| 177 | + at java.lang.reflect.Method.invoke(Native Method) |
| 178 | + at java.lang.reflect.Method.invoke(Method.java:372) |
| 179 | + at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1009) |
| 180 | + at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:804) |
| 181 | +02-16 09:58:12.401 21199-21199/wj.com.fuck E/AndroidRuntime: --->CockroachException:Thread[main,5,main]<--- |
| 182 | + java.lang.RuntimeException: handler exception... |
| 183 | + at wj.com.fuck.MainActivity$4$1.run(MainActivity.java:63) |
| 184 | + at android.os.Handler.handleCallback(Handler.java:815) |
| 185 | + at android.os.Handler.dispatchMessage(Handler.java:104) |
| 186 | + at android.os.Looper.loop(Looper.java:194) |
| 187 | + at com.wanjian.cockroach.Cockroach$1.run(Cockroach.java:47) |
| 188 | + at android.os.Handler.handleCallback(Handler.java:815) |
| 189 | + at android.os.Handler.dispatchMessage(Handler.java:104) |
| 190 | + at android.os.Looper.loop(Looper.java:194) |
| 191 | + at android.app.ActivityThread.main(ActivityThread.java:5826) |
| 192 | + at java.lang.reflect.Method.invoke(Native Method) |
| 193 | + at java.lang.reflect.Method.invoke(Method.java:372) |
| 194 | + at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1009) |
| 195 | + at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:804) |
| 196 | +02-16 09:58:13.241 21199-21199/wj.com.fuck E/AndroidRuntime: --->CockroachException:Thread[Thread-26326,5,main]<--- |
| 197 | + java.lang.RuntimeException: new thread exception... |
| 198 | + at wj.com.fuck.MainActivity$5$1.run(MainActivity.java:76) |
| 199 | + |
| 200 | + |
| 201 | +``` |
| 202 | + |
| 203 | + |
| 204 | +When uninstalling `Cockroach` and then throw an exception in the click, the log is as follows |
| 205 | + |
| 206 | +```java |
| 207 | + |
| 208 | +02-16 09:59:01.251 21199-21199/wj.com.fuck E/AndroidRuntime: FATAL EXCEPTION: main |
| 209 | + Process: wj.com.fuck, PID: 21199 |
| 210 | + java.lang.RuntimeException: click exception... |
| 211 | + at wj.com.fuck.MainActivity$3.onClick(MainActivity.java:53) |
| 212 | + at android.view.View.performClick(View.java:4909) |
| 213 | + at android.view.View$PerformClick.run(View.java:20390) |
| 214 | + at android.os.Handler.handleCallback(Handler.java:815) |
| 215 | + at android.os.Handler.dispatchMessage(Handler.java:104) |
| 216 | + at android.os.Looper.loop(Looper.java:194) |
| 217 | + at android.app.ActivityThread.main(ActivityThread.java:5826) |
| 218 | + at java.lang.reflect.Method.invoke(Native Method) |
| 219 | + at java.lang.reflect.Method.invoke(Method.java:372) |
| 220 | + at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1009) |
| 221 | + at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:804) |
| 222 | + |
| 223 | + |
| 224 | +``` |
| 225 | + |
| 226 | + can be seen ` at com.wanjian.cockroach.Cockroach$1.run(Cockroach.java:47)` no interception, and APP crash. |
| 227 | + |
| 228 | + |
| 229 | +### Note |
| 230 | + |
| 231 | +* An exceptionHandler.handlerException (Thread thread, Throwable throwable) is called when the main thread or child thread throws an exception. |
| 232 | +* ExceptionHandler.handlerException may run in a non-UI thread. |
| 233 | + |
| 234 | +* HandlerException internal advice manually try {your exception handling logic} catch (throwable e) {}, in case handlerException internally throws an exception again, causing the loop to call handlerException |
| 235 | + |
| 236 | +* If you set the Thread.setDefaultUncaughtExceptionHandler, you may not be able to catch a child thread exception. |
| 237 | + |
| 238 | +Although you can catch all the exceptions, but may lead to some strange problems, such as view initialization occurred when the exception, abnormal code behind the implementation of the implementation, although not |
| 239 | +Will lead to app crash But the view has been a problem inside the run, there will be very strange when the phenomenon. |
| 240 | + |
| 241 | + |
| 242 | +Although it will lead to a variety of strange problems, but can maximize the normal operation of APP to ensure that many times we hope that the main thread even if the exception does not affect the normal use of the app, such as we |
| 243 | +To set the background color for a view, because the view is null will lead to app crash, like this problem we hope that even if the view can not set the color and do not crash |
| 244 | +When Cockroach can meet your needs. |
| 245 | + |
| 246 | +HandlerException (final thread thread, final Throwable throwable) Internal advice Ask your server to decide how to handle the exception, |
| 247 | +Directly ignore or kill APP or other operations. |
| 248 | + |
| 249 | + |
| 250 | +Cockroach using the android standard API written, no reliance, light enough to light to only less than 100 lines of code, generally there will be no compatibility issues, there is no performance on the issue, can be compatible with all android version. |
| 251 | + |
| 252 | +Has been uploaded to jcenter, compile 'com.wanjian:cockroach:0.0.5' |
| 253 | + |
| 254 | +Effect video [http://weibo.com/tv/v/EvM57BR6O?fid=1034:40b2f631632f0cf2a096a09c65db89ad](http://weibo.com/tv/v/EvM57BR6O?fid=1034:40b2f631632f0cf2a096a09c65db89ad) |
0 commit comments