androidxamarinazure-devopsxamarin.androidtypeloadexception

Random System.TypeLoadException


I'm building an Xamarin Android app with latest Visual Studio 2019 updates. On some devices I get some exceptions that I don't understand, because on most of the devices it seems to be fine.

Exception 1:

System.TypeLoadException: Could not resolve type with token 010000a0 from typeref (expected class 'System.St' in assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e')
System.Net.Http.HttpClientHandler..ctor () [0x00000] in <29982df1e97049f5afd4cab6d3aaf93d>:0
ModernHttpClient.NativeMessageHandler..ctor (System.Boolean throwOnCaptiveNetwork, System.Boolean customSSLVerification, ModernHttpClient.NativeCookieHandler cookieHandler) [0x00033] in <51795dd1dbb344bdacae7208f448a6b3>:0

Exception 2:

System.TypeLoadException: Could not set up parent class, due to: Could not load type of field 'System.Net.Http.HttpClientHandler:<DangerousAcceptAnyServerCertificateValidator>k__BackingField' (2) due to: Could not resolve type with token 0100003b from typeref (expected class 'System.Security.Cryptography.X509Certificates.X509Chais' in assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e') assembly:System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e type:System.Security.Cryptography.X509Certificates.X509Chais member:(null) assembly:System.Net.Http.dll type:HttpClientHandler member:(null)

Exception 3:

System.TypeInitializationException: Invalid IL code in System.Net.Http.HttpClientHandler/<>c:.cctor (): IL_000a: calli 0x9628021e
ModernHttpClient.NativeMessageHandler..ctor (System.Boolean throwOnCaptiveNetwork, System.Boolean customSSLVerification, ModernHttpClient.NativeCookieHandler cookieHandler) [0x00033] in <51795dd1dbb344bdacae7208f448a6b3>:0
ModernHttpClient.NativeMessageHandler..ctor () [0x00000] in <51795dd1dbb344bdacae7208f448a6b3>:0

Exception 4:

System.TypeLoadException: Could not resolve type with token 01000071 from typeref (expected class 'System.Net.Http.HttpRequestException' in assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') assembly:System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:System.Net.Http.HttpRequestException member:(null)

It looks like some assemblies or classes from "System.Net.Http" could not be found on some devices. I tried to change "AndroidLinkMode" from "SdkOnly" to "None" and retrieved a bigger APK file, but issue still remains.

Edit: I'm using NuGet packages deployed by Azure Dev Ops (different Agent as my computer). Visual Studio is up to date and all package references have the same version.


Solution

  • I could solve this issue by enabling "Bundle assemblies into native code" under "Android Options" in project properties, hopefully.