VS 2022 Xamarin build exception in Android.dll - unable to build my solution

Charles Mills 26 Reputation points
2022-09-26T17:24:24.82+00:00

I have a VS 2022 Windows Xamarin Forms application that has been working successfully on Android and UWP. I have started working with a cloud Mac service to build an iOS version. I have tweaked a couple of things the way you do when you are trying to get something new to work, but I don't know what I might have done to cause this. Obviously I know what a basic OutOfRangeException is, but I have no idea how to solve this or even really what to look for.

The problem occurs during the build, not when running my code, so I have no idea where or how to look for the problem.

I tried unloading the iOS project but the error still occurs.

Here is the error

2>  KhyberPassWithUWP2.Android -> C:\Users\Charles\source\repos\KhyberPass 2022\KhyberPassWithUWP2.Android\bin\Debug\KhyberPassWithUWP2.Android.dll  
2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1683,3): error XAGPM7006: System.IndexOutOfRangeException: Index was outside the bounds of the array.  
2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1683,3): error XAGPM7006:    at Xamarin.Android.Tasks.GeneratePackageManagerJava.<>c__DisplayClass131_0.<AddEnvironment>g__AddEnvironmentVariable|2(String name, String value)  
2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1683,3): error XAGPM7006:    at Xamarin.Android.Tasks.GeneratePackageManagerJava.AddEnvironment()  
2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1683,3): error XAGPM7006:    at Xamarin.Android.Tasks.GeneratePackageManagerJava.RunTask()  
2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1683,3): error XAGPM7006:    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  
========== Rebuild All: 2 succeeded, 1 failed, 0 skipped ==========  

<GeneratePackageManagerJava> is not in my code. It is a section (?) in an XML (?) file that is part of Visual Studio, Xamarin.Android.Common.targets.

I'm dead in the water. I have no ideas where to start looking.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Charles Mills 26 Reputation points
    2022-09-27T21:34:40.717+00:00

    Problem solved.

    The original issue was that the Xamarin project as set up by the VS Wizard had a reference to Mono.Android at the solution level. That obviously is not right for an iOS build. Searching the Web for an answer I found https://stackoverflow.com/questions/38199278/failed-to-resolve-assembly-java-interop. I followed the upvoted answer there and I must have somehow hosed up the .csproj files in some subtle way. I restored the .cscproj files from an archival backup and the problem has gone away. I fixed the Mono.Android reference from within VS rather than by hacking on .csproj as the answer above suggested.