Native AOT Deployment
Native AOT (Ahead-of-Time) compilation allows you to publish your Avalonia applications as self-contained executables with native performance characteristics. This guide covers Avalonia-specific considerations and setup for Native AOT deployment.
Benefits for Avalonia Applications
Native AOT compilation provides several advantages specifically relevant to Avalonia applications:
- Faster application startup time, particularly beneficial for desktop applications
- Reduced memory footprint for resource-constrained environments
- Self-contained deployment without requiring .NET runtime installation
- Improved security through reduced attack surface (no JIT compilation)
- Smaller distribution size when combined with trimming
Setting Up Native AOT for Avalonia
1. Project Configuration
Add the following to your csproj file:
<PropertyGroup>
<PublishAot>true</PublishAot>
<!-- Recommended Avalonia trimming settings for Native AOT -->
<BuiltInComInteropSupport>false</BuiltInComInteropSupport>
<TrimMode>link</TrimMode>
</PropertyGroup>
2. Trimming Configuration
Native AOT requires trimming. Add these trim settings specific to Avalonia:
<ItemGroup>
<!-- Preserve Avalonia types for reflection -->
<TrimmerRootAssembly Include="Avalonia.Themes.Fluent" />
<TrimmerRootAssembly Include="Avalonia.Themes.Default" />
</ItemGroup>
Avalonia-Specific Considerations
XAML Loading
When using Native AOT, XAML is compiled into the application at build time. Ensure you:
- Use
x:CompileBindings="True"
in your XAML files - Avoid dynamic XAML loading at runtime
- Use static resource references instead of dynamic resources where possible
Assets and Resources
- Bundle all assets as embedded resources
- Use
AvaloniaResource
build action for your assets - Avoid dynamic asset loading from external sources
ViewModels and Dependency Injection
- Register your ViewModels at startup
- Use compile-time DI configuration
- Avoid reflection-based service location