On an Apple Silicon Mac, with Visual Studio 17.1.6 Preview, I am receiving the following error when trying to build a Xamarin project for Android:
/usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/32.0.509/targets/Microsoft.Android.Sdk.AssemblyResolution.targets(5,5): Error XAPRAS7000: System.BadImageFormatException: Image is too small.
at System.Reflection.Throw.ImageTooSmall()
at System.Reflection.PortableExecutable.PEHeaders.SkipDosHeader(PEBinaryReader& reader, Boolean& isCOFFOnly)
at System.Reflection.PortableExecutable.PEHeaders..ctor(Stream peStream, Int32 size, Boolean isLoadedImage)
at System.Reflection.PortableExecutable.PEReader.InitializePEHeaders()
at System.Reflection.PortableExecutable.PEReader.GetMetadataBlock()
at System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(PEReader peReader, MetadataReaderOptions options, MetadataStringDecoder utf8Decoder)
at Xamarin.Android.Tasks.ProcessAssemblies.DeduplicateAssemblies(List`1 output, Dictionary`2 symbols)
at Xamarin.Android.Tasks.ProcessAssemblies.RunTask()
at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 17 (XAPRAS7000)
I have tried deleting bin/obj directories, cleaning, correctly setting architectures, adjusting fast deployment and stripping. I have also reinstalled Visual Studio. The project builds with no changes on a second physically identical machine.
I suspect this issue may have been caused by attempting to build and publish multiple different frameworks and architectures (eg. -f net7.0-android or -r iOS-arm64) using the command line. We had discrepancies between Visual Studio build results and Azure DevOps, so I was using trial and error via the command line to reproduce the issue we saw on DevOps.
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish
Only after this did the build in Visual Studio start throwing the error.
In the end, using:
dotnet --info
Showed me that I had the x64 architecture installed as well as arm64. I can't say with certainty that this was the issue, but it prompted me to remove x64 and then clear my local NuGet cache.
dotnet nuget locals --clear
Only after clearing my local NuGet cache was the problem resolved.