Hello,
At the time of writing, .NET MAUI 8 is out of support. Please consider upgrading to .NET MAUI 9 or later for continued support and improvements.
Back to the main point, you are encountering a build failure in a .NET MAUI (.NET 8) Android Release configuration due to duplicate classes between androidx.collection.jvm
and androidx.collection.ktx
artifacts. This issue does not manifest in Debug builds but causes a crash during D8/R8 processing in Release builds.
The issue arises from the following:
- Your project includes both
Xamarin.AndroidX.Collection.Jvm
(1.3.0.1, containingandroidx.collection.collection-jvm.jar
) andXamarin.AndroidX.Collection.Ktx
(1.2.0.9, containingandroidx.collection.collection-ktx.jar
). - Starting with AndroidX Collection 1.3.0, the library was rewritten in Kotlin, and the main artifact (
collection-jvm
) now includes Kotlin extensions, renderingcollection-ktx
redundant and causing class overlaps (e.g.,ArrayMapKt
). - Debug builds bypass this issue due to fast deployment and partial dexing, but Release builds perform full dexing and shrinking, exposing the conflict during AAB generation.
Resolution
Use Collection 1.3.x and Exclude Collection.Ktx (Recommended)
- Remove Direct References to
Xamarin.AndroidX.Collection.Ktx
: Ensure your.csproj
file does not explicitly referenceXamarin.AndroidX.Collection.Ktx
. - Exclude Transitive
Collection.Ktx
Dependencies: Add the following to your.csproj
to neutralize any transitive inclusion ofXamarin.AndroidX.Collection.Ktx
:<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'"> <PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.2.0.9" ExcludeAssets="all" PrivateAssets="all" /> </ItemGroup>
- Update Related AndroidX KTX Packages: Ensure all AndroidX KTX packages (e.g.,
Xamarin.AndroidX.Fragment.Ktx
,Xamarin.AndroidX.Activity.Ktx
,Xamarin.AndroidX.Core.Ktx
) are updated to their latest versions compatible withXamarin.AndroidX.Collection
1.3.x. Older versions may transitively pull incollection-ktx
1.2.x, reintroducing the conflict.
Downgrade to Collection 1.2.x
If updating all dependencies is not feasible, downgrade to Xamarin.AndroidX.Collection
1.2.x to align with Xamarin.AndroidX.Collection.Ktx
1.2.x:
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
<PackageReference Include="Xamarin.AndroidX.Collection" Version="1.2.0.9" />
</ItemGroup>
Note: Avoid referencing Xamarin.AndroidX.Collection.Jvm
1.3.x. Plan to upgrade to 1.3.x and remove collection-ktx
in the future.
Additional Project Cleanup Recommendations
To prevent further conflicts and ensure a clean build:
- Remove Duplicate Package References:
- Your
.csproj
includes duplicate entries forControls.UserDialogs.Maui
(versions 1.7.0 and 1.3.0) andXamarin.AndroidX.Browser
. Retain only the latest version in the conditionalItemGroup
fornet8.0-android
. - Example: Keep
Controls.UserDialogs.Maui
1.7.0 if compatible with .NET 8.
- Your
- Remove Legacy Xamarin Packages:
- Remove
Xamarin.Essentials
, as its functionality is included inMicrosoft.Maui.Essentials
for .NET MAUI. - Review
Xam.Plugin.*
packages (e.g.,Media
,Connectivity
). Replace with MAUI-compatible alternatives or built-in APIs where possible, as these may pull in outdated AndroidX dependencies.
- Remove
Recommended .csproj
Updates
Update your .csproj
as follows to align dependencies and eliminate conflicts:
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
<PackageReference Include="Controls.UserDialogs.Maui" Version="1.7.0" />
<PackageReference Include="Plugin.CurrentActivity" Version="2.1.0.4" />
<PackageReference Include="Xamarin.Google.Guava" Version="32.0.1" />
<PackageReference Include="Xamarin.AndroidX.Core" Version="1.12.0.2" />
<PackageReference Include="Xamarin.AndroidX.Collection" Version="1.3.0.1" />
<PackageReference Include="Xamarin.AndroidX.Fragment.Ktx" Version="1.6.2" />
<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.0.1" />
<PackageReference Include="Xamarin.AndroidX.Browser" Version="1.6.0.2" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.2.0.9" ExcludeAssets="all" PrivateAssets="all" />
</ItemGroup>
Remove:
<!-- Remove duplicate/unnecessary references -->
<!-- <PackageReference Include="Xamarin.Essentials" Version="1.8.0" /> -->
<!-- Remove any unconditional duplicates of Controls.UserDialogs.Maui or Xamarin.AndroidX.Browser -->
Pre-build
- Delete the
bin
andobj
folders for all projects in your solution. - Clear NuGet caches:
dotnet nuget locals all --clear
- Restore and rebuild in Release mode:
dotnet restore dotnet build -c Release
I hope this helps clarify your issue.
References
- AndroidX Collection release notes (see 1.3.0)
- .NET MAUI GitHub issue
- Stack Overflow discussion
- .NET for Android errors and warnings index
- XA4307 page (related to Android tooling/proguard config issues often seen alongside Release builds)
- PackageReference in project files (see IncludeAssets/ExcludeAssets/PrivateAssets)
- dotnet list package (with --include-transitive)
- dotnet nuget locals (clear caches)
- Migrate Essentials (confirms Essentials is built into .NET MAUI; remove Xamarin.Essentials NuGet)