r/androiddev • u/Strong-Leg-9636 • 3d ago
onnxruntime-android + sherpa-onnx in the same APK: “cannot locate symbol OrtGetApiBase” and a workaround
Ran into this when adding a second speech model to an app that already used sherpa-onnx. Posting in case someone else hits it, and to ask if there’s a cleaner fix.
Setup: sherpa-onnx 2.0.2 (prebuilt AAR from their releases) for one model, and onnxruntime-android 1.28.0 from Maven to run a plain ONNX model directly. Both ship a libonnxruntime.so. sherpa’s is 1.28.2, Maven’s is 1.28.0. Only one file with that name can end up in the APK, so one copy gets dropped.
I assumed 1.28.0 vs 1.28.2 wouldn’t matter. It does. The exported symbols are versioned (VERS_1.28.0 / VERS_1.28.2), and the linker checks the version. Keep sherpa’s lib and onnxruntime’s Java bridge fails at load with cannot locate symbol "OrtGetApiBase". Keep the Maven one and sherpa breaks the same way.
What worked: keep sherpa’s 1.28.2 lib and patch onnxruntime’s JNI bridge (libonnxruntime4j_jni.so) so it asks for VERS_1.28.2 instead of VERS_1.28.0. The name is in the ELF version-needs section (SHT_GNU_verneed). Both strings are the same length, so it’s an in-place rewrite, but each verneed entry also stores the ELF hash of the version name, so that has to be recomputed too, or the linker still won’t match it. Then repack the AAR and depend on that instead of the Maven artifact.
It only works because the bridge imports just 3 functions from the core lib (OrtGetApiBase plus two execution provider calls), 1.28.2 exports all of them, and 1.28.2 still serves ORT API version 28, which is what the 1.28.0 bridge requests. I only did arm64-v8a since that’s all I ship.
Script (about 80 lines of Python, no dependencies): https://github.com/ahmedhmam1994/voxscribe-ai-voice-dictation/blob/main/android/voxscribe_app/android/tools/make_ort_aar.py
Questions for anyone who’s dealt with this:
- Is there a supported way to build sherpa-onnx against an external onnxruntime AAR instead of its bundled one? I didn’t find a clean switch for the prebuilt Android AAR.
- Any reason this patch is a bad idea I’m not seeing? It’s been fine on device so far, but it feels like the kind of thing that breaks quietly on the next version bump.
