How to use NativeAOT with Godot C# on Android

Godot Version

Godot-4.3RC2

Question

I have no idea because AOT does not support cross-OS compilation (I successfully compiled the Linux-64 version using WSL, but I wasn’t able to compile it for Android). Even if I manage to compile and export the .so file, I don’t know how to tell Godot about it.

NativeAOT support for Android is experimental. See NativeAOT status for Android · Issue #106748 · dotnet/runtime · GitHub

Even if I manage to compile and export the .so file, I don’t know how to tell Godot about it.

You are not supposed to build the project yourself, use the Export dialog in the Godot editor. To enable NativeAOT follow the instructions in the Microsoft documentation: Native AOT deployment overview - .NET | Microsoft Learn

Add this to your csproj:

<PropertyGroup>
    <PublishAot>true</PublishAot>
</PropertyGroup>

Then, since the GodotSharp assemblies don’t support trimming, you’ll also have to root the assemblies. See Trimming options - .NET | Microsoft Learn

Add this to your csproj:

<ItemGroup>
    <TrimmerRootAssembly Include="GodotSharp" />
    <TrimmerRootAssembly Include="$(TargetName)" />
 </ItemGroup>

And be aware of the limitations of NativeAOT, and the linux-bionic runtime. For example, crypto APIs in the BCL will make your game crash. Some of the prerequisites and limitations are documented in runtime/src/coreclr/nativeaot/docs/android-bionic.md at v8.0.0 · dotnet/runtime · GitHub