bugsnag-unity
bugsnag-unity copied to clipboard
Notify handled android java exceptions
Description
In our Unity game, we have a lot of android plugins that need to report exceptions.
Since we use the Unity SDK, there is no obvious way to report these java/kotlin exceptions.
Describe the solution you'd like
Any way to send a java/kotlin exception to the bugsnag dashboard.
A simple api could be: public void Notify(AndroidJavaObject jvmException)
Describe alternatives you've considered
Calling Bugsnag.Notify with stacktrace string
Bugsnag.Notify(new Exception("<my android stacktrace>"))
Result: I think the message is not retraced? (class names will show a.b.c.d)
Calling the bugsnag android SDK via reflection:
package com.demo.util
import android.content.Context
import android.util.Log
import com.unity3d.player.UnityPlayer
import java.lang.Exception
import java.lang.reflect.InvocationTargetException
object BugsnagTest {
@JvmStatic
fun reportException() {
val clazz = Class.forName("com.bugsnag.android.Bugsnag")
// required, otherwise notify() will throw an exception
val bsgInit = clazz.getDeclaredMethod("init", Context::class.java)
safeInvoke {
bsgInit(null, UnityPlayer.currentActivity)
}
val bsgNotify = clazz.getDeclaredMethod("notify", java.lang.Throwable::class.java)
val ex = Exception("hello")
safeInvoke {
bsgNotify(null, ex)
}
}
private fun safeInvoke(block: () -> Unit) {
try {
block()
} catch (ex: InvocationTargetException) {
Log.wtf("BugsnagTest", ex.cause)
} catch (ex: Throwable) {
Log.wtf("BugsnagTest", ex)
}
}
}
Result: This does work.. but any native crash that happens after this (e.g. SIGTRAP) is not reported to bugsnag.
Hey @troy-lamerton As you have seen, accessing the Unity notifier to manually notify on handled java exceptions is not currently supported. We are however looking at alternative ways to do this. At the moment we don't have a definite timeline but we will post here with any updates.
Hey @johnkiely1, I would like to know if the Unity notifier only reports exceptions on Unity scenes. It the game has portions of code that are native are exceptions reported? Would it be possible to use two integrations on the same project? ( https://docs.bugsnag.com/platforms/android/ and https://docs.bugsnag.com/platforms/unity/) Thanks
Hey @interpegasus,
Unhandled native exceptions are captured when using the Unity notifier. This applies to Android iOS and MacOs. This would occur by default once you have integrated Bugsnag into your application. https://docs.bugsnag.com/platforms/unity/#reporting-unhandled-errors
The Android/iOS/macOs notifiers are used under the hood by the Unity notifier to do this.
We have a solution internally and are testing it at the moment. So this is lower priority for us now.
Solution goes like this:
-
Log.getStackTraceString
to serialize a Java/Kotlin Throwable to a string - Send string to Unity
- Use
new AndroidJavaException()
to create a C# exception from the Throwable stacktrace string - Notify bugsnag of this exception, just like any other C# exception
Code snippet for steps 1 and 2:
@AnyThread
@JvmStatic
fun reportPluginError(ex: Throwable) {
// getStackTraceString: exception class, message and stacktrace
val readableException = Log.getStackTraceString(ex).trim().replace("\t", " ")
UnityPlayer.UnitySendMessage("NativePluginListener", "ReportNativeWarning", readableException)
}
Would be good to keep this issue open to track implementing the feature in the Bugsnag source code.