How to report exceptions in android project automatically to Firebase? - android

I've seen the option of automatic crash reports to firebase. But what about regular try/catch exceptions? Is there any way to set automatic logs to Firebase on each caught exception in my android app?

Once you use the Firebase Crash Reporting SDK in your app, uncaught exception are automatically reported to the server.
To report an exception that you catch, cal FirebaseCrash.report:
...
}
catch (IOException ex) {
FirebaseCrash.report(ex);
}
I highly recommend reading the Firebase Crash Reporting documentation. It's pretty much a single page and covers the entire API and many common use-cases.

Related

How to report Silent Crashes in Flutter

Our app uses Firebase Crashlytics and Sentry to keep track of all the crashes. We have covered all of our dart code with crash handling but have noticed that in some cases the crashes come from native code, which means that they are not caught by our catchers.
For example, we recently had an issue with one of our images accidentally getting published in very high res causing memory issues and the app to crashed without any reports.
We were to able to know about it and fix it because of feedback from the users and the fact that it was easily reproducible, but we'd rather have some sort of reporting in place for such crashes.
Firebase Crashlytics can pick up those errors too.
Follow all steps on the package setup guide.
https://pub.dev/packages/firebase_crashlytics
quoting the guide
Overriding FlutterError.onError with Crashlytics.instance.recordFlutterError will automatically catch all errors that are thrown from within the Flutter framework.
If you want to catch errors that occur in runZoned, you can supply Crashlytics.instance.recordError to the onError parameter:
runZoned<Future<void>>(() async {
// ...
}, onError: Crashlytics.instance.recordError);
Have you tried catching NDK crashes? Crashlytics supports it

crashlytics custom crash reporting not showing on Firebase Console

so I'm using this tutorial https://firebase.google.com/docs/crashlytics/customize-crash-reports to enable crashlytics crash report in my Android App. I noticed that Custom Crashes never appears on my Firebase Console using this code:
Crashlytics.log(Log.DEBUG, "tag", "message");
After setting my UserIdentifier.
But when I tried Forcing a crash like this:
Crashlytics.getInstance().crash(); // Force a crash
it appears on my Dashboard. So why is my Custom Crash missing.
When I switched back to custom logging, It never works. I also tried using LogException, that didn't work as well.
Crashlytics.log(Log.ASSERT, TAG, Log.getStackTraceString(e));
Crashlytics.logException(e);
Please note that It's Firebase console I'm using.
Mike from Firebase here.
Crashlytics.log(Log.DEBUG, "tag", "message"); will log information into a crash, but the logs are only sent and processed if a crash or non-fatal exception happens in the same session of the app.
Writing the log is done async, and we focus on capturing crashes or non-fatal exceptions over writing the log to disk. If the log happens just before a crash or non-fatal as in this example:
Crashlytics.log(Log.ASSERT, TAG, Log.getStackTraceString(e));
Crashlytics.logException(e);
It's possible we didn't have time to write the log before the exception happened. In that case, we'd prioritize capturing the exception believing that getting the exception or crash is more important then a log.

How to find which level causes a crash in Android

I have a small game which has a few levels. In the google play console, I am getting crashes in the Game-Fragment but it's hard for me to find which level is causing the crash. The first few levels look okay and tested. I am just wondering what is best/standard approach to get the crash along with the level ID/some more information.
There are different good libraries that allow you to improve logging of the exceptions, one of them is Crashlytics from Google.
With Crashlytics, you can catch exceptions that are happening with following code and log it(and include custom parameters in the message variable):
try {
methodThatThrows();
} catch (Exception e) {
Crashlytics.log(priority, tag, message);
// additional handling of your exception
}
You can improve your logging this way to include all kind of helpful information. See documentation page for details:
https://firebase.google.com/docs/crashlytics/customize-crash-reports

firebase android functional setup

Added firebase to my android marshmallow application and have run the application with the recommended FirebaseCrash.report(new Exception("")) to verify the setup and am getting the report in my firebase console.
Questions due to the developer guide being a bit unclear:
do I need to add a firebase.log or firebase.logcat to each of the caught exceptions in my application in order to get report entries for them. If so, which is recommended? Should I use firebasecrash.report instead?
Is there any additional work needed to capture uncaught exceptions generated
In the evaluation phase at this point and would prefer not to make a lot of code changes for something that might not work out.
From the documentation of Firebase Crash Reporting (I added numbers to correlate them to your questions):
Firebase Crash Reporting automatically generates reports for fatal errors (or uncaught exceptions) (2). However, you can also generate reports in instances where you catch an exception but still want to report the occurrence (1).
So:
if you want to report caught exception you have to report them yourself with FirebaseCrash.report().
uncaught exceptions are reported automatically.

