1

I'm upgrading a Xamarin Forms project to MAUI taking the projects to .NET 8 and everything seems to be working alright until I try and run tests. The app works and all of my tests show in the Test Explorer.

I get the following error:

========== Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ==========
Building Test Projects
Starting test discovery for requested test run
========== Starting test discovery ==========
You must install or update .NET to run this application.
App: C:\Path\To\App\bin\Debug\net8.0-android34.0\testhost.exe
Architecture: x64
Framework: 'Microsoft.Android', (x64)
.NET location: C:\Program Files\dotnet
No frameworks were found.

My dotnet root is: C:\Program Files\dotnet.

runtimes:

C:\Program Files\dotnet\sdk> dotnet --list-runtimes
Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

sdks

C:\Program Files\dotnet\sdk> dotnet --list-sdks
8.0.407 [C:\Program Files\dotnet\sdk]
9.0.201 [C:\Program Files\dotnet\sdk]
9.0.202 [C:\Program Files\dotnet\sdk]

workloads (I find it a little odd that maui-android has no VS version listed)

C:\Program Files\dotnet\sdk> dotnet workload list

Installed Workload Id      Manifest Version       Installation Source
--------------------------------------------------------------------------------
android                    35.0.50/9.0.100        SDK 9.0.200, VS 17.13.35919.96
ios                        18.2.9180/9.0.100      SDK 9.0.200, VS 17.13.35919.96
maccatalyst                18.2.9180/9.0.100      SDK 9.0.200, VS 17.13.35919.96
maui-android               9.0.14/9.0.100         SDK 9.0.200
maui-windows               9.0.14/9.0.100         SDK 9.0.200, VS 17.13.35919.96

My target framework is set:

<TargetFramework>net8.0-android34.0</TargetFramework> 

Here are my dependencies:

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
    <PackageReference Include="Moq" Version="4.13.1"/>
    <PackageReference Include="MSTest.TestAdapter" Version="3.8.3"/>
    <PackageReference Include="MSTest.TestFramework" Version="3.8.3"/>
    <PackageReference Include="sqlite-net-pcl" Version="1.9.172"/>
    <PackageReference Include="SQLiteNetExtensions" Version="2.1.0"/>
    <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0"/>
  </ItemGroup>

Here is the csproj file for my Unit Test Project

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0-android</TargetFramework>
    <OutputType>Library</OutputType>
    <SccProjectName>SAK</SccProjectName>
    <SccLocalPath>SAK</SccLocalPath>
    <SccAuxPath>SAK</SccAuxPath>
    <SccProvider>SAK</SccProvider>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <Configurations>Debug;iOS Debug;iOS Release;Release</Configurations>
  </PropertyGroup>
  <PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    <AssemblyTitle>Mobile.Tests</AssemblyTitle>
    <Product>Mobile.Tests</Product>
    <Copyright>Copyright ©  2018</Copyright>
    <AssemblyVersion>1.0.0.0</AssemblyVersion>
    <FileVersion>1.0.0.0</FileVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <CodeAnalysisRuleSet>..\..\CodeAnalysis.ruleset</CodeAnalysisRuleSet>
    <NoWarn>1701;1702;CA1416</NoWarn>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug iOS|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug iOS\</OutputPath>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='iOS Debug|AnyCPU'">
    <NoWarn>1701;1702;CA1416</NoWarn>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='iOS Release|AnyCPU'">
    <NoWarn>1701;1702;CA1416</NoWarn>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <NoWarn>1701;1702;CA1416</NoWarn>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Intergraph.SPC.Shared.Data">
      <HintPath>..\..\..\PathToCode\Source\Shared\Shared.Data\bin\Shared.Data.dll</HintPath>
    </Reference>
  </ItemGroup>
  <Choose>
    <When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
      <ItemGroup>
        <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
      </ItemGroup>
    </When>
    <Otherwise />
  </Choose>
  <ItemGroup>
    <ProjectReference Include="..\..\Mobile.Client\Mobile.Client\Mobile.Client.csproj" />
    <ProjectReference Include="..\..\Mobile.TokenGenerator\Mobile.TokenGenerator.csproj" />
    <ProjectReference Include="..\Mobile.Tests.Helpers\Mobile.Tests.Helpers.csproj" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="icons\mac\cell-SwitchCell-16~dark~sel%402x.png"/>
    .......lots of content tags trimmed for readability.......
    <Content Include="icons\win\view-WebView-16.png" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk">
      <Version>16.11.0</Version>
    </PackageReference>
    <PackageReference Include="Moq">
      <Version>4.13.1</Version>
    </PackageReference>
    <PackageReference Include="MSTest.TestAdapter">
      <Version>3.8.3</Version>
    </PackageReference>
    <PackageReference Include="MSTest.TestFramework">
      <Version>3.8.3</Version>
    </PackageReference>
    <PackageReference Include="sqlite-net-pcl">
      <Version>1.9.172</Version>
    </PackageReference>
    <PackageReference Include="SQLiteNetExtensions">
      <Version>2.1.0</Version>
    </PackageReference>
    <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
  </ItemGroup>
  <Choose>
    <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
      <ItemGroup>
        <Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
      </ItemGroup>
    </When>
  </Choose>
