Search code examples
xamarin.androidmonovisual-studio-app-center

The type initializer for 'Android.Runtime.UncaughtExceptionHandler' threw an exception


my xamarin android app, which was working fine, started to crash on each launch with this exception.

The app is built from appcenter's build service, as it always has. If i build it locally in Debug or Release mode, it launches fine. Using a device with Android 7.

Did anyone encountered this exception and knows what to do ?
Ty !

18828 W monodroid: Calling into managed runtime init E mono : E mono : Unhandled Exception: E mono : System.TypeInitializationException: The type initializer for 'Android.Runtime.UncaughtExceptionHandler' threw an exception. ---> System.ArgumentNullException: Value cannot be null. E mono : Parameter name: method E mono : at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure, System.Boolean allowClosed) <0x56d026b4 + 0x00d74> in :0 E mono : at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) <0x56d03594 + 0x0001f> in :0 : at Android.Runtime.UncaughtExceptionHandler..cctor () <0x57d908a4 + 0x0003f> in :0 : --- End of inner exception stack trace --- : at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_generic_class_init (intptr) : at Android.Runtime.JNIEnv.Initialize (Android.Runtime.JnienvInitializeArgs* args)

-- EDIT

All AOT/LVVM/Concurrent garbage collector/proguard/multidex options are enabled

-- EDIT 2

Source code of error: https://github.com/xamarin/xamarin-android/blob/master/src/Mono.Android/Android.Runtime/UncaughtExceptionHandler.cs

static UncaughtExceptionHandler ()
    {
        var mono_UnhandledException = typeof (System.Diagnostics.Debugger)
            .GetMethod ("Mono_UnhandledException", BindingFlags.NonPublic | BindingFlags.Static);
        mono_unhandled_exception = (Action<Exception>) Delegate.CreateDelegate (typeof(Action<Exception>), mono_UnhandledException);

        var ad_due = typeof (AppDomain)
            .GetMethod ("DoUnhandledException",
                bindingAttr:  BindingFlags.NonPublic | BindingFlags.Instance,
                binder:       null,
                types:        new []{typeof (UnhandledExceptionEventArgs)},
                modifiers:    null);
        if (ad_due != null) {
            AppDomain_DoUnhandledException  = (Action<AppDomain, UnhandledExceptionEventArgs>) Delegate.CreateDelegate (
                    typeof (Action<AppDomain, UnhandledExceptionEventArgs>), ad_due);
        }
    }

So it seems either mono_UnhandledException is null or AppDomain_DoUnhandledException is null.

Will try to upgrade or downgrade mono version in appcenter. It should be wrong.

-- EDIT 3

AppDomain.cs has changed recently in mono:

https://github.com/mono/mono/commits/c1cbe060f617707258fd5111fd5ffd7ccd581899/mcs/class/corlib/System/AppDomain.cs

But DoUnhandledException has not changed.

-- EDIT 4

Works fine when built/published locally. So the problem is specific to appcenter.


Solution

  • AppCenter has recently released Mono 5.8 bundled with Xamarin.Android 8.2 (which supports TargetFrameworkVersion: 8.1), in order to use it, please change your branch configuration -> Save&Build.

    Also filed the issue for investigation by Xamarin.Android team.