Android

How to configure rollbar-java to work with your Android app

This plugin integrates Rollbar into your Android app using the Java & Android SDK. This guide will show you how to add a global exception handler to catch all uncaught errors in your Android app.

For an in-depth tutorial see our blog Monitoring Errors in Android Apps.

Installation

  1. Open your app.gradle of your project, and add the following dependencies.
implementation('com.rollbar:rollbar-android:1.+')

🚧

Gradle dependency versions

For a test application, using 1.+ will quickly pull the latest version of rollbar-android. In a production scenario it's good practice to include the full version, eg. 1.9.0. The latest version is always available on Maven Central.

  1. Log into your Rollbar account dashboard. Go to Settings → Project Access Tokens and then copy the token.

  2. Add the access token from step 2 into your manifest file's application section as shown below. You can find the manifest file in your project directory.

<meta-data android:name="com.rollbar.android.ACCESS_TOKEN" android:value="ACCESS_TOKEN" />

The manifest file looks like this after adding the meta-data section.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="rollbar.com.rollbarexample">

   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/AppTheme">
       <activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>

       <meta-data android:name="com.rollbar.android.ACCESS_TOKEN" android:value="YOUR_ACCESS_TOKEN" />
   </application>

</manifest>
  1. Open the code for your launcher activity, and then initialize Rollbar in the onCreate method.
import com.rollbar.android.Rollbar;

public class MainActivity extends AppCompatActivity {
  
    private Button button;
    private Rollbar rollbar;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Rollbar.init(this);
    }
}

📘

Initialization failures caused by manifest issues

In some cases, Rollbar will silently fail to initialize. This is caused by typos or other errors in the manifest.xml file, a temporary workaround would be to call the Rollbar.init() method with the access token as one of the parameters.

Usage

In the installation section, we created a global error handler to log the error in Rollbar. We also offer several methods to track other items at your convenience.

Logging a Specific Message

You can log your own messages anywhere in your app. For example, to log a debug message:

rollbar.debug("Here is some debug message");

Adding Context Informaton

You can pass user information as context like this:

rollbar.error("This is an error");
rollbar.setPersonData("123","John Doe","[email protected]");

Exception Logging

You can also report an error to Rollbar with a specific exception block. For example, to log any Exception add the following:

try {
   String test = null;
   test.toString();
} catch(Exception e) {
   rollbar.error(e,"This is a null pointer exception");
}

Setting the environment

To set the environment you need to modify the Rollbar.init call in your main activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String environment = "development";
        Rollbar.init(this, null, environment);
    }

The second argument is the access token, and should be left as null if it has been configured in the application's manifest file.

Detecting when the network is unavailable

By default rollbar-android will try to send errors to Rollbar even if the network is unavailable. Errors that cannot be sent will be discarded.

This behavior can be changed using the suspendWhenNetworkIsUnavailable option, which can be enabled during initialization:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        boolean suspendWhenNetworkIsUnavailable = true;
        // The second argument is the access token, and should be left 
        // as null if it has been configured in the application's manifest file.
        Rollbar.init(this, null, suspendWhenNetworkIsUnavailable);
    }

When this option is enabled, the notifier will try to detect if a network issue while reporting and error to Rollbar is due to the network being unavailable, and change its behavior as follows:

If it determines an error couldn't be sent to Rollbar because the network was unavailable, the error will be saved to be retried later.

Once an error couldn't be sent to Rollbar because the network was unavailable, it will suspend sending errors for a period of time, with the following rules:

  • If Android's ConnectivityManager class confirms the network is down, it will suspend sending errors until the ConnectivityManager notifies the Rollbar SDK that the network is back up, or until 5 minutes have passed, whichever comes first.
  • If the ConnectivityManager service is not available or it reports that the network is up, it will suspend sending errors for 1 second since it is likely network access has already been restored since the issue occurred.

Using Android's ConnectivityManager requires the ACCESS_NETWORK_STATE permission. While the suspendWhenNetworkIsUnavailable feature will degrade gracefully if this permission is not available, it is strongly recommended that the permission be requested by the application.

For further examples and information on using rollbar-android, check out the example app here.

📘

For more information on rollbar-java, please see the docs here.