百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

Android-NativeAOT

> 编程语言
开源

A .NET 8、Android 上的 NativeAOT 示例

99 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

A .NET 8、Android 上的 NativeAOT 示例

Android-NativeAOT

A .NET 8, NativeAOT example on Android.

Getting started

Configure your environment: the following environment variables are required:

  • ANDROID_NDK_HOME: The path to an Android NDK installation.
  • ANDROID_HOME: The path to an Android SDK installation.

Build the project:

bash
dotnet build
dotnet publish DotNet/libdotnet.csproj
(cd Native && ./gradlew assembleRelease)

Install the app:

bash
$ANDROID_HOME/platform-tools/adb install Native/app/build/outputs/apk/release/app-release.apk

Run the app:

bash
$ANDROID_HOME/platform-tools/adb shell am start com.jonathanpeppers.nativeaot/android.app.NativeActivity

Example using SkiaSharp

This sample has a C++ Android Studio project:

  • Uses Native Activity
  • No Java/Kotlin code
  • Configures OpenGL
  • Calls into C# / managed code
  • Managed code uses SkiaSharp for rendering a random Skia shader
  • Tap input randomly changes the shader

Some screenshots of the Skia content:

(Note these look completely smooth on a Pixel 5, I just tried to snap quick gifs with Vysor)

The C# side is a:

  • .NET 8 class library
  • Built with RID linux-bionic-arm64
  • Uses the SkiaSharp NuGet package, as one would.
    • Used a nightly build of SkiaSharp, as I wanted a feature from Skia 3.0

App Size

The release .apk file of the SkiaSharp sample is ~4.26 MB

A breakdown of the files inside:

…

libdotnet.so is ~1.07 MB, and libSkiaSharp.so is ~8.5MB!

If we reduce this to a "Hello World" example:

  • hello.apk is ~430 KB!
  • libdotnet.so (uncompressed) is ~821 KB!

Startup Time

The average of 10 runs on a Pixel 5 of the SkiaSharp sample:

log
Average(ms): 121
Std Err(ms): 3.29983164553722
Std Dev(ms): 10.434983894999

Average of 10 runs on a Pixel 5 of the "Hello World" example:

log
Average(ms): 120.9
Std Err(ms): 2.97937353594118
Std Dev(ms): 9.42160637400367

They might be effectively the same.

For comparison (as of .NET 8), a dotnet new android app is about ~180ms on a Pixel 5, and dotnet new maui is about ~560ms.

Source: https://github.com/jonathanpeppers/maui-profiling

"Hello World" Example

See the HelloWorld branch.

I had this managed code:

csharp
[UnmanagedCallersOnly(EntryPoint = "ManagedAdd")]
public static int ManagedAdd(int x, int y) => x + y;

I created a C++ Android project using NativeActivity, and I called the managed code from C++:

cpp
// in dotnet.h
extern "C" int ManagedAdd(int x, int y);

// in native-lib.cpp
int result = ManagedAdd(1, 2);
__android_log_print (ANDROID_LOG_INFO, TAG, "ManagedAdd(1, 2) returned: %i", result);

Results in the message:

log
01-31 11:42:44.545 28239 28259 I NATIVE  : Entering android_main
01-31 11:42:44.550 28239 28259 I NATIVE  : ManagedAdd(1, 2) returned: 3

See DotNet/README.md on how to build libdotnet.so.

Notes

Console.WriteLine() doesn't work because it basically just writes to Unix stdout. stdout does not appear in adb logcat output, as you have to call __android_log_print instead.

This was an interesting example, to start a thread that processes stdout and calls the appropriate Android API:

  • https://codelab.wordpress.com/2014/11/03/how-to-use-standard-output-streams-for-logging-in-android-apps/

Instead, we can p/invoke into:

csharp
[DllImport("log", EntryPoint = "__android_log_print", CallingConvention = CallingConvention.Cdecl)]
public static extern int LogPrint(LogPriority priority, string tag, string format);

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#androidcsharpdotnetnativeaot

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言