Hal terpenting yang harus dilakukan dalam pengembangan mobile apps ketika memasuki tahap testing adalah, bagaimana mengelola error reporting dengan baik. Tujuannya adalah agar setiap bug dapat segera dilaporkan dan kemudian dengan cepat diperbaiki. Di platform Android hal ini sangat terasa sekali karena banyaknya vendor device dengan behaviour yang berbeda-beda sehingga dapat terjadi bug yang berbeda pula. Bisa jadi di satu device tidak terjadi, tapi di lain device terjadi error.
Tujuannya adalah agar setiap bug dapat segera dilaporkan dan kemudian dengan cepat diperbaiki.
Error yang relatif mudah di tangani secara teknis adalah error yang masuk ke dalam Exception, bukan logic error atau flow error sehingga kita dapat melihatnya dengan memasang Log.e(). Tapi masalahnya adalahException yang tidak tertangkap oleh blok try catch dan terlebih ketika apk sudah berada ditangan Tester dan terjadi error yang berbeda dengan device yang kita gunakan. Sehingga jalan terbaiknya adalah menampilkan Log.error ke dalam Activity. Nah, itu yang akan kita bahas.
- Setiap exception yang tidak tertangkap, akan dilarikan ke dalam method UncaughtExceptionHandler. Jika ingin membuat behaviour yg berbeda, maka kita tinggal meng-implements darinya.
01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465package com.research.hangga.simplecrashreport;import android.app.Activity;import android.content.Intent;import android.os.Build;import android.util.Log;import java.io.PrintWriter;import java.io.StringWriter;/*** Created by hangga on 11/03/16.*/public class MyExceptionHandler implementsjava.lang.Thread.UncaughtExceptionHandler {private final Activity context;private final String LINE_SEPARATOR = "\n";public MyExceptionHandler(Activity activity) {this.context = activity;}@Overridepublic void uncaughtException(Thread thread, Throwable ex) {StringWriter stackTrace = new StringWriter();ex.printStackTrace(new PrintWriter(stackTrace));StringBuilder builder = new StringBuilder();builder.append("************ CAUSE OF ERROR ************\n\n");builder.append(stackTrace.toString());Log.e("ERROR", ""+stackTrace.toString());builder.append("\n************ DEVICE INFORMATION ***********\n");builder.append("Brand: ");builder.append(Build.BRAND);builder.append(LINE_SEPARATOR);builder.append("Device: ");builder.append(Build.DEVICE);builder.append(LINE_SEPARATOR);builder.append("Model: ");builder.append(Build.MODEL);builder.append(LINE_SEPARATOR);builder.append("Id: ");builder.append(Build.ID);builder.append(LINE_SEPARATOR);builder.append("Product: ");builder.append(Build.PRODUCT);builder.append(LINE_SEPARATOR);builder.append("\n************ FIRMWARE ************\n");builder.append("SDK: ");builder.append(Build.VERSION.SDK);builder.append(LINE_SEPARATOR);builder.append("Release: ");builder.append(Build.VERSION.RELEASE);builder.append(LINE_SEPARATOR);builder.append("Incremental: ");builder.append(Build.VERSION.INCREMENTAL);builder.append(LINE_SEPARATOR);Intent intent = new Intent(context, ActivityShowError.class);intent.putExtra("error", builder.toString());context.startActivity(intent);android.os.Process.killProcess(android.os.Process.myPid());System.exit(10);}}
- Siapkan Activity untuk menampilkan error.
0123456789101112131415161718package com.research.hangga.simplecrashreport;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.TextView;public class ActivityShowError extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_activity_show_error);TextView txtError = (TextView) findViewById(R.id.txtError);<span style="color: #000080;">String error = getIntent().getStringExtra("error");int a = error.indexOf("Caused by");txtError.setText(error.substring(a));</span>}}
- Pasang di setiap Activity, atau akan lebih mudah jika anda memiliki parent class Activity, tinggal pasang disana.
012345678910111213141516171819202122232425private int mNotifCount = 0;private LayerDrawable icon;private GoogleApiClient client;@Overrideprotected void onCreate(Bundle savedInstanceState) {<span style="color: #000080;"><strong>Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this));</strong></span>super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(toolbar);FloatingActionButton fab = null;// = (FloatingActionButton) findViewById(R.id.fab);fab.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();setBadgeCount(icon, mNotifCount++);}});// ATTENTION: This was auto-generated to implement the App Indexing API.// See https://g.co/AppIndexing/AndroidStudio for more information.client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();}
Tinggal copy paste, lalu kirim ke developer.
Hello! My name is AnnaMarkova, our company need to advertise on your website. What is your prices? Thank you. Best regards, Mary.
Iklan mbayar gan.. ha3x..
Find out what you need to improve in yourself to become more effective!
Woah! I’m really loving the template/theme of this website.
It’s simple, yet effective. A lot of times it’s very hard to get that
“perfect balance” between usability and appearance.
I must say you’ve done a amazing job with this. Also, the blog loads
extremely fast for me on Firefox. Exceptional Blog!
Awesome post.
Hello i am kavin, its my first occasion to commenting anywhere,
when i read this paragraph i thought i could also create comment due
to this good piece of writing.
This is the perfect blog for anybody who wants to understand this topic.
You understand so much its almost hard to argue with you (not that I actually would
want to…HaHa). You certainly put a brand new spin on a topic that has been written about for a long time.
Great stuff, just great!
bagaimana mengelola error reporting dengan baik.
Kalo sekarang mending pake crashlytics. Firebase punya.