Search code examples
c#androidxamarinxamarin.androidvisual-studio-mac

Native Xamarin.Android app keeps crashing in release mode


I have a native Xamarin.Android app I've built a few years back and I've been maintaining it since then. Lately, I tried to build a new release but the app keeps crashing. I did tens of releases in the past using the same project and compilation settings. Here's an exemple of a stack trace I get in release:

[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies.
[mono-rt]   at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction10-21 13:24:49.970 F/mono-rt (15785): File name: 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'
[mono-rt]   at Java.Interop.JniEnvironmentInfo..ctor () [0x00006] in :0 
[mono-rt]   at Java.Interop.JniEnvironment+c.b__35_0 () [0x00000] in :0 
[mono-rt]   at System.Threading.ThreadLocal`1[T].GetValueSlow () [0x00031] in :0 
[mono-rt]   at System.Threading.ThreadLocal`1[T].get_Value () [0x0003e] in :0 
[mono-rt]   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in :0ds.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0002d] in :0 
[mono-rt]   at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue* parameters) [0x00018] in :0 
[mono-rt]   at Java.Interop.JniEnvironment+StaticMet10-21 13:24:49.970 F/mono-rt (15785):   at Android.OS.Looper.get_MainLooper () [0x0000a] in :0 
[mono-rt]   at Android.Runtime.AndroidEnvironment.GetDefaultSyncContext () [0x00000] in :0 
[mono-rt]   at System.AndroidPlatform.GetDefaultSyncContext () [0x00000] in :0 
[mono-rt]   at System.Threading.SynchronizationContext.GetThreadLocalContext () [0x00005] in :0 
[mono-rt]   at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run (System.Threading.Tasks.Task ignored, System.Boolean canInlineContinuationTask) [0x00003] in :0 
[mono-rt]   at System.Threading.Tasks.Task.FinishStageThree () [0x0003c] in :0 
[mono-rt]   at System.Threading.Tasks.Task`1[TResult].TrySetResult (TResult result) [0x0004f] in :0 
[mono-rt]   at System.Threading.Tasks.TaskFactory`1+c__DisplayClass38_0`1[TResult,TArg1].b__0 (System.IAsyncResult iar) [0x00008] in :0 
[mono-rt]   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in 10-21 13:24:49.970 F/mono-rt (15785):   at (wrapper managed-to-native) System.Runtime.Remoting.Messaging.AsyncResult.Invoke(System.Runtime.Remoting.Messaging.AsyncResult)
[mono-rt]   at System.Runtime.Remoting.Messaging.AsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00000] in :0 
[mono-rt]   at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in :0 

From the stack trace, I have a feeling it might be a problem with the linker, but I have tried all 3 settings (Don't link, link SDK assemblies only, link all). When changing some code here and there, I have sometimes slightly different stack traces, but nothing really helpful. Here's the compilation settings I'm using:

Project settings #1

Project settings #2

Project settings #3

Project settings #4

Project settings #5

From time to time, the app will launch successfully without changing anytime special. It will only work once.

What I'm I missing?


Solution

  • I am pretty sure that you have some weird behavior in release. Since you use mono on android you may call an await function somewhere that return a Task<T> you must not use the await but .GetAwaiter().Result.

    https://github.com/xamarin/xamarin-android/issues/6409