dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.93k stars 532 forks source link

Binding libraries like Crashlytics use Throwable, which which leads to unusable logs #9442

Open DavidMarquezF opened 1 month ago

DavidMarquezF commented 1 month ago

Android framework version

net8.0-android

Affected platform version

Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.11.5

Description

I initially opened this issue in the AndroidX bindings but was told that here would be a better place for this issue. (see https://github.com/xamarin/AndroidX/issues/1011)

The main issue is that Crashlytics uses Java.Lang.Throwable. To convert our exceptions to this type we can use Java.Lang.Throwable.FromException. However, the resulting crash logs are pretty hard to read and use compared to when we were using AppCenter (to be deprecated in 2025). On top of that inner exceptions are not even displayed, so for some issues it's impossible to know what actually happened.

Given the deprecation of AppCenter and Firebase being the logical alternative (other than sentry), it feels like a big downgrade to not be able to get proper readable logs.

I believe this could improve a lot and make Crashlytics more useful if it was solved at the .net android level

Steps to Reproduce

  1. Create a new project and set up Firebase Crashlytics
  2. Throw a simple error -> This shows that the crashlytics error is usable but hard to understand, also firebase crashlytics groupping will not work very well in this case
  3. Create an inner exception like starting a task and throwing and error from inside (the inner exception is not logged and thus you loose crucial information)

Did you find any workaround?

You have some control over managed crashes by preparing the throwable a bit better, but unmanaged crashes are directly handled by Firebase, which doesn't leave us any way to improve it

Relevant log output

Example of AppCenter crash:

Xamarin Exception Stack:
System.Exception: Exception NavigateToFirstViewModel
  at x.AppStart.NavigateToFirstViewModel(Object hint)
  at MvvmCross.ViewModels.MvxAppStart.StartAsync(Object hint)
  at x.Droid.Activities.RootActivity.RunAppStartAsync(Bundle bundle)
  at x.Droid.Activities.RootActivity.OnResume()
  at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
  at Android.App.SyncContext.<>c__DisplayClass2_0.<Post>b__0()
  at Java.Lang.Thread.RunnableImplementor.Run()
  at Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)
  at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz)
--- End of inner exception stack trace ---
  System.NullReferenceException: Object reference not set to an instance of an object
    at x.Droid.UIManager.KeyboardHelper.EnableKeyboard(Activity activity)
    at x.Droid.MvxServices.UserInterfaceService.SetDisableKeyboard(Boolean disabled)
    at x.AppStart.NavigateToFirstViewModel(IAppStartStatusService appStartStatus, IMvxNavigationService navigationService, BusinessLogic dataService, IApplyUserSettingsService settingsService, IUserInterfaceService userSettingsService, IEventTracker eventTracker, String startName)
    at x.AppStart.NavigateToFirstViewModel(Object hint)

Same issue in Crashlytics:

Fatal Exception: android.runtime.JavaProxyThrowable: [System.Exception]: Exception NavigateToFirstViewModel
       at x.AppStart+<NavigateToFirstViewModel>d__6.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification()
       at MvvmCross.ViewModels.MvxAppStart+<StartAsync>d__5.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification()
       at x.Droid.Activities.RootActivity+<RunAppStartAsync>d__10.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification()
       at x.Droid.Activities.RootActivity+<OnResume>d__8.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Threading.Tasks.Task+<>c.<ThrowAsync>b__128_0()
       at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0()
       at Java.Lang.Thread+RunnableImplementor.Run()
       at Java.Lang.IRunnableInvoker.n_Run()
       at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V()
       at mono.java.lang.RunnableImplementor.n_run(SourceFile)
       at mono.java.lang.RunnableImplementor.run(SourceFile:1)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:223)
       at android.app.ActivityThread.main(ActivityThread.java:7680)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:423)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Looking at the whole crashlytics log, there is no reference at EnableKeyboard function, which is the one that threw the error

jpobst commented 1 month ago

This uses Java.Lang.Throwable.FromException where we perform stack trace translation. Perhaps there is a bug or enhancement here that could be done to provide a better trace?

jonpryor commented 1 week ago

We should investigate extending 1aa0ea7a8aef616d00e8467aa9eec83a1571cbd0 to include the Inner Exceptions.

The "problem"/"concern" is that the Java stack trace consists of StackTraceElements. Can we put in "arbitrary" text for the class and method name? Would it be possible to have a class or method name containing e.g. -- Begin inner exception …?