Skip to content

Instantly share code, notes, and snippets.

@Bip901
Last active December 23, 2023 17:15
Show Gist options
  • Save Bip901/af46447d7e7b0b9647f0841946979e0e to your computer and use it in GitHub Desktop.
Save Bip901/af46447d7e7b0b9647f0841946979e0e to your computer and use it in GitHub Desktop.
Build C# for Android Native
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net8.0\publish\android-arm64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>linux-bionic-arm64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>false</PublishSingleFile>
<DisableUnsupportedError>true</DisableUnsupportedError>
<PublishAotUsingRuntimePack>true</PublishAotUsingRuntimePack>
</PropertyGroup>
</Project>

Compiling a C# project to an ELF executable that can run on Android

Guide based on https://github.com/josephmoresena/NativeAOT-AndroidHelloJniLib

  • Download the Android NDK (I used version r26b)
  • Under your project root directory, in ./Properties/PublishProfiles, create a file named android-arm64.pubxml with the contents as described in this gist.
  • Run the following shell commands (written in PowerShell, may be ported to bash or similar)
dotnet build --configuration Release

$env:ANDROID_NDK_ROOT='C:\PUT\PATH\TO\NDK\HERE'
$env:PATH="$env:PATH;$env:ANDROID_NDK_ROOT\toolchains\llvm\prebuilt\windows-x86_64\bin"
$env:ObjCopyName="llvm-objcopy.exe"
$sysroot="$env:ANDROID_NDK_ROOT\toolchains\llvm\prebuilt\windows-x86_64\sysroot"

dotnet publish MyProject.csproj -p:PublishProfile=android-arm64 -p:CppCompilerAndLinker=clang -p:SysRoot="$sysroot"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment