I have a .NET MAUI project that stopped showing up on Windows platform (on Android it hangs on Hot Reload initializing) after adding a unit test project (xUnit 2.4.2) using .net7.0
.
I have referenced my .NET MAUI app as a dependency in my Tests project, following Gerald Versluis on Youtube and this article and I followed all the steps. I only tested in Debug mode.
I added the UseMaui
property to the Tests.csproj file
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseMaui>true</UseMaui>
<IsPackable>false</IsPackable>
</PropertyGroup>
I have added .net7.0
as a target framework and updated the condition on the OutputType
property in the MAUI app project file:
<PropertyGroup>
<!--If not running unit tests-->
<!--
<TargetFrameworks>net7.0-ios;net7.0-maccatalyst;net7.0-android33.0</TargetFrameworks>
<OutputType>Exe</OutputType>
-->
<!--If writing and running unit tests-->
<TargetFrameworks>net7.0-ios;net7.0-maccatalyst;net7.0-android33.0;net7.0</TargetFrameworks>
<OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<UseMauiEssentials>true</UseMauiEssentials>
</PropertyGroup>
Note the comments I added which I toggle based on which project I am running, which I want to avoid. The Tests project works fine but the MAUI app project builds and hangs on Windows platform by not showing the app window in Debug mode.
On Android, it's a different story, I get the app to launch but it's stuck on the splash screen with Hot Reload initializing until timeout.
The Output window is filled with errors similar to this [monodroid-assembly] open_from_bundles: failed to load assembly
[monodroid-assembly] open_from_bundles: failed to load assembly mymauiapp.Next.dll
[monodroid-gc] GREF GC Threshold: 46080
[monodroid-assembly] open_from_bundles: failed to load assembly Mono.Android.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Runtime.dll
[monodroid-assembly] open_from_bundles: failed to load assembly Java.Interop.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Runtime.InteropServices.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Collections.dll
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/mymauiapp.Next.dll
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/Mono.Android.dll [External]
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/System.Runtime.dll [External]
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/Java.Interop.dll [External]
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/System.Runtime.InteropServices.dll [External]
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/System.Collections.dll [External]
Resolved pending breakpoint for 'Android.Runtime.JNIEnv.RegisterJniNatives(System.IntPtr, System.Int32, System.IntPtr, System.IntPtr, System.Int32)' to /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNIEnv.cs:127 [0x00000].
[monodroid-assembly] open_from_bundles: failed to load assembly System.Threading.dll
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/System.Threading.dll [External]
[monodroid-assembly] open_from_bundles: failed to load assembly System.Threading.Thread.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Diagnostics.StackTrace.dll
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/System.Threading.Thread.dll [External]
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/System.Diagnostics.StackTrace.dll [External]
[me.mgrecruitin] Attempt to remove non-JNI local reference, dumping thread
Loaded assembly: data-0x7c27ceeddcc0 [External]
Loaded assembly: data-0x7c27ceeeaf00 [External]
Loaded assembly: data-0x7c27ceefb240 [External]
[monodroid-assembly] open_from_bundles: failed to load assembly netstandard.dll
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/netstandard.dll [External]
[monodroid-assembly] open_from_bundles: failed to load assembly System.Linq.dll
Loaded assembly: /data/data/com.companyname.mymauiapp/files/.__override__/System.Linq.dll [External]
[monodroid-assembly] open_from_bundles: failed to load assembly Microsoft.VisualStudio.DesignTools.TapContract.dll
[monodroid-assembly] open_from_bundles: failed to load assembly Microsoft.VisualStudio.DesignTools.TapContract.dll
[monodroid-assembly] open_from_bundles: failed to load assembly Xamarin.HotReload.Contracts.dll
[monodroid-assembly] open_from_bundles: failed to load assembly Xamarin.HotReload.Contracts.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Collections.Concurrent.dll
Possibly related question.
I created a new project to reproduce the issue which worked as intended, now I am puzzled to why this is happening. I'm guessing it's something to do with my mauiapp csproj configuration but can't seem to pinpoint exactly. This is a gist to my full app csprojs.
I tried clean/build again, deleted bin/obj folders, restarted VS, unloaded my Tests project multiple times and on the third or forth time I get the project working again. I don't know what is causing this.