Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Monitoring Errors in Android Apps

DZone's Guide to

Monitoring Errors in Android Apps

Learn how to trace, monitor, and analyze errors in your Android-native mobile applications using Rollbar’s Android SDK.

· Performance Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

When developing mobile apps, it’s important to monitor errors so that you can understand your user’s experience. You need deeper insight than just a crash report because errors could cause a degraded user experience or a drop in key behavioral metrics. Your team needs to know quickly when there are production problems either with the app itself or with your backend services so you can fix the issue before more customers are impacted.

Rollbar’s Android SDK lets you track and analyze errors that happen in your Android native applications, and even trace problems to backend services and third-party APIs. It provides you with a live error feed from your application, including complete stack traces and contextual data to debug errors quickly. We also track the environment the error is coming from (prod or staging), the server that generated the error, and even the user’s session data. You can then quickly assign ownership of errors to your team and track when they are fixed. Learn more about Rollbar’s product features for Android.

Below, you can see that we've created an example app that triggers an exception when the user clicks on a button. The error message is tracked in Rollbar, including a stack trace where you can see the line of code that caused the error. Rollbar captures errors that occur anywhere in the app. You can follow along with our example using the source code on GitHub.

Gif of Rollbar android example

Adding Rollbar to Your Code

  1. Visit https://rollbar.com and sign up for an account if you haven’t done so yet. Next, create your project and select Other from the list of notifiers. Select the client side access token that is generated for you. You’ll need this to configure Rollbar in the steps below.
  2. Open the app.gradle of your project, and add the following dependencies.
compile('com.rollbar:rollbar-java:1.1.0')
compile('com.rollbar:rollbar-android:1.1.0@aar')
  1. Add the project access token from step 1 in your app manifest file under the application section. You can find the manifest file in your project directory.
<meta-data android:name="com.rollbar.android.ACCESS_TOKEN" android:value="ACCESS_TOKEN" />

The app does not provide you the meta-data section by default, so you need explicitly to add the Rollbar 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.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Rollbar.init(this);   
}

Test Rollbar With an Example Android App

To test that it’s working, let’s create an activity that will generate an error message. In the example below, you’ll be able to generate an error by clicking the "Generate an error" button.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="rollbar.com.rollbarexample.MainActivity">
    <Button
        android:layout_margin="30dp"
        android:layout_centerInParent="true"
        android:text="Generate an error"
        android:id="@+id/clickMe"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</RelativeLayout>
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);

       button=(Button)findViewById(R.id.clickMe);
       Rollbar.init(this);
       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
             throwError();
           }
       });
   }

   private void generateError(){
       String test=null;
       test.toString(); // This will throw null pointer exception
   }
}

When you click the "Generate an error" button, it will trigger the GenerateError method. In this method, we have added a bug which is throwing a null pointer exception.

Viewing Errors in Rollbar

Open Rollbar to see what these errors look like on your account’s item page. The error we just generated should be called

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()'.

Image title

Get more details by clicking on the item. You can now see a traceback showing you the exact source code file, method, and line number that generated the error.

Rollbar also provides a lot of other contextual information that will help you troubleshoot and debug problems faster. Along the row of sub-tabs above you can get extra drill-down information on each individual occurrence and how many times it occurred, the people affected in client-side apps and how many people were impacted, and much more. You can use this to keep an eye on the error rate and drill down to troubleshoot high-impact errors.

It’s pretty easy to get error tracking across your entire app thanks to Rollbar’s Android SDK. It only takes a few minutes to set up, and you will have way more context to track and debug problems faster in the future. You’ll know about errors right away so your users can experience your app the way it was meant to be.

If you haven’t already, sign up for a 14-day free trial of Rollbar and let us help you take control of your web and mobile application errors.

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
rollbar ,monitoring ,mobile ,performance ,android

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}