Working with Android Crash Report Analytics

Android Crash Report Analytics is a free and open source android library which helps android mobile applications to Email their Crash reports or post the crash reports to its reporting server.  Which is really helpful for developers to identify the the reason for the application’s crash.

 

1) Add the ACRA (Current version is 4.9.2) library to the project.

Simple way to activate the ACRA to your Android Studio Gradle project is, Add the following line to the [code]build.gradle[/code] of the application module. to get Maven or other links for the ACRA Library click here.

[code]

   compile
'ch.acra:acra:4.9.2'

[/code]

 

2) Extend Application Class & override ‘attachBaseContext’ method

 

[code]

import android.app.Application;
import android.content.Context;

import org.acra.ACRA;
import org.acra.ReportField;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;

@ReportsCrashes(mailTo = "zumry@appsgit.com",
        mode = ReportingInteractionMode.TOAST,
        resToastText = R.string.crash_toast_text)
public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        // The following line triggers the initialization of ACRA
        ACRA.init(this);
        System.out.println("application initialized  ACRA");
    }
}

[/code]

 

3) Force the app to throw exception and send mail to the application author

Add a button the main activity and add a click method.

 

activity_main.xml file is,

 

[code]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="actratest.appsgit.com.acra_test.MainActivity">

    <Button
        android:text="Crash The App"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="187dp"
        android:id="@+id/button"
        android:onClick="onButtonClick" />
</RelativeLayout>

[/code]

 

MainActivity.java file is as follows,

[code]

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void onButtonClick(View Button) {

Toast.makeText(this, “Button Clicked”, Toast.LENGTH_SHORT).show();

int x = 1/0; // this line throws java.lang.Arithmetic Exception
}
}

[/code]

 

Your main activity would be like,

 

Android Crash Analytics Report

 

You are done.! When you press the button, you should get an email popup and the email contents would be like this,

 

[code]

USER_COMMENT=
ANDROID_VERSION=4.4.2
APP_VERSION_NAME=1.0
BRAND=Huawei
PHONE_MODEL=T1 7.0
CUSTOM_DATA=
STACK_TRACE=java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4449)
at android.view.View$PerformClick.run(View.java:18483)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
… 11 more
Caused by: java.lang.ArithmeticException: divide by zero
at actratest.appsgit.com.acra_test.MainActivity.onButtonClick(MainActivity.java:21)
… 14 more
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4449)
at android.view.View$PerformClick.run(View.java:18483)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArithmeticException: divide by zero
at actratest.appsgit.com.acra_test.MainActivity.onButtonClick(MainActivity.java:21)
… 14 more
java.lang.ArithmeticException: divide by zero
at actratest.appsgit.com.acra_test.MainActivity.onButtonClick(MainActivity.java:21)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4449)
at android.view.View$PerformClick.run(View.java:18483)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
at dalvik.system.NativeStart.main(Native Method)

[/code]

 

I will update the post with more information about how to send the analytics report to the server.

 

Thanks,

Zumry

 

 

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.