dotnet / dotnet/maui

Android application crashes without debugger

Open
#28,575 8 comments 0 reactions 0 assignees View on GitHub
area-controls-shell p/3 platform/android s/triaged s/verified t/bug version/android-14
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

https://github.com/janseris/MauiShellWrapperCrashOnStartupWithoutDebugger

### Steps to Reproduce

Run with debugger. OK
Run without debugger: crash on startup

This is a minimal sample from a larger app where instead of crash, plain black or plain white screen was shown in the application depending on current dark/light mode.

### Link to public reproduction project repository

https://github.com/janseris/MauiShellWrapperCrashOnStartupWithoutDebugger

### Version with bug

9.0.50 SR5

### Is this a regression from previous behavior?

Not sure, did not test other versions

### Last version that worked well

Unknown/Other

### Affected platforms

Android

### Affected platform versions

Android 11 emulator, Android 12 device.

Android 14 emulator in Debug configuration and with attached debugger (without debugger attached, it crashes as Android 11 and 12):

![Image](https://github.com/user-attachments/assets/a9612294-ee3c-493e-bbae-902757fbf4af)

```
System.Reflection.TargetInvocationException
Message=Exception has been thrown by the target of an invocation.
```

Stack trace:
```
0xFFFFFFFFFFFFFFFF in Android.Runtime.RuntimeNativeMethods.monodroid_debugger_unhandled_exception C#
0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5 C#
0x1E in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:122,26 C#
0xBB in Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue at /_/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs:364,5 C#
0x22C in Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit at /_/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs:161,6 C#
0x93 in Microsoft.Maui.Controls.Xaml.ElementNode.Accept at /_/src/Controls/src/Xaml/XamlNode.cs:189,5 C#
0x6B in Microsoft.Maui.Controls.Xaml.ElementNode.Accept at /_/src/Controls/src/Xaml/XamlNode.cs:185,6 C#
0x84 in Microsoft.Maui.Controls.Xaml.RootNode.Accept at /_/src/Controls/src/Xaml/XamlNode.cs:242,6 C#
0xA0 in Microsoft.Maui.Controls.Xaml.XamlLoader.Visit at /_/src/Controls/src/Xaml/XamlLoader.cs:216,4 C#
0xE4 in Microsoft.Maui.Controls.Xaml.XamlLoader.Load at /_/src/Controls/src/Xaml/XamlLoader.cs:82,6 C#
0x4 in Microsoft.Maui.Controls.Xaml.XamlLoader.Load at /_/src/Controls/src/Xaml/XamlLoader.cs:57,82 C#
0x2C in Microsoft.Maui.Controls.Xaml.XamlLoader.Load at /_/src/Controls/src/Xaml/XamlLoader.cs:53,4 C#
0x7 in Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml at /_/src/Controls/src/Xaml/ViewExtensions.cs:42,4 C#
0xC in MauiApp4.AppShellWrapper.InitializeComponent at C:\Users\Jan\source\repos\MauiApp4\MauiShellWrapperCrashOnStartupWithoutDebugger\MauiApp4\obj\Debug\net9.0-android\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\AppShellWrapper.xaml.sg.cs:31,4 C#
> 0x9 in MauiApp4.AppShellWrapper..ctor at C:\Users\Jan\source\repos\MauiApp4\MauiShellWrapperCrashOnStartupWithoutDebugger\MauiApp4\AppShellWrapper.xaml.cs:16,9 C#
0x1 in MauiApp4.App.CreateWindow at C:\Users\Jan\source\repos\MauiApp4\MauiShellWrapperCrashOnStartupWithoutDebugger\MauiApp4\App.xaml.cs:12,13 C#
0x72 in Microsoft.Maui.Controls.Application.Microsoft.Maui.IApplication.CreateWindow at /_/src/Controls/src/Core/Application/Application.cs:448,5 C#
0x69 in Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow at /_/src/Core/src/Platform/Android/ApplicationExtensions.cs:46,4 C#
0x3D in Microsoft.Maui.MauiAppCompatActivity.OnCreate at /_/src/Core/src/Platform/Android/MauiAppCompatActivity.cs:28,5 C#
0x11 in Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net9.0/android-35/mcw/Android.App.Activity.cs:3196,4 C#
0x9 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:121,5 C#

```

When added try catch:

```
{System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: Parent of a Page must also be a Page
at Microsoft.Maui.Controls.Page.OnParentSet() in /_/src/Controls/src/Core/Page/Page.cs:line 552
at Microsoft.Maui.Controls.Element.SetParent(Element value) in /_/src/Controls/src/Core/Element/Element.cs:line 430
at Microsoft.Maui.Controls.Element.OnChildAdded(Element child) in /_/src/Controls/src/Core/Element/Element.cs:line 594
at Microsoft.Maui.Controls.VisualElement.OnChildAdded(Element child) in /_/src/Controls/src/Core/VisualElement/VisualElement.cs:line 1310
at Microsoft.Maui.Controls.Element.AddLogicalChild(Element element) in /_/src/Controls/src/Core/Element/Element.cs:line 225
at Microsoft.Maui.Controls.Layout.Add(IView child) in /_/src/Controls/src/Core/Layout/Layout.cs:line 163
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
--- End of inner exception stack trace ---
at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, Object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo) in /_/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs:line 364
at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit(ElementNode node, INode parentNode) in /_/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs:line 161
at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 189
at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 185
at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 242
at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode rootnode, HydrationContext visitorContext, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 216
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 82
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 57
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, Type callingType) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 53
at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[AppShellWrapper](AppShellWrapper view, Type callingType) in /_/src/Controls/src/Xaml/ViewExtensions.cs:line 42
at MauiApp4.AppShellWrapper.InitializeComponent() in C:\Users\Jan\source\repos\MauiApp4\MauiShellWrapperCrashOnStartupWithoutDebugger\MauiApp4\obj\Debug\net9.0-android\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\AppShellWrapper.xaml.sg.cs:line 27
at MauiApp4.AppShellWrapper..ctor() in C:\Users\Jan\source\repos\MauiApp4\MauiShellWrapperCrashOnStartupWithoutDebugger\MauiApp4\AppShellWrapper.xaml.cs:line 18}
```

And also:

```
{System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: Parent of a Page must also be a Page
at Microsoft.Maui.Controls.Page.OnParentSet() in /_/src/Controls/src/Core/Page/Page.cs:line 552
at Microsoft.Maui.Controls.Element.SetParent(Element value) in /_/src/Controls/src/Core/Element/Element.cs:line 430
at Microsoft.Maui.Controls.Element.OnChildAdded(Element child) in /_/src/Controls/src/Core/Element/Element.cs:line 594
at Microsoft.Maui.Controls.VisualElement.OnChildAdded(Element child) in /_/src/Controls/src/Core/VisualElement/VisualElement.cs:line 1310
at Microsoft.Maui.Controls.Element.AddLogicalChild(Element element) in /_/src/Controls/src/Core/Element/Element.cs:line 225
at Microsoft.Maui.Controls.Layout.Add(IView child) in /_/src/Controls/src/Core/Layout/Layout.cs:line 163
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
--- End of inner exception stack trace ---
at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, Object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo) in /_/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs:line 364
at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit(ElementNode node, INode parentNode) in /_/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs:line 161
at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 189
at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 185
at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode) in /_/src/Controls/src/Xaml/XamlNode.cs:line 242
at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode rootnode, HydrationContext visitorContext, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 216
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 82
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Boolean useDesignProperties) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 57
at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, Type callingType) in /_/src/Controls/src/Xaml/XamlLoader.cs:line 53
at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[AppShellWrapper](AppShellWrapper view, Type callingType) in /_/src/Controls/src/Xaml/ViewExtensions.cs:line 42
at MauiApp4.AppShellWrapper.InitializeComponent() in C:\Users\Jan\source\repos\MauiApp4\MauiShellWrapperCrashOnStartupWithoutDebugger\MauiApp4\obj\Debug\net9.0-android\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\AppShellWrapper.xaml.sg.cs:line 27
at MauiApp4.AppShellWrapper..ctor() in C:\Users\Jan\source\repos\MauiApp4\MauiShellWrapperCrashOnStartupWithoutDebugger\MauiApp4\AppShellWrapper.xaml.cs:line 18}
```

### Did you find any workaround?

No

Why does it work with debugger?

Contributor guide

Open the contributing guide

Research direction

Run the linked MauiShellWrapperCrashOnStartupWithoutDebugger sample on Android with and without a debugger, starting from AppShellWrapper.xaml.cs and App.xaml.cs. Trace the XAML startup path through XamlLoader.cs, ApplyPropertiesVisitor.cs, and Page.cs, using the reported exception to compare both configurations. Done means the sample starts without a debugger on the affected Android versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, csharp
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.