fix(android): Avoid using AndroidJavaObject from background threads #161
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Goal
Work around an issue present in Unity 2017/2018 where instantiating
AndroidJavaObject
or usingAndroidJavaObject.Call()
(event indirectly usingAndroidJNI
helpers) crashes when called from a background thread on x86. This change removes use of JNI APIs when on background threads and running on x86 Android.Changeset
Added a reference to the main thread to the Android
NativeInterface
class, which is checked by functions before invoking the JNI, exiting early if the current thread is not the main thread.Tests
Tested manually in Unity 2018 / Unity 2017 by calling
Notify()
, logging, leaving breadcrumbs, and causing unhandled crashes from C#, Java, and C++ code and verifying:Notify()
/ report a crash does not crash the app on background threads on Android x86 (though no report is sent)The crash is known to affect:
The Android x86 target no longer builds on 2019.2 (possibly related to its deprecation), and the target is removed altogether in 2019.3 (announcement, release notes).
Builds
Bugsnag.unitypackage.zip
Bugsnag-with-android-64bit.unitypackage.zip