josephmoresena / NativeAOT-AndroidHelloJniLib

This repo is a sample of an Android JNI library compilated using NativeAOT
MIT License
60 stars 6 forks source link

HelloJniLib

This repo is a sample for Android JNI libraries compilation with NativeAOT. For now, we support two ways to compile this library:

  1. Using BFlat tool.
  2. Using .NET SDK 8.0

BFlat

In order to optimize the build process we recommend the use of BFlat tool. This tool works on both Windows and Linux, so WSL environment is not needed for android compilation.

Prerequisites

This library depends on Rxmxnx.PInvoke.Extensions, so you need download the latest version of these packages:

Extract dlls file to BFlatSupport folder.

Build

Use the following command to build library.

    {PATH_TO_BFLAT}bflat build -r BFlatSupport/Rxmxnx.PInvoke.Extensions.dll --no-stacktrace-data --no-globalization --no-exception-messages -Os --no-pie --separate-symbols --os:linux --arch:arm64 --libc:bionic -o:libhello-jni.so

NET SDK 8.0

With new .NET 8.0 SDK we are now able to compile NativeAOT android binaries using linux-bionic RID.

How to build it

This process was tested for android-arm64 (linux-bionic-arm64) compilation but may work for android-x64 ( linux-bionic-x64) too.
The following commands assume:

Environment Parameters

MSBuild Parameters

Considerations

How to test it?

This sample is inspired by ndk-sample/hello-jni so with some changes you can load this library from that application.

Considerations: