迁移Xamarin.Forms到.NET MAUI时遇AndroidX依赖重复定义JAVA错误
解决.NET MAUI迁移中android.support.v4.app.INotificationSideChannel$Stub重复定义错误
问题原因
该错误源于AndroidX库与旧版Android Support库的依赖冲突,通常是某个第三方NuGet包间接引用了旧版Support库,与你添加的AndroidX包产生重复定义。
解决方案步骤
1. 统一AndroidX包版本
你当前引用的Xamarin.AndroidX.Collection(1.4.0.4)和Xamarin.AndroidX.Collection.Ktx(1.4.0.3)版本不一致,易引发兼容性问题。将两个包版本统一为同版本(建议用最新兼容版):
<PackageReference Include="Xamarin.AndroidX.Collection" Version="1.4.0.4" /> <PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.4" />
2. 处理冲突的第三方包
Xamarin.Azure.NotificationHubs.Android 1.1.4.1属于Xamarin时代的包,大概率依赖旧版Support库,可按以下方式处理:
- 优先升级该包至最新版本,或替换为MAUI原生兼容的Azure Notification Hubs包;
- 若无法替换,在包引用中排除旧版Support库依赖:
<PackageReference Include="Xamarin.Azure.NotificationHubs.Android" Version="1.1.4.1"> <ExcludePackageReferences> <ExcludePackageReference>Xamarin.Android.Support.v4</ExcludePackageReference> </ExcludePackageReferences> </PackageReference>
3. 启用Jetifier自动迁移依赖
在Android项目的.csproj文件中添加以下配置,让Jetifier自动将所有旧版Support库依赖转换为AndroidX:
<PropertyGroup> <AndroidUseJetifier>true</AndroidUseJetifier> <AndroidEnableDesugar>true</AndroidEnableDesugar> </PropertyGroup>
4. 清理重建项目
- 手动删除项目的
bin和obj文件夹,或右键项目选择「清理」; - 清理整个解决方案后执行「重建」操作。
5. 移除不必要的兼容包
若未使用旧版Xamarin.Forms的兼容控件,可移除Microsoft.Maui.Controls.Compatibility包,减少潜在依赖冲突:
<!-- 删除该行引用 --> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.40" />
6. 升级Google Play Services包
Xamarin.GooglePlayServices.Base 118.3.0.1版本较旧,可能与MAUI 8不兼容,尝试升级至最新稳定版本。
内容的提问来源于stack exchange,提问作者zmaten
相关产品推荐
相关产品推荐

