Skip to content

Dotnet Android targeting - #3485

Merged
csukuangfj merged 5 commits into
k2-fsa:masterfrom
GLM-FM:dotnet-android
Apr 8, 2026
Merged

csukuangfj merged 5 commits into
k2-fsa:masterfrom
GLM-FM:dotnet-android

Conversation

@GLM-FM

@GLM-FM GLM-FM commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3483 and probably also #1241

This is marked WIP because I am currently just grabbing the sherpa-onnx-c-api from a local android build, because there is no Android specific wheel I can just grab off of Huggingface. Not sure if Android wheels are even a thing? I don't know much about wheels in general.

Anyway, I manually built the necessary libraries with this command: SHERPA_ONNX_ENABLE_C_API=ON ./build-android-arm64-v8a.sh. But I did not look at how to integrate this with CI yet because I wasn't sure what the preferred way forward actually is. Thoughts?

Summary by CodeRabbit

  • Chores
    • Extended support for Android ARM64 platform, enabling builds for ARM64-based Android devices.
    • Added .NET 10.0 as a newly supported target framework alongside existing versions.
    • Improved build automation to streamline multi-platform compilation and package generation.
    • Enhanced build system configuration for better platform-specific output management.

@coderabbitai

coderabbitai Bot commented Apr 8, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR adds Android arm64 runtime package support to the sherpa-onnx .NET ecosystem. It introduces Android-specific build generation logic, updates project configurations to target Android, and refactors the build script to handle multiple runtime identifiers dynamically while fetching Android native libraries.

Changes

Cohort / File(s) Summary
Build Configuration
scripts/dotnet/.gitignore, scripts/dotnet/sherpa-onnx.csproj.in, scripts/dotnet/sherpa-onnx.csproj.runtime.in
Added android-arm64 to gitignore patterns and RuntimeIdentifiers. Updated TargetFrameworks to include net10.0. Added PackageReference for Android-specific runtime package.
Generation Script
scripts/dotnet/generate.py
Introduced process_android(s, rid) function that generates Android-specific .csproj files by populating Jinja2 templates with Android library paths and rendering output to platform-specific directories.
Build Automation
scripts/dotnet/run.sh
Refactored directory creation and build/pack steps into parameterized loops using RIDS array. Added Android arm64 library fetch logic: downloads tarball if missing, extracts native .so files (onnxruntime, sherpa-onnx-c-api), and copies them to target directory.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

size:L

Poem

🐰 A new Android path emerges bright,
With arm64 libraries bundled tight,
From tarballs extracted, libraries in place,
Sherpa-onnx builds with newfound grace! 📱✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Dotnet Android targeting' directly describes the main change: adding Android arm64 support to .NET packages. It is concise and clearly summarizes the primary modification.
Linked Issues check ✅ Passed The PR successfully implements Android arm64 runtime support for .NET packages, addressing issue #3483's requirement for native library availability. Code changes add Android platform support to build scripts, csproj templates, and package generation without meeting all future CI automation goals.
Out of Scope Changes check ✅ Passed All changes are scoped to adding Android arm64 support: .gitignore entry, generate.py Android function, run.sh Android library fetching, and csproj framework/runtime updates. No unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@csukuangfj

Copy link
Copy Markdown
Collaborator

Thank you for your contribution!

because there is no Android specific wheel I can just grab off of Huggingface

Please use the following file

sherpa-onnx-v1.12.35-android.tar.bz2

You can find the required .so files for Android inside it. Users have successfully used Rust with Android via Tauri.

Note that you only need to copy two .so files, one is the c api, the other is the onnxruntime lib. No need to copy the cxx so lib.

@csukuangfj

Copy link
Copy Markdown
Collaborator

Please also update

<RuntimeIdentifiers>linux-x64;linux-arm64;osx-x64;osx-arm64;win-x64;win-x86;win-arm64</RuntimeIdentifiers>

and

