Skip to content

NativeAOT Deployment

Justin Wojciechowski edited this page Nov 30, 2026 · 15 revisions

NativeAOT Deployment Guide

This guide covers deploying .NET applications that use Swift bindings with NativeAOT compilation.

Why NativeAOT?

NativeAOT produces ahead-of-time compiled native code, providing the best runtime experience for Swift interop:

  1. No JIT limitations — the small percentage of P/Invokes that fall back to direct CallConvSwift (where no @_cdecl wrapper or native thunk is available) work correctly on NativeAOT but may encounter Mono JIT limitations in certain frame types
  2. Full AOT compilation — no JIT compilation overhead, deterministic startup, no finalizer-thread JIT issues
  3. Compact binaries — NativeAOT produces small native binaries (~2 MB for a test app)

The same generated bindings work on both runtimes. The vast majority of P/Invokes use CallConvCdecl — either through native thunks (ARM64 and SysV AMD64) or @_cdecl Swift wrappers — and work identically on Mono and NativeAOT. See Known Limitations: Calling Convention Architecture for the full breakdown.

Note: NativeAOT has one known calling convention issue: custom C# structs with float/double fields have values placed in GPR instead of FPR on ARM64. System types like CGRect are unaffected. The generator automatically detects float-field structs and routes them through @_cdecl wrappers, so this is transparent to consumers.

Quick Start

Device deployment (NativeAOT)

Add these properties to your .csproj:

<PropertyGroup>
  <PublishAot>true</PublishAot>
  <PublishAotUsingRuntimePack>true</PublishAotUsingRuntimePack>
  <TrimMode>partial</TrimMode>
  <NoWarn>$(NoWarn);IL2026;IL2087;IL2091;IL3050</NoWarn>
</PropertyGroup>

Publish for device:

dotnet publish -c Release

Simulator deployment (Mono)

No special configuration needed. Simulator builds use Mono JIT automatically — NativeAOT does not support iossimulator-arm64.

dotnet build    # Uses Mono, works on simulator

On .NET 11 a simulator build, and a device build without PublishAot, runs on CoreCLR instead of Mono: the interpreter in Debug, ReadyToRun in Release. The device NativeAOT publish on this page works the same way on .NET 11 (net11.0-ios). See Choosing .NET and Xcode.

On .NET 10 with Xcode 27.2, use workload set 10.0.404 or later. The earlier Xcode 27.0/27.1 .NET 10 workloads failed a NativeAOT publish on an upstream trimming regression (IL2037); the 27.2 workload does not.

Required .csproj Properties

Property Value Purpose
PublishAot true Enables NativeAOT compilation
PublishAotUsingRuntimePack true Uses the NativeAOT runtime pack for iOS
TrimMode partial Preserves reflection paths used by SwiftMarshal

Trimming Warning Suppressions

The Swift runtime uses reflection for tuple marshalling and generic type metadata. These produce trimming analysis warnings that are safe to suppress:

Warning Source Why It's Safe
IL2026 SwiftMarshal.MarshalFromSwift<T> → tuple path Only affects tuple marshalling; non-tuple paths verified safe
IL2087 SwiftMarshal.MarshalToSwift<T> generic annotations Same — tuple-specific reflection
IL2091 SwiftMarshal.MarshalFromSwift<T> generic annotations Same
IL3050 TypeMetadata.TryGetTupleTypeMetadata MakeGenericMethod for tuple metadata construction

Add to your .csproj:

<NoWarn>$(NoWarn);IL2026;IL2087;IL2091;IL3050</NoWarn>

Non-tuple types (primitives, SwiftString, SwiftArray, SwiftOptional, classes, structs) do not use these reflection paths and are fully trimming-safe.

Trimmer roots are shipped automatically

SwiftBindings.Runtime ships an ILLink.Descriptors.xml via buildTransitive, so the runtime types that NativeAOT needs to keep alive (protocol proxy infrastructure, marshalling helpers, System.ValueTuple constructors used by Optional<(T1,T2)>, etc.) are rooted automatically. Generated binding packages also contribute their own descriptors when needed. You don't need to author <TrimmerRootDescriptor> items in your app — the suppressions above are the only trimming knobs you should normally touch.

SwiftBindingsInteropMode

Generated binding packages include a .targets file that automatically detects your deployment target and adjusts diagnostic behavior.

How It Works

The SwiftBindingsInteropMode property controls whether Mono JIT safety warnings are shown:

Mode Behavior When Used
Auto (default) Detects PublishAot — resolves to Direct or Safe Always, unless overridden
Direct Suppresses SB0001 warnings — full API access NativeAOT builds (PublishAot=true)
Safe Shows SB0001 warnings on methods using CallConvSwift fallback Mono builds (simulator, no PublishAot)

In Auto mode (the default), the build system checks $(PublishAot):

  • PublishAot=true → Direct → SB0001 suppressed → clean API, no warnings
  • Otherwise → Safe → SB0001 visible as warnings on methods using CallConvSwift fallback

Direct mode suppresses the warning; it does not change IntelliSense. SB0001 members carry [EditorBrowsable(Never)] unconditionally so they don't clutter completion for the majority of consumers, who are on a JIT runtime where these members throw. They remain fully callable and compilable — the [Obsolete] message and its wiki link are the discovery path.

Overriding

To force a specific mode, set it explicitly in your .csproj:

<PropertyGroup>
  <!-- Force full API access regardless of runtime (at your own risk on Mono) -->
  <SwiftBindingsInteropMode>Direct</SwiftBindingsInteropMode>
</PropertyGroup>

Diagnostic IDs

