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

When I export to Android via the Godot editor, I get the error: Cross-OS native compilation is not supported. C:\Users\TrifingZW\.nuget\packages\microsoft.dotnet.ilcompiler\8.0.8\build\Microsoft.NETCore.Native.Publish.targets(60,5).

I manually compiled the C# solution successfully using the following command:

$env:ANDROID_SDK_ROOT="C:\Users\TrifingZW\AppData\Local\Android\Sdk"
$env:ANDROID_NDK_ROOT="C:\Users\TrifingZW\AppData\Local\Android\Sdk\ndk\23.2.8568313"
$env:PATH="C:\Users\TrifingZW\AppData\Local\Android\Sdk\ndk\23.2.8568313\toolchains\llvm\prebuilt\windows-x86_64\bin;" + $env:PATH
dotnet publish -c ExportRelease -r linux-bionic-arm64 -p:DisableUnsupportedError=true -p:PublishAotUsingRuntimePack=true

This successfully compiled the solution to C:\Users\TrifingZW\GodotProjects\BtlEditor\.godot\mono\temp\bin\ExportRelease\linux-bionic-arm64\native\BtlEditor.so.

You could try adding those properties to the csproj:

<PropertyGroup>
    <DisableUnsupportedError>true</DisableUnsupportedError>
    <PublishAotUsingRuntimePack>true</PublishAotUsingRuntimePack>
</PropertyGroup>

But the runtime identifier specified by Godot on exporting is android, not linux-bionic, so if that’s required it might still not work.

I manually published the dotnet project to a .so file and manually included the .so file into the pck. It successfully ran, and then I automated this process using a Python script. The problem has been solved, thank you very much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.