7

When I try to run my program in release mode, it give this error:

Severity    Code    Description Project File    Line    Suppression State
Error   XAGJS7004   System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Xamarin.Android.Tasks.TypeMapGenerator.GenerateRelease(Boolean skipJniAddNativeMethodRegistrationAttributeScan, List`1 javaTypes, String outputDirectory, ApplicationConfigTaskState appConfState)
   at Xamarin.Android.Tasks.GenerateJavaStubs.WriteTypeMappings(List`1 types, TypeDefinitionCache cache)
   at Xamarin.Android.Tasks.GenerateJavaStubs.Run(DirectoryAssemblyResolver res)
   at Xamarin.Android.Tasks.GenerateJavaStubs.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 MauiApp1    C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.440\tools\Xamarin.Android.Common.targets   1438    

It is strange because in debug mode it works fine and I can't find any answer online. Window snippet

emilim
  • 109
  • 7
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jul 31 '22 at 17:02
  • That's an unusual error. If you make a new Maui project, then run it in release mode, does it cause the same error? If not, try *gradually* adding your code to that new project. See when the error starts happening. Goal is to find the minimum code needed to cause that problem. Then make a public github repo, and open an issue at .Net Maui's github. – ToolmakerSteve Jul 31 '22 at 19:13
  • yes, even when I try with a project it gives me the same error. It would seem to be a bug in Maui. Maybe because I'm using .net6.0 instead of .net7.0? – emilim Aug 02 '22 at 06:54
  • Suddenly same problem here. My solution, after months of fussing with MAUI. I will try Avalonia. – Klamsi Sep 22 '22 at 07:27
  • Did you update to sdk 31? Maybe this is the trigger? – Klamsi Sep 22 '22 at 09:25

3 Answers3

4

There is fore sure some incompatibility between MAUI and .NET7, but you can set your project to use previous NET6 version following few steps:

  1. Discover installed SDKs and keep note of the exact version 6

    dotnet --info SDKs list

  2. Move to project folder and create a global.json file, it sets the NET SDK to be used. Be sure to set the installed SDK version:

    dotnet new globaljson --sdk-version 6.0.301 --roll-forward disable

  3. Restore workloads required by your project:

    dotnet workload restore

  4. Open project in Visual Studio, confirm to reload the project if requred.

Now your project will be built using NET6 SDK.

Giampiero
  • 41
  • 3
  • This worked for me with one caveat. I had a WPF project in the solution which doesn't support the dotnet workload restore and caused it to fail. If this happens, just go into the project's subfolder and run the dotnet workload restore (it can take a while) – Leeland Clay Oct 04 '22 at 17:56
2

I solved it by uninstalling .net7, I found on Maui's github page people with the same problem and I think it will be fixed in the future.

It remains a shame Maui is not yet ready for .net7

emilim
  • 109
  • 7
  • I have uinstalled with "dotnet-core-uninstall", and uninstall Visual Studio 2022 Preview 17.3 (P5). After re-install, the same "XAGJS7004" error. Even after uninstall the .NET Preview 7 (7.0.100-preview.6.22352.1), "dotnet --list-sdks" continue to show Preview 7 and contiue to get the error "XAGJS7004". I just delete the folder "c:\program files\dotnet\sdk\7.0.100-preview.6.22352.1". Now all works fine. thanks @emilim – antonio Aug 08 '22 at 05:47
-1

Had the same problem, working on debug, this exception on release. I solved it by: Clean Maui solution -> delete bin and obj -> Close Visual Studio -> Rebuild -> Working

Hope this helps if you didn't try

IagoCC
  • 1
  • 2