Modern Apps git

← Modern-Apps.git

library/ocr: keep OcrEngine inert when the JNI link fails

Commit
fdc60c497869e114b950878e163009efab3ef930
Author
Vayun Mathur <[email protected]>
When
2026-11-07 06:25 UTC (29 days ago)
Parents
80cc99b
TextRecognizer catches Exception around MlNative.createPpocr, but a
missing native symbol throws UnsatisfiedLinkError (an Error), which
escaped and crashed OcrEngine.isAvailable()/recognize() instead of
degrading to the documented empty result - seen on host JVMs whose
libmodelrunner.so loads but exports no PP-OCR entry point. Catch
Throwable at engine creation so the inert guarantee holds locally.

(cherry picked from commit f6325035915f9b07e2d9ab8fda28e76b957857ee)

Diff

library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.kt
diff --git a/library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.kt b/library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.ktindex 8c09864663..44b0c2dbbd 100644--- a/library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.kt+++ b/library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.kt@@ -112,7 +112,14 @@ class OcrEngine(private val context: Context) {         recognizer?.let { return it.isAvailable }         if (initTried) return false         initTried = true-        val created = TextRecognizer(context.applicationContext)+        // TextRecognizer documents "never throws", but a JNI linkage failure is an+        // Error, not an Exception, and escapes its catch - keep this engine's own+        // "inert, never crashes" guarantee here rather than depending on that.+        val created = try {+            TextRecognizer(context.applicationContext)+        } catch (_: Throwable) {+            return false+        }         if (!created.isAvailable) {             // Already logged natively under the `ModelRunner` tag, with the reason.             created.close()