From my search, i got the below code for getting the Crash Log .
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null)
{
log.append(line);
}
But where do i add this code, so that i should get the crash report whenever my app crashes .
Also i want to email it or send to the server, but after the app getting crashed, how to call the action to send email/HTTP post method .
Please advise and thanks in advance .
The best way to handle crash logs is creating an UncaughtExceptionHandler
and handling it as per your requirement. Create a BaseActivity
class and extend all the Activities with that and put this code stuff in the BaseActivity
class.
private Thread.UncaughtExceptionHandler handleAppCrash =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("error", ex.toString());
//send email here
}
};
Then just enable is inside onCreate()
method of your BaseActivity
by using
Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);
So, now whenever there will be a crash in your Application uncaughtException()
will be called and you will have to handle the crash accordingly.
I recommend you use ARCA https://github.com/ACRA/acra.
Include arca in your build.gradle--it uses the apache 2.0 license as of 10/29.
compile 'ch.acra:acra:4.9.0' //TODO: Apache 2.0 license https://github.com/ACRA/acra
In your class that extends Application, drop this on top of the "class" declaration.
@ReportsCrashes(mailTo = "[email protected]",
customReportContent = { ReportField.APP_VERSION_CODE, ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL, ReportField.CUSTOM_DATA, ReportField.STACK_TRACE, ReportField.LOGCAT },
mode = ReportingInteractionMode.TOAST,
resToastText = R.string.resToastText) //you get to define resToastText
public class MyApplication extends Application {
Then override the following method from the same Application class like so:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// The following line triggers the initialization of ACRA
ACRA.init(this);
}
In my case I have a bug that i cant replicate on my phone I just want the stack trace back from a lone tester. The simplest way I could find to do this was to get it copied into the users clipboard and ask them to send it to me here is the code:
import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* Copies the stack trace the exception that is causing your application to crash into the clip board.
* Ask your testers to paste it into an email / text message to you.
*
* @author Stuart Clark
*/
public class CrashDebugApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable e) {
// Get the stack trace.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
// Add it to the clip board and close the app
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Stack trace", sw.toString());
clipboard.setPrimaryClip(clip);
System.exit(1);
}
});
}
}
Then set the android:name
property in Android Manifesto i.e.
<application android:icon="@mipmap/ic_launcher" android:name=".CrashDebugApplication">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With