Xamarin "MSB4062: The Xamarin task could not be loaded" in CI
MSB4062 means MSBuild found a reference to a task but could not load its assembly. For Xamarin, this usually means the Xamarin.Android or Xamarin.iOS build tasks are not installed on the runner, so the targets cannot run.
What this error means
A Xamarin build fails with "error MSB4062: The \"...\" task could not be loaded from the assembly ...Xamarin... Confirm that the declaration is correct and the assembly and dependencies are available."
error MSB4062: The "Xamarin.Android.Tasks.GetAndroidPackageName" task could not be
loaded from the assembly .../Xamarin.Android.Build.Tasks.dll. Could not load file or
assembly. Confirm that the <UsingTask> declaration is correct.Common causes
Xamarin build tasks not installed on the runner
The Xamarin.Android/iOS workloads or Visual Studio components that provide the task assemblies are absent, so MSBuild cannot load the task.
Wrong MSBuild driving a Xamarin project
Using a plain dotnet/MSBuild without the Xamarin targets installed cannot find the Xamarin task assemblies.
How to fix it
Use an image with Xamarin components installed
- Pick a runner image that includes the Xamarin workloads.
- Drive the build with the MSBuild that has the Xamarin targets.
- Restore and build so the task assemblies load.
# macOS image with Xamarin components
runs-on: macos-13
steps:
- run: msbuild MyApp.Android/MyApp.Android.csproj /p:Configuration=ReleaseInstall the Xamarin workloads
Provision the Xamarin components so the task assemblies are present before the build.
# install the Mono / Xamarin SDK components the build needs
brew install --cask xamarin-androidHow to prevent it
- Build Xamarin projects on images that ship Xamarin components.
- Use the MSBuild that owns the Xamarin targets.
- Pin the Xamarin/Mono version so task assemblies stay consistent.