<ItemGroup>
<PackageReference Include="org.k2fsa.sherpa.onnx.runtime.linux-x64" Version="{{ version }}" />
<PackageReference Include="org.k2fsa.sherpa.onnx.runtime.linux-arm64" Version="{{ version }}" />
<PackageReference Include="org.k2fsa.sherpa.onnx.runtime.osx-x64" Version="{{ version }}" />
<PackageReference Include="org.k2fsa.sherpa.onnx.runtime.osx-arm64" Version="{{ version }}" />
<PackageReference Include="org.k2fsa.sherpa.onnx.runtime.win-x64" Version="{{ version }}" />
<PackageReference Include="org.k2fsa.sherpa.onnx.runtime.win-x86" Version="{{ version }}" />
<PackageReference Include="org.k2fsa.sherpa.onnx.runtime.win-arm64" Version="{{ version }}" />
</ItemGroup>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for the android-arm64 platform in the .NET build scripts. Key changes include updating the gitignore, adding a process_android function to the generation script, and refactoring run.sh to use an array and a loop for building various Runtime Identifiers (RIDs). A syntax error was identified in the new bash loop in run.sh that requires a semicolon or newline before the 'do' keyword.

Comment thread scripts/dotnet/run.sh Outdated
@csukuangfj

Copy link
Copy Markdown
Collaborator

by the way, make sure you know the changes you are making.

Here is what chatgpt tells me that should be changed

<TargetFrameworks>
  net8.0;
  net7.0;
  net6.0;
  net8.0-android
</TargetFrameworks>

for the csproj file.

Please test the changes locally.

@csukuangfj

Copy link
Copy Markdown
Collaborator
<Project Sdk="Microsoft.NET.Sdk">

  <!-- =========================
       General Properties
       ========================= -->
  <PropertyGroup>
    <TargetFrameworks>
      net8.0;
      net7.0;
      net6.0;
      netstandard2.0;
      net8.0-android
    </TargetFrameworks>

    <OutputType>Library</OutputType>
    <LangVersion>10.0</LangVersion>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

    <AssemblyName>sherpa-onnx</AssemblyName>
    <Version>{{ version }}</Version>

    <!-- NuGet Metadata -->
    <PackageId>org.k2fsa.sherpa.onnx</PackageId>
    <Title>sherpa-onnx v{{ version }}</Title>
    <Authors>The Next-gen Kaldi development team</Authors>
    <Company>Xiaomi Corporation</Company>
    <Description>
      sherpa-onnx is an open-source real-time speech recognition toolkit.
      It supports offline streaming recognition across multiple platforms.
    </Description>

    <PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
    <PackageProjectUrl>https://github.com/k2-fsa/sherpa-onnx</PackageProjectUrl>
    <RepositoryUrl>https://github.com/k2-fsa/sherpa-onnx</RepositoryUrl>
    <PackageReadmeFile>README.md</PackageReadmeFile>

    <PackageTags>
      speech recognition voice audio stt asr speech-to-text AI offline
    </PackageTags>

    <!-- Signing -->
    <SignAssembly>false</SignAssembly>
    <PublicSign>false</PublicSign>
    <DelaySign>false</DelaySign>
  </PropertyGroup>

  <!-- =========================
       Restore Sources
       ========================= -->
  <PropertyGroup>
    <RestoreSources>
      {{ packages_dir }};
      $(RestoreSources);
      https://api.nuget.org/v3/index.json
    </RestoreSources>
  </PropertyGroup>

  <!-- =========================
       Package Content
       ========================= -->
  <ItemGroup>
    <None Include="../README.md" Pack="true" PackagePath="/" />
  </ItemGroup>

  <!-- =========================
       Platform-specific runtimes
       ========================= -->

  <!-- Windows -->
  <ItemGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
    <PackageReference Include="org.k2fsa.sherpa.onnx.runtime.win-x64" Version="{{ version }}" />
    <PackageReference Include="org.k2fsa.sherpa.onnx.runtime.win-x86" Version="{{ version }}" />
    <PackageReference Include="org.k2fsa.sherpa.onnx.runtime.win-arm64" Version="{{ version }}" />
  </ItemGroup>

  <!-- Linux -->
  <ItemGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
    <PackageReference Include="org.k2fsa.sherpa.onnx.runtime.linux-x64" Version="{{ version }}" />
    <PackageReference Include="org.k2fsa.sherpa.onnx.runtime.linux-arm64" Version="{{ version }}" />
  </ItemGroup>

  <!-- macOS -->
  <ItemGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))">
    <PackageReference Include="org.k2fsa.sherpa.onnx.runtime.osx-x64" Version="{{ version }}" />
    <PackageReference Include="org.k2fsa.sherpa.onnx.runtime.osx-arm64" Version="{{ version }}" />
  </ItemGroup>

  <!-- Android -->
  <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
    <PackageReference Include="org.k2fsa.sherpa.onnx.runtime.android-arm64" Version="{{ version }}" />
  </ItemGroup>

