My apps crash all the time during development (thanks to plethora of those pesky NullPointerExceptions).
Okay, probably not the best way to market myself BUT I do fix those bugs before the build is released to production (or alpha, or beta, whatever stage the app is currently in development). One thing I wanted to cover in this post is a neat little trick that I learned recently on how to keep crash logs during development.
Most of the time, when apps crash while running in Android Studio, you can see those little red error messages in the Android Monitor.
These logs tell you exactly what happened and where so that you can go and fix it. Pretty easy right?
But this is only applicable if you app is in a crashed state. If your app restarts automatically thanks to the Android OS trying to save face, these logs will be reset and your error messages will be gone, leaving you unable to figure out what went wrong in your codebase.
The way I used to figure out where my app went wrong was like playing a stealth video game like the old Metal Gear Solid games on the original PlayStation where you had to sneak up on your enemies to kill them. I would literally have my screenshot tool ready, open the app in my physical device with the Android Monitor open to full screen, replicate the steps necessary to crash the app, and as soon as the app crashed, try to time my screenshot so that I can capture the error messages before Android Studio erases them from my view. Yes, super primitive and a silly way to go about it.
I can’t remember why (I read it somewhere awhile ago)… but apparently the reason Android Studio wipes the logs when the app restarts is actually a feature that’s supposed to make your development life easier. But if you’re in a situation where you want to keep your logs for whatever reason (i.e. being able to see your error logs), you can create a custom filter configuration in your Android Monitor that won’t erase your previous logs even if your app restarts.
In the top right section of your Android Monitor, click the dropdown and create a new Logcat Filter.
Name your Filter whatever you want, and simply select that filter when trying to replicate the crash. You’ll see that your logs won’t be erased even when the app doesn’t restart.