.NET MAUI发布iOS ipa时Xamarin.Shared.Sdk.targets抛出三类异常
问题复现
执行以下命令构建.NET MAUI应用,目标为生成iOS平台.ipa安装包时触发报错:dotnet publish -f:net6.0-ios -c:Release
报错信息如下:
C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\15.4.303\targets\ Xamarin.Shared.Sdk.targets(366,3) : error : MessagingRemoteException: 客户端Build在响应主题xvs/build/execute-task/OnesieMobile/18e8f3c002fMergeAppBundles时发生错误 [C:\Development_BuildPipeline\OnesieMobile\OnesieMobile\OnesieMobile.csproj]
C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\15.4.303\targets\ Xamarin.Shared.Sdk.targets(366,3) : error : AggregateException: 发生一个或多个错误。(该方法或操作未实现。) [C:\Development_BuildPipeline\OnesieMobile\OnesieMobile\OnesieMobile.csproj]
C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\15.4.303\targets\ Xamarin.Shared.Sdk.targets(366,3) : error : NotImplementedException: 该方法或操作未实现。 [C:\Development_BuildPipeline\OnesieMobile\OnesieMobile\OnesieMobile.csproj]
C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\15.4.303\targets\Xamarin.Shared.Sdk.targets(366,3): error : [C:\Development_BuildPipeline\OnesieMobile\OnesieMobile\OnesieMobile.csproj]
相同环境下,Visual Studio自带.NET MAUI模板Demo、公开MVVM示例项目均可正常走完发布流程并安装到iPhone设备;自有项目可正常在Windows本地调试运行,也可通过VS连接云端Mac启动远程iOS模拟器运行,仅Release打包流程触发上述错误。
运行环境
- Windows 10 家庭版 21H2
- Visual Studio 2022 版本 17.3.0 Preview 1.1
项目.csproj配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks> <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks> <!-- 如需构建Tizen应用,需先安装对应Tizen工作负载 --> <!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> --> <OutputType>Exe</OutputType> <RootNamespace>OnesieMobile</RootNamespace> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> <!-- 应用显示名称 --> <ApplicationTitle>OnesieMobile</ApplicationTitle> <!-- 应用标识 --> <ApplicationId>com.maui.app</ApplicationId> <ApplicationIdGuid>447A62C6-2841-41D6-B9AE-444E6E1A908B</ApplicationIdGuid> <!-- 版本信息 --> <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> <ApplicationVersion>1</ApplicationVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion> <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion> <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion> </PropertyGroup> <PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'"> <RuntimeIdentifiers>ios-arm64</RuntimeIdentifiers> <CodesignProvision>Maui Provisioning</CodesignProvision> <CodesignKey>Apple Distribution: My Name (XXXX)</CodesignKey> <ArchiveOnBuild>true</ArchiveOnBuild> <TcpPort>58181</TcpPort> <ServerAddress>xxxxx</ServerAddress> <ServerUser>xxxx</ServerUser> <ServerPassword>xxxx</ServerPassword> <_DotNetRootRemoteDirectory>/Users/xxxx/Library/Caches/Xamarin/XMA/SDKs/dotnet/</_DotNetRootRemoteDirectory> </PropertyGroup> <ItemGroup> <!-- 应用图标 --> <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" /> <!-- 启动页 --> <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" /> <!-- 图片资源 --> <MauiImage Include="Resources\Images\*" /> <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" /> <!-- 自定义字体 --> <MauiFont Include="Resources\Fonts\*" /> <!-- 原始资源(自动移除"Resources\Raw"路径前缀) --> <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" /> </ItemGroup> <ItemGroup> <PackageReference Include="CommunityToolkit.Maui" Version="1.0.0" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="7.1.2" /> <PackageReference Include="sqlite-net-pcl" Version="1.8.116" /> </ItemGroup> <ItemGroup> <Compile Update="View\ActivityDetailPage.xaml.cs"> <DependentUpon>ActivityDetailPage.xaml</DependentUpon> </Compile> </ItemGroup> <ItemGroup> <MauiXaml Update="View\ActivityDetailPage.xaml"> <Generator>MSBuild:Compile</Generator> </MauiXaml> <MauiXaml Update="View\SettingsPage.xaml"> <Generator>MSBuild:Compile</Generator> </MauiXaml> </ItemGroup> </Project>
修复步骤
该报错为Visual Studio 2022 17.3.0 Preview 1.1版本的已知缺陷,和项目业务代码无关,按以下步骤操作即可修复:
- 将Visual Studio 2022升级到17.3正式版或更高版本。17.3 Preview 1版本中iOS远程构建的MergeAppBundles任务存在未实现的逻辑漏洞,仅在多目标框架项目发布iOS Release包时触发,单iOS框架项目、Debug调试场景不会触发该问题,这也是模板Demo、示例项目可正常发布、自有项目调试正常但打包失败的核心原因。
- 升级完成后,删除csproj中硬编码的Mac连接相关配置节点,包括
TcpPort、ServerAddress、ServerUser、ServerPassword、_DotNetRootRemoteDirectory。这类参数会由VS连接Mac后自动注入,硬编码容易和当前连接的Mac实际环境参数冲突,触发构建异常。 - 清理构建缓存:本地删除项目目录下的
bin、obj文件夹,远程连接的Mac设备上删除~/Library/Caches/Xamarin/目录下的所有缓存文件,再重新执行发布命令即可。 - 若升级后提示NuGet包版本不匹配,将
CommunityToolkit.Maui升级到和当前MAUI SDK对应的版本即可,不会影响现有业务逻辑。
内容的提问来源于stack exchange,提问作者Chris

