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
Project Configuration
Add the following to your csproj file:
<PropertyGroup>
<PublishAot>true</PublishAot>
<!-- Necessary before Avalonia 12.0, was used for accessiblity APIs -->
<BuiltInComInteropSupport>false</BuiltInComInteropSupport>
</PropertyGroup>
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
Publishing Avalonia Native AOT Applications
Windows
dotnet publish -r win-x64 -c Release
Linux
dotnet publish -r linux-x64 -c Release