MAUI Android项目构建报错NETSDK1031:未指定RuntimeIdentifier导致自包含应用构建失败求助及原理咨询
Hey there, sorry to hear you're stuck with this MAUI Android build error! Let's break down what's happening and fix it step by step.
What's causing the NETSDK1031 error?
The error message is pretty straightforward: when you build a self-contained application (one that bundles the entire .NET runtime with your app), you have to tell MSBuild exactly which platform you're targeting via a RuntimeIdentifier (RID).
Chances are, something changed in your build configuration since you successfully published last week—maybe you accidentally enabled self-contained mode in Visual Studio's publish settings, or a recent update adjusted default build flags. Your current .csproj doesn't explicitly set either <SelfContained> or <RuntimeIdentifier>, so MSBuild is now throwing this error when it tries to build with self-contained mode enabled.
Fixes to Try
You have two main options depending on whether you need a self-contained app or not:
Option 1: Disable Self-Contained Mode (Most Likely What You Need)
If you don't need to bundle the .NET runtime with your app (which is the default for MAUI apps, since Android devices can have the runtime pre-installed or fetch it), just add <SelfContained>false</SelfContained> to your main <PropertyGroup> in the .csproj. This tells MSBuild to build a framework-dependent app, which doesn't require a RID.
Here's your modified .csproj snippet:
<PropertyGroup> <TargetFrameworks>net6.0-ios;net6.0-maccatalyst;net6.0-android31.0</TargetFrameworks> <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks> <OutputType>Exe</OutputType> <RootNamespace>AppRipsaPagosMAUI</RootNamespace> <UseMaui>true</UseMaui> <SingleProject>true</SingleProject> <ImplicitUsings>enable</ImplicitUsings> <!-- Add this line to disable self-contained mode --> <SelfContained>false</SelfContained> <!-- App Identifier --> <ApplicationId>com.companyname.appripsapagosmaui</ApplicationId> <ApplicationIdGuid>61252DD6-68C5-4051-9E5F-30226AFF9A7E</ApplicationIdGuid> <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>
Option 2: Enable Self-Contained Mode with a Specific RID
If you do need a self-contained build (e.g., for devices without access to the .NET runtime), you need to specify both <SelfContained>true</SelfContained> and a valid Android RID. Common Android RIDs are:
android-arm(32-bit ARM devices)android-arm64(64-bit ARM devices, most modern phones)android-x64(64-bit x86 devices, like emulators or some tablets)
You can set this specifically for Android to avoid affecting other target frameworks:
<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'"> <SelfContained>true</SelfContained> <RuntimeIdentifier>android-arm64</RuntimeIdentifier> </PropertyGroup>
Final Steps
After updating your .csproj, do a Clean Solution (right-click your solution in Visual Studio → Clean) followed by a Rebuild Solution. This will clear any cached build artifacts that might be causing issues.
内容的提问来源于stack exchange,提问作者Leandro Toloza