Trouble with logging my data with crashlytics

I'm trying to get logs with some service data with Crashlytics in my android application. But I don't see my logs in dashboard.
I used this:
String myLog = getServiceData(); //myLog is not null and non-empty
CrashLytics.log(myLog);
and this:
String myLog = getServiceData(); //myLog is not null and non-empty
CrashLytics.log(Log.Error, getString(R.string.app_name), myLog);
I tried to generate exception in my application and handle it, but have no results:
try {
int a = 0;
a = 1/a;
}
catch (Exception e) {
CrashLytics.log(myLog);
}
Also I read on Crashlytics log not sent I need to initialize crashlytics before log data. I put Crashlytics.start(this) in onStart() event of my Activity but didn't see my logs in dashboard again. At last I tried to put Crashlitycs.start(this) directly before logging my data, but still have no logs in dashboard.
Plase, tell me what I do wrong and how to get my custom logs in Crashlytics dashboard?
I had a similar situation. With some experimenting, I was able to deduce the rules to Crashlytics' behavior.
I'm sharing my learnings here so (hopefully) others don't have to go through the arduous and time-consuming process that I did to figure it out.
Crashlytics will only upload a crash report "to the dashboard" immediately if a fatal exception occurs. In other words, when your app crashes. And nothing shows in the dashboard unless and until a crash report is uploaded.
If you log a non-fatal exception, using CrashLytics.logException(e), a crash report will not be uploaded till the next time your app is restarted. So you will not see the exception in the Crashlytics dashboard till an app restart.
You can tell when an upload occurs because you'll see this sort of message in LogCat:
07-17 19:30:41.477 18815-18906/com.foo.bar I/Crashlytics﹕ Crashlytics report upload complete: 55A9BA0C01D7-0001-462D-B8B4C49333333.cls
A Crashlytics log message must be associated with a fatal or non-fatal exception to show up in the dashboard.
Furthermore, log messages that aren't associated with an exception do not survive app restart.
So, if you do something like log a few messages, then restart the app, then the app throws an exception, or it logs a non-fatal exception using Crashlytics.logException(), the log messages will be lost. They will not show up in the dashboard.
If you want to log some messages without a fatal exception, use one or more Crashlytics.log() statements followed by Crashlytics.logException().
To verify that it works, have the code run, then restart the app. In the dashboard, you should see the logs associated with the issue created for the non-fatal exception. In the wild, you'll just have to trust your users restart the app with some regularity.
On the Fabric/Crashlytics dashboard, you'll need to select All Events or (if you want to see just your logging calls) Non-Fatals.
according to Crashlytics knowledgebase:
Logged messages are associated with your crash data and are visible in
the Crashlytics dashboard if you look at the specific crash itself.
And from my experience this seems true. However I am unsure as to what determines which logging is associated with a crash report. Perhaps a time window (time around crash) or a limited number of logs (before the crash) is associated with the crash report?
However Crashlytics knowledgebase does say that exceptions can be logged:
All logged exceptions will appear as "non-fatal" issues in the
Crashlytics dashboard.
So if you changed your try/catch to:
try {
int a = 0;
a = 1/a;
}
catch (Exception e) {
CrashLytics.logException(e);
}
then it should show up in the Crashlytics dashboard.
This is an old question but since I was having the same problem today, I figured I should post my solution (it's in Kotlin though).
With Crashlytics now a part of Google's Firebase offering, it initializes behind the scenes, so a solution I wrote a few years ago had to be updated to this:
Implement some kind of log caching, First In First Out. I put one together based on cache-lite.
Put this function in YourApplicationClass that extends Application:
fun setExceptionHandler() {
val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler { thread, ex ->
Crashlytics.log(cacheLoggingTree.getAll().joinToString("++"))
Crashlytics.logException(ex)
defaultExceptionHandler.uncaughtException(thread, ex)
}
}
Then you call the function from the end of your onCreate function in your MainActivity:
(application as YourApplicationClass).setExceptionHandler()
With that, any exceptions that get thrown will first post the most recent log entries, then log the exception. You probably want to be cautious with how many lines you cache though, lest you overload the system.
Also, I used ++ as a flag to manually replace with carriage returns when you download the log, since the \n I had gets stripped in the upload process.
Instead of catching the exception and logging it, you can instead use RuntimeExceptions:
throw new RuntimeException("Test crash");
Android Studio will not require you to catch these, thus having the app terminate and upload the report immediately.

Categories

Resources