Common Android exceptions and how to debug them with Raygun

| 4 min. (678 words)

Java and Android offer a rich and powerful set of tools for creating compelling mobile apps. The development of those apps can be quite productive these days, with modern tools like Android Studio, Gradle and Git. When it comes to releasing your app onto Google Play Store, however, the software development lifecycle dictates the job is only beginning. The maintenance phase is the most critical, and has the highest associated costs for fixing bugs that aren’t identified earlier during implementation or testing.

Fortunately, error tracking tools like Raygun can help catch runtime errors that slip through the QA process, and alert you when your app crashes for your users. Often, they’ll be quick to delete it from their devices, but you can save a customer and potentially negative reviews by informing them when it’s fixed, without them having to lift a finger. It doesn’t take much to amaze users with great support, who will then recommend your app to their friends and family without hesitation. Much better than a bad review for a buggy app!

There are many common Android exceptions that Raygun and its open-source provider Raygun4Android can alert you to when they occur on your user’s devices, and what follows is a list of the most critical.

OutOfMemoryError

This error can happen at runtime when you ask Dalvik to allocate a large amount of memory on the heap – for instance when trying to add a large bitmap into a container like an ImageView. If you’re pulling the bitmap from a remote endpoint you need to be sure you’re guarding against files, as these might cause the app to crash if this exception isn’t handled. There are configuration flags available such as android:largeHeap in the AndroidManifest if you do need to allocated large objects.

Application Not Responding

This very common dialog is caused by the system killing unresponsive applications that have hung on a long-running operation. This could occur when a network I/O connection to a third-party is unresponsive, perhaps due to unreliable connectivity, and many other such conditions. It is critical you keep the UI thread buttery smooth by offloading all long-running tasks onto an appropriate function, such as AsyncTask, or the classic Thread class if you need more control.

NullPointerException

If I had a dollar for every one of these I’ve seen..

It’s pretty easy to advise null guards around every variable access, but there’s always one degenerate case that slips though. As above, if these aren’t handled correctly higher up, the app will crash. Raygun4Android will catch these at the highest level possible, making sure you know where the holes in your app are, so you can fix them without your users needing to file a potentially unhelpful bug report.

StackOverflowError

You might run into this one which indicates infinite or deep recursion when creating nested layouts programatically. Ideally these will be caught during implementation, but in the case one slips through to the public release, Raygun will catch these.

Other common Java exceptions

You’ll also come across the standard set of built-in Java exceptions, depending on your problem domain, how your app is constructed and what libraries you are using – including InterruptedException, IOException, various subclasses of RuntimeException, ArithmeticException, and a whole bunch more. The good thing is, you don’t just need to rely on the compiler and the testing phase to ensure an app that is fit for purpose. Raygun exists as a constant monitor of code in the wild, that allows you to fix exceptions as they occur (and they always do).

Do you already have a Raygun account, are using it with a server-side language and would like to integrate it with your client Android apps? You can get started by creating a new application in Raygun and following the instructions. If you don’t yet have an account, you can start your 14-day free trial here (no credit card needed).

If you have any questions about how Raygun can help track errors in production Android apps, let us know in the comments below. Good luck with the error blasting!