Blog Android iNextrix

Prevent App crashing in Android using ACRA


One truth for developing a mobile application is: there are so many constraints, for example, a hardware limitation (CPU, RAM, Battery, etc) which cause application crashing specifically in android application development. In any android app development, If your code design is not good enough then get ready for the most critical problem on earth: “Crash”.

According to a study,

Application Crashing is the most complained problem from mobile app user. and furthermore, if an application crashes more than 3 times in a row, about half of users will remove it from their phone.

To overcome this issue,We need to setup Crash Tracking System, which collects every single detail of crash directly from user’s device and sent it to the developer. In the crash tracking system, whenever the application crashes, the whole stacktrace will be sent via email address which allows a developer to fix every problem in the easiest manner. With this method, you would be able to deliver a Crash-Free Application in very short time. And it will lift up individual or organization to become top android application development company. You can also hire android developers from our company we will help you to work on your app concept.

As a mobile application development company we prefer to use ACRA. ACRA is one of the solutions for to deliver a Crash-Free Application. ACRA stands for Application Crash Reporting on Android (ACRA). ACR is an android library to simplify crash detection while your app is under development and crash will be automatically detected by the library then app tester/user can easily send the crash report on one click to the developer and it will help him/her to fix the issue. It’s a free library which solves the ‘manual error reporting’ problem with a few lines of code. After you’ve implemented the library and if everything goes well, you will be able to extract the same Android error logs as the Google default automatically and without requiring the user to take action.

Basic steps to implement ACRA in android project:

  1. Add dependency in your application
  2. Configure application class programmatically
  3. Declare application class in Android Manifest

1. Add dependency : 

  • ACRA is available on Maven central and can easily be integrated as a dependency of your project using your favorite build system.
  • Gradle  – compile ‘ch.acra:acra:4.9.0’
  • Maven

<dependency>
<groupId>ch.acra</groupId>
<artifactId>acra</artifactId>
<version>4.9.0</version>
<type>aar</type>
</dependency>

2. Configure application class : 

If you don’t already have an Application class, then create one. Apply the configuration to your Application class.

Steps to create Application class : 

  1. Create new application class in your root project
  2. Set it the name like “MyApplication” which extending from android.app.Application or subclass of that
  3. Update your application element in you Android Manifest

Configure ACRA declaratively : 

  • Add a @ReportsCrashes annotation to your Application class and then override the attachBaseContext() method to add ACRA.init(this);
  • Created class will be looks like:

import org.acra.ACRA;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;
import android.app.Application;

@ReportsCrashes(mailTo = “test@gmail.com”, mode = ReportingInteractionMode.TOAST, resToastText = “Crash report sent to developer”)
public class MyApplication extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
ACRA.init(this);
super.onCreate();
}
}

Here there are 4 modes available for reporting :

  • Toast : Display a simple Toast when the application crashes
  • Dialog : Display a report confirmation dialog right after the crash
  • Notification : Display a status bar notification when the application crashes
  • Silent : No interaction, reports are sent silently and a “Force close” dialog terminates the app.

3. Declare application class in Android Manifest :

  • AndroidManifest needs to have the following modified
  • Declare your application class in application element
  • Use must have to declare internet permission to send the report by email

Configure Android Manifest :

  • In Android Manifest, There is application element available. You need to configure android:name attribute to point to your application class

<application
android:name=”MyApplication”
android:icon=”@drawable/app_icon”
android:label=”@string/app_name”>

  • Also add Internet permission in your Android Manifest

<uses-permission android:name=”android.permission.INTERNET” />