</Project>

I'm on Microsoft Visual Studio Professional 2022 (64-bit) - Version 17.13.5

If I write regular unit tests that don't target android they will execute. I've tried making new solutions and use different versions of things, but once I target android34.0 nothing works anymore. Any thoughts/ideas as to why this is or where I can look would be very helpful. Thanks in advance!

6
  • 1
    Please change the TargetFramework to the net9.0-android, it will keep the latest android version. Commented Apr 1 at 1:27
  • @LeonLu thanks, but unfortunately that didn't help. I still get the same results. Commented Apr 1 at 18:15
  • Please share your csproj file. You can open the VS installer, click the repair your VS. After that, please execute dotnet workload list, will you see VS version for android platform. Commented Apr 2 at 5:23
  • @LeonLu I did this and nothing changed. I'll add my csproj file contents, trimmed a bit for readability, for my unit test project to the question area above. Commented Apr 2 at 18:18
  • 1
    Should a unit test project be targeting net8.0-android? Commented Apr 5 at 17:43

1 Answer 1

1
+50

Your unit test project shouldn't target net8.0-android. Instead, change it to .NET 8.0 (net8.0). However, this will cause another issue: since your test project is referencing your MAUI project, and the MAUI project targets net8.0-android, the target frameworks between the two won't match.

To fix this issue, follow the steps below:

✅ Steps to Fix Target Framework Mismatch

  1. In your MAUI project, add net8.0 to the target frameworks:

    <TargetFrameworks>net8.0;net8.0-android</TargetFrameworks>
    
  2. To allow your MAUI project to compile as a DLL (which is required by the test project), conditionally set the OutputType:

    <OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>
    

For more information about setting up unit test projects in .NET MAUI, check out Gerald's video and sample repo:

📺 Video: https://www.youtube.com/watch?v=C9vIDLQwc7M
📁 GitHub Repo: https://github.com/jfversluis/MauixUnitTestSample

Sign up to request clarification or add additional context in comments.

8 Comments

This sounds exactly like the problem I'm dealing with, but when I add net8.0 as a target framework, the MAUI project stops building. Nothing droid related compiles because net8.0 doesn't have a reference to Microsoft.Android. All of my "using" import statements turn red until I remove net8.0 from the target frameworks list.
After above changes.. clean solution... close IDE...delete obj/bin... and rebuild your solution. @dghalbr
@dghalbr also note the you have to replace the OutputType in maui project with this <OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>.... remove the old one
Yes. I performed all of these steps, but it still doesn't work. I even tried taking the GitHub sample and start adding our code to it, just in case there is some magic I missed, and it fails also. Once net8.0 is listed alongside net8.0-android my android related imports break. I also tried with .net9.0 and got the same result. Thanks again, I really appreciate the help.
@dghalbr I Guess in your maui project whereever you have android specific code you need to add conditional compilation learn.microsoft.com/en-us/dotnet/maui/platform-integration/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.