The one diagnostic that changes meaning between Mono and NativeAOT is SB0001: a direct CallConvSwift P/Invoke with no @_cdecl wrapper or native thunk. It may crash on Mono (Simulator) and is safe on NativeAOT. SwiftBindingsInteropMode auto-suppresses it in Direct (device) mode. Those members are also [EditorBrowsable(Never)].

Every other SB* ID describes a condition that holds on both runtimes. The full table — including SB0009 (tombstone), SB0010 (inert reverse-dispatch protocol), and SB1003 (struct write-back) — lives in Troubleshooting — Binding Diagnostic IDs.

Dual-Runtime Compatibility

The same generated bindings work on both Mono and NativeAOT without code changes. The vast majority of P/Invokes use CallConvCdecl (via native thunks or @_cdecl wrappers), which works identically on both runtimes. The remaining methods use direct CallConvSwift:

┌───────────────────────────────┐     ┌───────────────────────────────┐
│   Simulator (Mono JIT)        │     │   Device (NativeAOT)          │
├───────────────────────────────┤     ├───────────────────────────────┤
│ Native thunks: WORK            │     │ Native thunks: WORK            │
│ @_cdecl wrappers: WORK        │     │ @_cdecl wrappers: WORK        │
│ Direct CallConvSwift: MOSTLY  │     │ Direct CallConvSwift: WORK    │
└───────────────────────────────┘     └───────────────────────────────┘

Methods and constructors that can't receive thunks or wrappers (method-level generics, optional existentials, cross-module extensions, certain generic-host constructors, Swift variadic-pack / result-builder wrappers, and return-type-only overloads) are annotated with [Obsolete(DiagnosticId = "SB0001")] — auto-suppressed in NativeAOT builds via SwiftBindingsInteropMode. The SB0001 surface is kept tight on purpose: the generator routes as many generic-host constructors as it can through normalized @_cdecl shims (which drop SB0001 and work on Mono too), and reserves the marker for the shapes that genuinely require the direct CallConvSwift fallback — so on a Mono build you get a build-time warning rather than a runtime crash.

Device Publish Workflow

Prerequisites

  • Xcode with a valid Apple Development certificate
  • A provisioning profile covering your app's bundle identifier
  • A physical iOS device connected or on the same network

Code Signing Properties

<PropertyGroup>
  <CodesignKey>Apple Development: Your Name (XXXXXXXXXX)</CodesignKey>
  <CodesignProvision>Your Provisioning Profile Name</CodesignProvision>
  <TeamIdentifierPrefix>XXXXXXXXXX</TeamIdentifierPrefix>
</PropertyGroup>

Build and Deploy

# Publish NativeAOT for device
dotnet publish -c Release

# Install on device
xcrun devicectl device install app \
  --device "Your Device Name" \
  bin/Release/net10.0-ios/ios-arm64/publish/YourApp.app

# Launch with console output
xcrun devicectl device process launch --console \
  --device "Your Device Name" \
  com.your.bundleid

App Bundle Size

NativeAOT produces compact binaries. A test app with Swift bindings + runtime:

  • IPA: ~2 MB (code-signed, includes Swift frameworks)

The Swift standard library is not bundled — a stable-ABI, min-iOS-15 app links the OS-resident /usr/lib/swift, so the .ipa carries no libswift*.dylib and no SwiftSupport/ folder. The SwiftBindings.Runtime native helper embeds as a signed Frameworks/SwiftBindingsRuntime.framework, not a loose dylib, so the finished .ipa is App Store–compliant with no extra steps. See Publishing — App Store submission and native packaging.

Limitations

NativeAOT is Device-Only (iOS)

NativeAOT for iOS targets ios-arm64 only. The iossimulator-arm64 runtime identifier is not supported for dotnet publish with PublishAot=true. Simulator builds always use Mono JIT on .NET 10, and CoreCLR on .NET 11.

This means:

  • Development/debugging → Simulator (Mono on .NET 10, CoreCLR on .NET 11) — fast iteration, no code signing
  • Release/production → Device (NativeAOT) — full API access, no JIT limitations

macOS note: macOS native builds use CoreCLR, which handles CallConvSwift correctly. The @_cdecl wrappers and native thunks still provide value for patterns that require C-compatible calling conventions (non-blittable types, custom float-field structs). NativeAOT is available for macOS but not required for correct operation.

CPU architectures (Apple Silicon and Intel x86_64)

Pre-built SwiftBindings.Apple.* packages ship native wrapper slices for both arm64 and x86_64, so the osx-x64, maccatalyst-x64, iossimulator-x64, and tvossimulator-x64 runtime identifiers resolve a matching slice with no extra configuration. (For your own bindings, the SDK emits an x86_64 slice automatically when the source xcframework includes one; an arm64-only source yields an arm64-only wrapper.) osx-x64 runs on CoreCLR (natively on Intel Macs, under Rosetta on Apple Silicon). The one architecture-specific runtime caveat: net10.0-maccatalyst apps on Intel default to the Mono interpreter (the Mono x64 JIT has open calling-convention defects on this target), which the SDK applies automatically. Opt back into the JIT — at your own risk — with <SwiftBindingsMacCatalystX64UseJit>true</SwiftBindingsMacCatalystX64UseJit>. See Known Limitations — CPU Architectures for the full breakdown.

DllImportResolver Conflict

Generated bindings use [ModuleInitializer] to register a DllImportResolver for the assembly. If your application also calls NativeLibrary.SetDllImportResolver for the same assembly, it will throw InvalidOperationException. Wrap your call in a try-catch if you need custom resolution alongside generated bindings.

Further Reading

Clone this wiki locally