0

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. 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.

Julian
  • 5,290
  • 1
  • 17
  • 40
Anass
  • 301
  • 1
  • 3
  • 13
  • Why did you enable `UseMaui` in the test project? That sounds incorrect to me, but shouldn't affect the MAUI project. I have a working setup with NUnit in my [MAUI samples](https://github.com/ewerspej/maui-samples) repository, but the same applies to xUnit, I've done it with both already. – Julian Apr 28 '23 at 18:59
  • I checkout your MAUI samples repo and even did the exact same configuration on my end. Same issue. Switching to a prior branch about 1 commit behind seems to not have the problem. The current commit adds the unit test project with modification on the mauiapp csproj that adds the ```.net7.0``` to TargetFrameworks tag and also inserts the condition on the OutputType tag. – Anass May 02 '23 at 16:27

1 Answers1

1

I noticed two things that seem to be incorrect. For Android, it should be net7.0-android, not net7.0-android33.0, and you're missing the target framework for Windows entirely.

Your project file should be updated to look like this:

<PropertyGroup>
    <TargetFrameworks>net7.0;net7.0-ios;net7.0-maccatalyst;net7.0-android;</TargetFrameworks>
    <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
    <OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
    <UseMaui>true</UseMaui>
    <SingleProject>true</SingleProject>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseMauiEssentials>true</UseMauiEssentials>
</PropertyGroup>

You also shouldn't have to enable the UseMaui option in the test project, since you don't want those dependencies.

Julian
  • 5,290
  • 1
  • 17
  • 40
  • Changing net7.0-android33.0 to net7.0-android and removing ```UseMaui ``` tag does not do anything different. The target framework for Windows is already present in my mauiapp csproj or if you kindly check the gist link that I attached above. I needed to access a few .NET MAUI APIs in my tests therefore I added the ```UseMaui ``` and ```UseMauiEssentials``` tags. Thanks – Anass May 02 '23 at 16:23
  • The files in the gist are not well formatted, which makes reading them difficult. I still think you should add the line `$(TargetFrameworks);net7.0-windows10.0.19041.0` right under the other framework definitions. Also, targeting `net7.0-android33.0` specifically, although possible, will make your app run exclusively against API 33.0. Make sure to change it everywhere, not just in the one location. – Julian May 02 '23 at 16:42