Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 519
Softlion Avatar asked Mar 09 '18 07:03

Softlion


People also ask

How do I fix type initializer for threw an exception?

Here are some helpful steps for finding the root cause of this problem... Click Debug--> Exceptions and check ON all the Thrown checkboxes. This will cause the debugger to stop on all first chance exceptions and will help you find the error under the Type Initializer error that you're seeing.

What is type initialization exception in C#?

Most commonly, a TypeInitializationException exception is thrown when a static constructor is unable to instantiate a type. The InnerException property indicates why the static constructor was unable to instantiate the type.


2 Answers

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.

like image 100
nevalenny Avatar answered Oct 24 '22 00:10

nevalenny


In my case, the issue was caused by using Android SDK 8.1 which App Center doesn't support yet. I had to downgrade to SDK 8.0 and now build works fine.

App Center team promised to add support to SDK 8.1, but they don't have an ETA yet.

like image 39
pfedotovsky Avatar answered Oct 24 '22 01:10

pfedotovsky