You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GitHub Actions运行Xamarin.Android单元测试遇MSB4019错误求助

Fix MSB4019 Error When Running Xamarin.Android Unit Tests in GitHub Actions

Let's break down why you're hitting this error and how to fix it quickly:

The Root Cause

When you run dotnet test on a Xamarin.Android project, the .NET CLI tries to look for Xamarin-specific MSBuild targets in the dotnet SDK directory (like C:\Program Files\dotnet\sdk\3.1.300\Xamarin\Android\...), but those targets don't live there. Xamarin.Android's build targets are actually installed with Visual Studio/MSBuild, not the standalone dotnet SDK. That's why your non-Android unit tests work fine—they're pure .NET projects that play nice with dotnet test.

Solution 1: Use MSBuild's Test Target Instead of dotnet test

Replace your dotnet test command with an MSBuild invocation that runs the tests directly. This ensures you're using the MSBuild environment that has access to Xamarin's targets.

Update your GitHub Actions step to look like this:

- name: UnitTests.Droid
  run: |
    nuget restore
    cd MobileAccess.UnitTests.Droid
    msbuild MobileAccess.UnitTests.Droid.csproj /verbosity:normal /t:Rebuild /p:Configuration=Release
    # Run tests via MSBuild instead of dotnet test
    msbuild MobileAccess.UnitTests.Droid.csproj /t:Test /p:Configuration=Release /p:AndroidSdkDirectory=$ANDROID_SDK_ROOT

Solution 2: Ensure GitHub Actions Has the Right Build Environment

To make sure MSBuild can find Xamarin's targets, add steps to set up MSBuild and the required Xamarin components in your workflow:

# Add these steps before your unit test step
- name: Setup .NET SDK
  uses: actions/setup-dotnet@v3
  with:
    dotnet-version: '3.1.x'

- name: Setup MSBuild
  uses: microsoft/setup-msbuild@v1.1

This setup-msbuild action points to the Visual Studio installation on the runner, which includes all the Xamarin.Android build targets you need.

Alternative: Use VSTest.Console.exe

If you prefer using the VSTest runner directly, you can call it explicitly (this works well if you need more control over test execution):

& "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" .\bin\Release\MobileAccess.UnitTests.Droid.dll

Note: The path to vstest.console.exe might vary depending on the Visual Studio version installed on the GitHub runner—adjust it if needed.

Key Takeaway

Xamarin.Android projects rely on Visual Studio/MSBuild-specific targets, so dotnet test isn't the right tool for the job here. Stick with MSBuild's Test target or VSTest to run your Android unit tests in GitHub Actions.

内容的提问来源于stack exchange,提问作者S. Koshelnyk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:48:00