</Project>

Here is the suggestion from ChatGPT. You can test it.

@csukuangfj

Copy link
Copy Markdown
Collaborator

https://github.com/k2-fsa/sherpa-onnx/actions/runs/24126389428/workflow

Here is the CI in case you want to test it locally.

@GLM-FM

GLM-FM commented Apr 8, 2026

Copy link
Copy Markdown
Contributor Author

No worries, I've already been testing it locally by adding the built .nupkgs as a local package source for my application. It definitely no longer struggles with loading the DLL now.
The missing PackageReference is a good catch - I didn't notice earlier because I added a PackageReference to the Android runtime package in my project when troubleshooting, then forgot about it.

Here is what chatgpt tells me that should be changed

<TargetFrameworks>
  net8.0;
  net7.0;
  net6.0;
  net8.0-android
</TargetFrameworks>

for the csproj file.

I'm pretty sure ChatGPT is wrong here; the OS-specific TFM is needed if the library is using platform specific APIs, but this only refers to C# APIs, not native runtime libraries. It builds and runs fine for me without the additional net8.0-android TFM.

The conditional ItemGroups are also wrong, because MSBuild::IsOSPlatform checks the OS of the build host. So with the changes proposed by ChatGPT the CI would end up publishing a NuGet package that exclusively depends on the Linux runtime packages, and would therefore break on any other platform.

I suppose adding OS-specific TFMs for every supported platform and then making the runtime packages conditional on that could make sense, but I'm pretty sure that depending on how the final application is published (portable vs for a specific target runtime) , those runtimes are already stripped out anyway so it wouldn't really do much. For example, looking inside the produced APK I only see the Android runtime libraries anyway.

While I'm at it though, I could add the recently released net10.0 to TargetFrameworks if that's okay.

@csukuangfj

Copy link
Copy Markdown
Collaborator

Yes, please add net10.0

@GLM-FM

GLM-FM commented Apr 8, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for your contribution!

because there is no Android specific wheel I can just grab off of Huggingface

Please use the following file

sherpa-onnx-v1.12.35-android.tar.bz2

You can find the required .so files for Android inside it. Users have successfully used Rust with Android via Tauri.

Note that you only need to copy two .so files, one is the c api, the other is the onnxruntime lib. No need to copy the cxx so lib.

Done! Is there a defined order in which things are run in CI? Or is it all parallel? If a release is made, the Android native libraries need to be done building before the dotnet packages for the same version.

Yes, please add net10.0

Also done.

@csukuangfj

Copy link
Copy Markdown
Collaborator

The CI is triggered manually after all deps are built.

Thanks! Will release this week. Probably tomorrow.

@GLM-FM
GLM-FM marked this pull request as ready for review April 8, 2026 12:38
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 8, 2026
@GLM-FM

GLM-FM commented Apr 8, 2026

Copy link
Copy Markdown
Contributor Author

Wonderful! I've removed the draft status as well.

@csukuangfj csukuangfj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you again for your contribution!

@csukuangfj
csukuangfj merged commit 5459ad1 into k2-fsa:master Apr 8, 2026
1 check was pending
@GLM-FM GLM-FM mentioned this pull request Apr 8, 2026
@csukuangfj

Copy link
Copy Markdown
Collaborator

New NuGet packages released. Please re-check.

https://www.nuget.org/packages/org.k2fsa.sherpa.onnx.runtime.android-arm64/

@GLM-FM

GLM-FM commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

Seems to work as before! On to actually implementing and testing now :D Thanks for the quick release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Missing runtime libraries for .NET on Android

2 participants