Modern Apps git

← Modern-Apps.git

log: one guarded native loader for every Native wrapper

Commit
086ec10faa280be1474ae6a3ed64cc1244fb995f
Author
Vayun Mathur <[email protected]>
When
2026-11-19 15:51 UTC (16 days ago)
Parents
8565a49
Twenty-eight <Feature>Native wrappers hand-rolled their
System.loadLibrary guard in four variants: runCatching one-liners,
verbose try/catch logging, silent try/catch, and unguarded
init { System.loadLibrary(...) } in euicc, photos, passwords,
youpipe and both e2ee libraries - where a missing .so poisons the
class with ExceptionInInitializerError on every later touch. Two
variants caught only Exception, which UnsatisfiedLinkError (an
Error) escapes; weather even had the Exception catch first.

NativeLoader in :library:log (the module every app and library
already depends on) now owns the guard: catches Throwable, treats
already-loaded as success, logs through Log, returns availability.
Each wrapper is one line; the unguarded five gain an available
flag. networklocation's three wrappers still each load (harmless
idempotent) but through the same guard.

Deliberately untouched: KartNative/YuuAvatarNative (load +
nativeInit are one try), SqlCipher's own idempotent loader,
WebRtcInit (initialisation flow, not a wrapper), and maps'
OfflineRouter (its init also starts the tile server).

Verified: :library:log:testDebugUnitTest incl. new NativeLoader
test, and compile of all 23 touched app/library modules.

Diff

appstore/src/main/java/com/vayunmathur/appstore/data/accrescent/SignifyNative.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/accrescent/SignifyNative.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/accrescent/SignifyNative.ktindex ab32ba54f8..49a1f83bbf 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/accrescent/SignifyNative.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/accrescent/SignifyNative.kt@@ -1,6 +1,8 @@ // PACKAGE STRUCTURE EXCEPTION (JNI): FQN frozen for native RegisterNatives/symbol mangling package com.vayunmathur.appstore.data.accrescent +import com.vayunmathur.library.log.NativeLoader+ /**  * JNI surface for the native ed25519 verifier (libappstore_signify.so), backed  * by the Rust `ed25519-dalek` crate. Replaces Bouncy Castle, whose only use@@ -20,5 +22,5 @@ internal object SignifyNative {      * crash class initialisation with an [UnsatisfiedLinkError], which is      * an [Error] and escapes every `catch (Exception)` fail-closed path.      */-    val available: Boolean = runCatching { System.loadLibrary("appstore_signify") }.isSuccess+    val available: Boolean = NativeLoader.load("appstore_signify", "SignifyNative") }
astronomy/src/main/java/com/vayunmathur/astronomy/domain/AstronomyNative.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/AstronomyNative.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/AstronomyNative.ktindex 693ec6b3f8..710905e12a 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/AstronomyNative.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/AstronomyNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.astronomy.domain +import com.vayunmathur.library.log.NativeLoader+ /**  * JNI bridge to the native `astronomy_engine` Rust library: the per-tick /  * per-frame hot paths (batch RaDec -> AltAz, batch stereographic projection).@@ -15,7 +17,7 @@ object AstronomyNative {      * callers that have a Kotlin equivalent (see `projectAll`) check this and fall back      * rather than dying with an [UnsatisfiedLinkError].      */-    val available: Boolean = runCatching { System.loadLibrary("astronomy_engine") }.isSuccess+    val available: Boolean = NativeLoader.load("astronomy_engine", "AstronomyNative")      /**      * Batch RaDec -> AltAz. [radec] is interleaved `[ra0,dec0,ra1,dec1,...]` in
camera/src/main/java/com/vayunmathur/camera/platform/StitchNative.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/platform/StitchNative.kt b/camera/src/main/java/com/vayunmathur/camera/platform/StitchNative.ktindex 93d329cbf6..afe323bed8 100644--- a/camera/src/main/java/com/vayunmathur/camera/platform/StitchNative.kt+++ b/camera/src/main/java/com/vayunmathur/camera/platform/StitchNative.kt@@ -1,6 +1,7 @@ package com.vayunmathur.camera.platform -import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader+  /**  * JNI bridge to the native `camera_stitch` Rust library (feature-based panorama@@ -9,17 +10,7 @@ import com.vayunmathur.library.log.Log  * Usage: newSession() -> addFrame()* -> stitch()/merge() -> free().  */ object StitchNative {-    val isAvailable: Boolean = try {-        System.loadLibrary("camera_stitch")-        Log.status("StitchNative", "libcamera_stitch loaded")-        true-    } catch (e: UnsatisfiedLinkError) {-        Log.error("StitchNative", "System.loadLibrary(camera_stitch) failed", e)-        false-    } catch (e: SecurityException) {-        Log.error("StitchNative", "System.loadLibrary(camera_stitch) failed", e)-        false-    }+    val isAvailable: Boolean = NativeLoader.load("camera_stitch", "StitchNative")      /** Opens a stitch session. [sphere] selects full-sphere vs flat panorama. */     external fun newSession(sphere: Boolean): Long
code/src/main/java/com/vayunmathur/code/util/TreeSitterNative.kt
diff --git a/code/src/main/java/com/vayunmathur/code/util/TreeSitterNative.kt b/code/src/main/java/com/vayunmathur/code/util/TreeSitterNative.ktindex da5d7543c9..e680a093a1 100644--- a/code/src/main/java/com/vayunmathur/code/util/TreeSitterNative.kt+++ b/code/src/main/java/com/vayunmathur/code/util/TreeSitterNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.code.util +import com.vayunmathur.library.log.NativeLoader+ // PACKAGE STRUCTURE EXCEPTION (JNI) // This file stays in `util` on purpose: the Rust side binds the class by its // fully-qualified name (Java_com_vayunmathur_code_util_TreeSitterNative_highlight@@ -7,7 +9,6 @@ package com.vayunmathur.code.util // and silently drop tree-sitter highlighting to the regex fallback.  import com.vayunmathur.code.domain.syntax.Language-import com.vayunmathur.library.log.Log  /** A tree-sitter capture kind, aligned 1:1 (by ordinal) with the Rust side's enum. */ enum class TsKind { KEYWORD, STRING, NUMBER, COMMENT, ANNOTATION, FUNCTION, TYPE, PROPERTY }@@ -26,17 +27,7 @@ data class TsSpan(val start: Int, val end: Int, val kind: TsKind)  */ object TreeSitterNative { -    val isAvailable: Boolean =-        try {-            System.loadLibrary("code_ts")-            true-        } catch (e: UnsatisfiedLinkError) {-            Log.status("TreeSitter", "native lib missing, using regex highlighting", e)-            false-        } catch (e: SecurityException) {-            Log.status("TreeSitter", "native lib blocked, using regex highlighting", e)-            false-        }+    val isAvailable: Boolean = NativeLoader.load("code_ts", "TreeSitterNative")      /**      * Highlights [source] for [languageId] (see [languageIdFor]), returning packed
communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RustMlsCrypto.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RustMlsCrypto.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RustMlsCrypto.ktindex 2d629d9d7c..a91d78ea36 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RustMlsCrypto.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RustMlsCrypto.kt@@ -1,6 +1,7 @@ package com.vayunmathur.communicate.data.rcs.e2e -import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader+  /**  * JNI bridge to the `communicate_mls` Rust crate (OpenMLS, RFC 9420).@@ -20,19 +21,7 @@ object RustMlsCrypto {      private const val TAG = "RustMlsCrypto" -    val isAvailable: Boolean = try {-        System.loadLibrary("communicate_mls")-        Log.status(TAG, "libcommunicate_mls loaded")-        true-    } catch (expected: Throwable) {-        if (expected.message?.contains("already loaded", ignoreCase = true) == true) {-            Log.status(TAG, "libcommunicate_mls already loaded")-            true-        } else {-            Log.error(TAG, "System.loadLibrary(communicate_mls) failed", expected)-            false-        }-    }+    val isAvailable: Boolean = NativeLoader.load("communicate_mls", TAG)      /** Generate an identity for `e164` UTF-8 bytes. Returns `[empty, identity]`. */     @JvmStatic external fun generateIdentity(e164: ByteArray): Array<ByteArray>?
communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/RustSignalCrypto.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/RustSignalCrypto.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/RustSignalCrypto.ktindex 767abc5bf8..55a4bf62de 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/RustSignalCrypto.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/RustSignalCrypto.kt@@ -1,6 +1,7 @@ package com.vayunmathur.communicate.data.signal.e2e -import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader+  /**  * JNI bridge to the Rust Signal crate + libsignal-android Java layer.@@ -21,19 +22,7 @@ object RustSignalCrypto {     private const val PRIVATE_KEY_SIZE = 32     private const val ENCRYPT_PARTS = 3 -    val isAvailable: Boolean = try {-        System.loadLibrary("communicate_signal")-        Log.status(TAG, "libcommunicate_signal loaded (Signal)")-        true-    } catch (expected: Throwable) {-        if (expected.message?.contains("already loaded", ignoreCase = true) == true) {-            Log.status(TAG, "libcommunicate_signal already loaded")-            true-        } else {-            Log.error(TAG, "System.loadLibrary(communicate_signal) failed", expected)-            false-        }-    }+    val isAvailable: Boolean = NativeLoader.load("communicate_signal", TAG)      // -- Primitive key operations (reused from crypto.rs) -- 
communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/e2e/RustWhatsAppCrypto.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/e2e/RustWhatsAppCrypto.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/e2e/RustWhatsAppCrypto.ktindex ab313a68e4..5590480875 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/e2e/RustWhatsAppCrypto.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/whatsapp/e2e/RustWhatsAppCrypto.kt@@ -1,6 +1,7 @@ package com.vayunmathur.communicate.data.whatsapp.e2e -import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader+  /**  * JNI wrapper around `libwhatsapp_signal.so` (Rust crate `whatsapp_signal`).@@ -16,15 +17,7 @@ object RustWhatsAppCrypto {     private const val PRIVATE_KEY_SIZE = 32     private const val ENCRYPT_PARTS = 3 -    val isAvailable: Boolean = try {-        System.loadLibrary("communicate_signal")-        Log.status(TAG, "libwhatsapp_signal loaded")-        true-    } catch (expected: Throwable) {-        Log.error(TAG, "System.loadLibrary(communicate_signal) failed", expected)-        false-    }-+    val isAvailable: Boolean = NativeLoader.load("communicate_signal", TAG)     // -- Primitive key operations --      /**
euicc/src/main/java/com/vayunmathur/euicc/EuiccNative.kt
diff --git a/euicc/src/main/java/com/vayunmathur/euicc/EuiccNative.kt b/euicc/src/main/java/com/vayunmathur/euicc/EuiccNative.ktindex fce144e6c8..efa267e396 100644--- a/euicc/src/main/java/com/vayunmathur/euicc/EuiccNative.kt+++ b/euicc/src/main/java/com/vayunmathur/euicc/EuiccNative.kt@@ -1,6 +1,8 @@ // PACKAGE STRUCTURE EXCEPTION (JNI): FQN frozen for native RegisterNatives/symbol mangling package com.vayunmathur.euicc +import com.vayunmathur.library.log.NativeLoader+ /**  * JNI entry points into the native SGP.22 core (libeuicc.so).  *@@ -14,9 +16,7 @@ package com.vayunmathur.euicc  * [activeChannel] for the duration of the block.  */ object EuiccNative {-    init {-        System.loadLibrary("euicc")-    }+    val available: Boolean = NativeLoader.load("euicc", "EuiccNative")      /**      * The transmit function for the currently open ISD-R logical channel, or
games/voxels/src/main/java/com/vayunmathur/games/voxels/platform/VoxelsNative.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/platform/VoxelsNative.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/platform/VoxelsNative.ktindex 7e8cdbf34d..9d4b6566df 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/platform/VoxelsNative.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/platform/VoxelsNative.kt@@ -3,7 +3,8 @@ @file:Suppress("InvalidPackageDeclaration") package com.vayunmathur.games.voxels.util -import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader+ import android.view.Surface  // JNI bridge: one extern fun per native entry point (count is inherent to the@@ -11,14 +12,7 @@ import android.view.Surface /** JNI bridge to the Rust Voxels engine (`voxels_engine`). */ @Suppress("TooManyFunctions", "TooGenericExceptionCaught") object VoxelsNative {-    val isAvailable: Boolean = try {-        System.loadLibrary("voxels_engine")-        Log.status("VoxelsNative", "libvoxels_engine loaded")-        true-    } catch (t: Throwable) {-        Log.error("VoxelsNative", "System.loadLibrary(voxels_engine) failed", t)-        false-    }+    val isAvailable: Boolean = NativeLoader.load("voxels_engine", "VoxelsNative")     // Starts loading the world on the engine thread and returns at once; poll nativeLoadState.     external fun nativeInit(filesDir: String, seed: Int): Boolean     // 0 idle, 1 generating, 2 meshing, 3 ready (see ui/LoadingOverlay.kt LoadPhase).
library/e2ee-openpgp/src/main/java/com/vayunmathur/e2ee/PgpNative.kt
diff --git a/library/e2ee-openpgp/src/main/java/com/vayunmathur/e2ee/PgpNative.kt b/library/e2ee-openpgp/src/main/java/com/vayunmathur/e2ee/PgpNative.ktindex 3d2ee1513a..aba5415759 100644--- a/library/e2ee-openpgp/src/main/java/com/vayunmathur/e2ee/PgpNative.kt+++ b/library/e2ee-openpgp/src/main/java/com/vayunmathur/e2ee/PgpNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.e2ee +import com.vayunmathur.library.log.NativeLoader+ // PACKAGE STRUCTURE EXCEPTION (JNI)  /**@@ -39,7 +41,5 @@ internal object PgpNative {         data: ByteArray,     ): ByteArray? -    init {-        System.loadLibrary("e2ee_pgp")-    }+    val available: Boolean = NativeLoader.load("e2ee_pgp", "PgpNative") }
library/e2ee-p2p/src/main/java/com/vayunmathur/e2ee/PqcNative.kt
diff --git a/library/e2ee-p2p/src/main/java/com/vayunmathur/e2ee/PqcNative.kt b/library/e2ee-p2p/src/main/java/com/vayunmathur/e2ee/PqcNative.ktindex 184dd9eeb5..51b80e2b47 100644--- a/library/e2ee-p2p/src/main/java/com/vayunmathur/e2ee/PqcNative.kt+++ b/library/e2ee-p2p/src/main/java/com/vayunmathur/e2ee/PqcNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.e2ee +import com.vayunmathur.library.log.NativeLoader+ /**  * JNI surface for the native Rust PQC library (libe2ee_pqc.so): ML-KEM-768 +  * ML-DSA-65. All keys are DER (X.509 SPKI / PKCS#8), byte-compatible with the@@ -16,7 +18,5 @@ internal object PqcNative {     external fun nativeMldsaSign(privDer: ByteArray, msg: ByteArray): ByteArray?     external fun nativeMldsaVerify(pubDer: ByteArray, msg: ByteArray, sig: ByteArray): Boolean -    init {-        System.loadLibrary("e2ee_pqc")-    }+    val available: Boolean = NativeLoader.load("e2ee_pqc", "PqcNative") }
library/log/AGENTS.md
diff --git a/library/log/AGENTS.md b/library/log/AGENTS.mdindex 32e6abdd86..2ddf2af225 100644--- a/library/log/AGENTS.md+++ b/library/log/AGENTS.md@@ -18,6 +18,7 @@  ## Key files - com/vayunmathur/library/log/Log.kt — the facade (use this, never `android.util.Log`)+- com/vayunmathur/library/log/NativeLoader.kt — the one guarded `System.loadLibrary` caller; every `<Feature>Native` wrapper loads through it and exposes the returned availability flag - com/vayunmathur/library/log/MaInitProvider.kt — dev-gate init via manifest merger  ## Verify (this module only)
library/log/src/main/java/com/vayunmathur/library/log/NativeLoader.kt
diff --git a/library/log/src/main/java/com/vayunmathur/library/log/NativeLoader.kt b/library/log/src/main/java/com/vayunmathur/library/log/NativeLoader.ktnew file mode 100644index 0000000000..a3b3aa8538--- /dev/null+++ b/library/log/src/main/java/com/vayunmathur/library/log/NativeLoader.kt@@ -0,0 +1,48 @@+package com.vayunmathur.library.log++/**+ * The one guarded way to load a native library.+ *+ * Every `<Feature>Native` wrapper in the repo used to hand-roll this:+ * twenty-plus copies in four variants (a `runCatching` one-liner, a+ * try/catch that logs, a silent try/catch, and — worst — an unguarded+ * `init { System.loadLibrary(...) }` in photos, euicc, passwords,+ * the e2ee libraries and youpipe, where a missing `.so` poisons the+ * class with `ExceptionInInitializerError` on every later touch).+ * Two of the hand-rolled variants also caught only `Exception`, which+ * lets `UnsatisfiedLinkError` — an `Error` — escape the guard entirely.+ *+ * [load] never throws: it catches `Throwable`, treats an+ * already-loaded report as success (the same library can be reached+ * through more than one class loader path), records the outcome+ * through [Log], and returns whether the library is usable, so a+ * wrapper becomes `val available = NativeLoader.load("pdf_render",+ * "PdfNative")` and its callers can fall back instead of crashing.+ *+ * Lives in `:library:log` because that is the one module every app+ * and library module already depends on; JNI-frozen wrapper classes+ * stay in their apps (their names are symbol-mangling contracts) —+ * only this guard is shared.+ */+object NativeLoader {++    /**+     * Load the library named [name] (without the `lib` prefix or+     * `.so` suffix), logging under [tag]. Returns true when the+     * library is loaded and usable, false when it is absent or was+     * refused; never throws.+     */+    fun load(name: String, tag: String): Boolean = try {+        System.loadLibrary(name)+        Log.status(tag, "lib$name loaded")+        true+    } catch (failure: Throwable) {+        if (failure.message?.contains("already loaded", ignoreCase = true) == true) {+            Log.status(tag, "lib$name already loaded")+            true+        } else {+            Log.error(tag, "System.loadLibrary($name) failed", failure)+            false+        }+    }+}
library/log/src/test/java/com/vayunmathur/library/log/NativeLoaderTest.kt
diff --git a/library/log/src/test/java/com/vayunmathur/library/log/NativeLoaderTest.kt b/library/log/src/test/java/com/vayunmathur/library/log/NativeLoaderTest.ktnew file mode 100644index 0000000000..296b293ac5--- /dev/null+++ b/library/log/src/test/java/com/vayunmathur/library/log/NativeLoaderTest.kt@@ -0,0 +1,28 @@+package com.vayunmathur.library.log++import kotlin.test.Test+import kotlin.test.assertFalse+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import org.robolectric.shadows.ShadowLog++/**+ * The shared native-load guard: a library that does not exist (every+ * library, on a host JVM) must answer false and log the failure rather+ * than throwing — that is the whole point of routing the twenty-plus+ * hand-rolled `<Feature>Native` load guards through here.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class NativeLoaderTest {++    @Test+    fun missingLibraryReturnsFalseAndLogsInsteadOfThrowing() {+        assertFalse(NativeLoader.load("definitely_not_a_real_library_zzz", "NativeLoaderTest"))+        val logs = ShadowLog.getLogsForTag("NativeLoaderTest")+        assert(logs.any { it.msg.contains("definitely_not_a_real_library_zzz") }) {+            "expected the failed load to be logged, got $logs"+        }+    }+}
library/map/src/main/java/com/vayunmathur/library/map/MapNative.kt
diff --git a/library/map/src/main/java/com/vayunmathur/library/map/MapNative.kt b/library/map/src/main/java/com/vayunmathur/library/map/MapNative.ktindex dc86812056..f695549c66 100644--- a/library/map/src/main/java/com/vayunmathur/library/map/MapNative.kt+++ b/library/map/src/main/java/com/vayunmathur/library/map/MapNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.library.map +import com.vayunmathur.library.log.NativeLoader+ import android.view.Surface  /**@@ -28,12 +30,7 @@ internal object MapNative {      * False on a device whose ABI we did not build for. The caller shows the background      * colour rather than crashing.      */-    val isAvailable: Boolean = try {-        System.loadLibrary("map_renderer")-        true-    } catch (_: Throwable) {-        false-    }+    val isAvailable: Boolean = NativeLoader.load("map_renderer", "MapNative")      /**      * Bring up Vulkan on [surface]. Returns an opaque handle, or 0 on failure.
library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.kt
diff --git a/library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.kt b/library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.ktindex 6f0de637d5..2f66f09978 100644--- a/library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.kt+++ b/library/ml/src/main/java/com/vayunmathur/library/ml/MlNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.library.ml +import com.vayunmathur.library.log.NativeLoader+ /**  * The JNI surface of the Vulkan compute runtime in `library/ml/src/main/rust`.  *@@ -28,12 +30,7 @@ internal object MlNative {      * Vulkan or fp16 is available — that is only known once a create call is attempted,      * because it needs a `VkDevice` to ask.      */-    val isAvailable: Boolean = try {-        System.loadLibrary("modelrunner")-        true-    } catch (_: Throwable) {-        false-    }+    val isAvailable: Boolean = NativeLoader.load("modelrunner", "MlNative")      /**      * Bring up MediaPipe Selfie Segmentation from a `.maml` blob. Returns 0 on failure.
measure/src/main/java/com/vayunmathur/measure/platform/vio/MeasureCvNative.kt
diff --git a/measure/src/main/java/com/vayunmathur/measure/platform/vio/MeasureCvNative.kt b/measure/src/main/java/com/vayunmathur/measure/platform/vio/MeasureCvNative.ktindex 627c6cd917..8b5f186911 100644--- a/measure/src/main/java/com/vayunmathur/measure/platform/vio/MeasureCvNative.kt+++ b/measure/src/main/java/com/vayunmathur/measure/platform/vio/MeasureCvNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.measure.platform.vio +import com.vayunmathur.library.log.NativeLoader+ import com.vayunmathur.measure.domain.Pose3 import com.vayunmathur.measure.domain.Vec3 @@ -38,9 +40,7 @@ data class CvFrameFlow(  */ object MeasureCvNative { -    val available: Boolean by lazy {-        runCatching { System.loadLibrary("measure_cv") }.isSuccess-    }+    val available: Boolean by lazy { NativeLoader.load("measure_cv", "MeasureCvNative") }      fun createSession(fx: Double, fy: Double, cx: Double, cy: Double): Long =         if (!available) 0L else nativeCreateSession(fx, fy, cx, cy)
networklocation/src/main/java/com/vayunmathur/networklocation/GeocoderNative.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/GeocoderNative.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/GeocoderNative.ktindex 8b6d0ff7aa..2d2d1329c7 100644--- a/networklocation/src/main/java/com/vayunmathur/networklocation/GeocoderNative.kt+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/GeocoderNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.networklocation +import com.vayunmathur.library.log.NativeLoader+ // PACKAGE STRUCTURE EXCEPTION (JNI)  /**@@ -62,7 +64,7 @@ object GeocoderNative {     const val KIND_PLACE = 4      /** Whether the `.so` loaded. Guarded so host/unit contexts degrade gracefully. */-    val available: Boolean = runCatching { System.loadLibrary("networklocation") }.isSuccess+    val available: Boolean = NativeLoader.load("networklocation", "GeocoderNative")      /**      * Open the geocoder DB from a file descriptor. [offset] is the database's start offset
networklocation/src/main/java/com/vayunmathur/networklocation/NetworkLocationNative.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/NetworkLocationNative.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/NetworkLocationNative.ktindex d22900205b..b0a7ec3065 100644--- a/networklocation/src/main/java/com/vayunmathur/networklocation/NetworkLocationNative.kt+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/NetworkLocationNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.networklocation +import com.vayunmathur.library.log.NativeLoader+ // PACKAGE STRUCTURE EXCEPTION (JNI)  /**@@ -15,7 +17,7 @@ package com.vayunmathur.networklocation object NetworkLocationNative {     /** Whether the `.so` loaded. Always true on device; guarded so host/unit      * contexts without the native library degrade gracefully. */-    val available: Boolean = runCatching { System.loadLibrary("networklocation") }.isSuccess+    val available: Boolean = NativeLoader.load("networklocation", "NetworkLocationNative")      /**      * Estimate the device position from beacon fixes.
networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.ktindex c272186c06..edf3386f7d 100644--- a/networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.kt+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.networklocation +import com.vayunmathur.library.log.NativeLoader+ // PACKAGE STRUCTURE EXCEPTION (JNI)  /**@@ -19,7 +21,7 @@ package com.vayunmathur.networklocation  */ object WpsStoreNative {     /** Whether the `.so` loaded. Guarded so host/unit contexts degrade gracefully. */-    val available: Boolean = runCatching { System.loadLibrary("networklocation") }.isSuccess+    val available: Boolean = NativeLoader.load("networklocation", "WpsStoreNative")      /**      * Open a WPSDB store from a file descriptor. [offset] is the store's start offset within
office/src/main/java/com/vayunmathur/office/util/OfficeNative.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeNative.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeNative.ktindex b14289c62b..77800f9a83 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeNative.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.office.util +import com.vayunmathur.library.log.NativeLoader+ import com.vayunmathur.office.domain.DocumentTreeCrdt import com.vayunmathur.office.platform.OfficeViewModel // PACKAGE STRUCTURE EXCEPTION (JNI)@@ -36,13 +38,7 @@ import kotlinx.serialization.json.Json  */ object OfficeNative {     /** False when the native lib is absent for this ABI; callers must fall back, not crash. */-    val isAvailable: Boolean = try {-        System.loadLibrary("office_engine")-        true-    } catch (expected: UnsatisfiedLinkError) {-        false-    }-+    val isAvailable: Boolean = NativeLoader.load("office_engine", "OfficeNative")     // ---- Document tree CRDT ----      /** Creates a native CRDT for [device]; returns a handle (0 on failure). */
passwords/src/main/java/com/vayunmathur/passwords/util/KdbxNative.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/util/KdbxNative.kt b/passwords/src/main/java/com/vayunmathur/passwords/util/KdbxNative.ktindex 4604657759..8762370e29 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/util/KdbxNative.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/util/KdbxNative.kt@@ -1,6 +1,8 @@ // PACKAGE STRUCTURE EXCEPTION (JNI): FQN frozen for native RegisterNatives/symbol mangling package com.vayunmathur.passwords.util +import com.vayunmathur.library.log.NativeLoader+ /**  * JNI surface for the native Rust KDBX library (libpasswords_kdbx.so), backed  * by the Rust `keepass` crate. Replaces keepassjava2 + Bouncy Castle.@@ -14,7 +16,5 @@ internal object KdbxNative {     external fun nativeImport(password: String, kdbx: ByteArray): String?     external fun nativeExport(password: String, entriesJson: String): ByteArray? -    init {-        System.loadLibrary("passwords_kdbx")-    }+    val available: Boolean = NativeLoader.load("passwords_kdbx", "KdbxNative") }
pdf/src/main/java/com/vayunmathur/pdf/util/PdfNative.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/util/PdfNative.kt b/pdf/src/main/java/com/vayunmathur/pdf/util/PdfNative.ktindex 287cc59520..9a6ceea07c 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/util/PdfNative.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/util/PdfNative.kt@@ -1,7 +1,8 @@ // PACKAGE STRUCTURE EXCEPTION (JNI): FQN frozen for native symbol mangling // (Java_com_vayunmathur_pdf_util_PdfNative_* in Rust jni_bindings) package com.vayunmathur.pdf.util-import com.vayunmathur.library.log.Log++import com.vayunmathur.library.log.NativeLoader  /**  * JNI bridge to the native Rust PDF renderer (`libpdf_render.so`, built from@@ -18,15 +19,7 @@ import com.vayunmathur.library.log.Log // so moving any declaration to another class breaks its native binding. object PdfNative { -    val isAvailable: Boolean =-        try {-            System.loadLibrary("pdf_render")-            Log.status("PdfNative", "libpdf_render loaded")-            true-        } catch (expected: UnsatisfiedLinkError) {-            Log.error("PdfNative", "System.loadLibrary(pdf_render) failed", expected)-            false-        }+    val isAvailable: Boolean = NativeLoader.load("pdf_render", "PdfNative")      /**      * Parse [data] (the raw PDF bytes) and return an opaque handle, or 0 on
photos/src/main/java/com/vayunmathur/photos/platform/PhotosNative.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/PhotosNative.kt b/photos/src/main/java/com/vayunmathur/photos/platform/PhotosNative.ktindex d7822f849e..d15f4481d9 100644--- a/photos/src/main/java/com/vayunmathur/photos/platform/PhotosNative.kt+++ b/photos/src/main/java/com/vayunmathur/photos/platform/PhotosNative.kt@@ -1,5 +1,7 @@ package com.vayunmathur.photos.platform +import com.vayunmathur.library.log.NativeLoader+ /**  * JNI bridge to the native `photos_fx` Rust library: performance-critical  * pixel-processing filters (stylize, unsharp mask, liquify, blur, content-aware@@ -7,9 +9,7 @@ package com.vayunmathur.photos.platform  * returns them.  */ object PhotosNative {-    init {-        System.loadLibrary("photos_fx")-    }+    val available: Boolean = NativeLoader.load("photos_fx", "PhotosNative")      /** Stylize filter. mode: 0=None, 1=FindEdges (Sobel), 2=Emboss. */     external fun stylize(pixels: IntArray, w: Int, h: Int, mode: Int): IntArray
share/src/main/java/com/vayunmathur/share/protocol/ShareNative.kt
diff --git a/share/src/main/java/com/vayunmathur/share/protocol/ShareNative.kt b/share/src/main/java/com/vayunmathur/share/protocol/ShareNative.ktindex be8170c3cb..bcf86157c6 100644--- a/share/src/main/java/com/vayunmathur/share/protocol/ShareNative.kt+++ b/share/src/main/java/com/vayunmathur/share/protocol/ShareNative.kt@@ -1,6 +1,8 @@ // PACKAGE STRUCTURE EXCEPTION (JNI): FQN frozen for native RegisterNatives/symbol mangling package com.vayunmathur.share.protocol +import com.vayunmathur.library.log.NativeLoader+ import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json @@ -122,11 +124,7 @@ internal object ShareNative {         // turns every later access into NoClassDefFoundError, which the call sites'         // catch (UnsatisfiedLinkError) cannot match. A failed load leaves the class         // usable; native calls then throw UnsatisfiedLinkError, which they do catch.-        try {-            System.loadLibrary("share_nearby")-        } catch (_: UnsatisfiedLinkError) {-            // Native calls fail individually and are handled at their call sites.-        }+        NativeLoader.load("share_nearby", "ShareNative")     }      /** `p000\eanu.java:8` — the device type `:share` advertises. */
share/src/main/java/com/vayunmathur/share/protocol/ShareNativeDiscovery.kt
diff --git a/share/src/main/java/com/vayunmathur/share/protocol/ShareNativeDiscovery.kt b/share/src/main/java/com/vayunmathur/share/protocol/ShareNativeDiscovery.ktindex f0f6242fcf..77a272d9c5 100644--- a/share/src/main/java/com/vayunmathur/share/protocol/ShareNativeDiscovery.kt+++ b/share/src/main/java/com/vayunmathur/share/protocol/ShareNativeDiscovery.kt@@ -1,6 +1,8 @@ // PACKAGE STRUCTURE EXCEPTION (JNI): FQN frozen for native RegisterNatives/symbol mangling package com.vayunmathur.share.protocol +import com.vayunmathur.library.log.NativeLoader+ import kotlinx.serialization.json.Json  /**@@ -160,10 +162,6 @@ internal object ShareNativeDiscovery {         // turns every later access into NoClassDefFoundError, which the call sites'         // catch (UnsatisfiedLinkError) cannot match. A failed load leaves the class         // usable; native calls then throw UnsatisfiedLinkError, which they do catch.-        try {-            System.loadLibrary("share_nearby")-        } catch (_: UnsatisfiedLinkError) {-            // Native calls fail individually and are handled at their call sites.-        }+        NativeLoader.load("share_nearby", "ShareNative")     } }
vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.kt b/vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.ktindex 22307f1357..98fbfdef5a 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.kt@@ -1,6 +1,6 @@ // PACKAGE STRUCTURE EXCEPTION (JNI): FQN frozen for native RegisterNatives/symbol mangling package com.vayunmathur.vpn.util-import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader  /**  * JNI bridge to the native `vpn_wireguard` Rust library.@@ -19,20 +19,7 @@ object VpnNative {      * with NoClassDefFoundError/ExceptionInInitializerError instead.      * Callers that need a tunnel must check this before calling natives.      */-    val isLoaded: Boolean--    init {-        isLoaded = try {-            System.loadLibrary("vpn_wireguard")-            true-        } catch (expected: UnsatisfiedLinkError) {-            Log.error("VpnNative", "System.loadLibrary(vpn_wireguard) failed", expected)-            false-        } catch (expected: SecurityException) {-            Log.error("VpnNative", "System.loadLibrary(vpn_wireguard) blocked", expected)-            false-        }-    }+    val isLoaded: Boolean = NativeLoader.load("vpn_wireguard", "VpnNative")      /** Dummy init so we force class load on cold start */     @JvmStatic external fun init()
weather/src/main/java/com/vayunmathur/weather/domain/map/OmTilesNative.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/domain/map/OmTilesNative.kt b/weather/src/main/java/com/vayunmathur/weather/domain/map/OmTilesNative.ktindex 11bc226a61..d2c19b766b 100644--- a/weather/src/main/java/com/vayunmathur/weather/domain/map/OmTilesNative.kt+++ b/weather/src/main/java/com/vayunmathur/weather/domain/map/OmTilesNative.kt@@ -1,5 +1,6 @@ package com.vayunmathur.weather.domain.map-import com.vayunmathur.library.log.Log++import com.vayunmathur.library.log.NativeLoader  /**  * JNI bridge to the native Rust `.om` decoder (`libweather_om.so`, built from@@ -8,21 +9,9 @@ import com.vayunmathur.library.log.Log  * instead of crashing.  */ object OmTilesNative {-    // Broad catch is deliberate: loadLibrary throws UnsatisfiedLinkError (an Error,+    // Guarded by NativeLoader: loadLibrary throws UnsatisfiedLinkError (an Error,     // not an Exception) plus undocumented RuntimeExceptions on some ABIs.-    @Suppress("TooGenericExceptionCaught")-    val isAvailable: Boolean =-        try {-            System.loadLibrary("weather_om")-            Log.status("OmMap", "libweather_om loaded")-            true-        } catch (e: Exception) {-            Log.error("OmMap", "System.loadLibrary(weather_om) failed", e)-            false-        } catch (e: UnsatisfiedLinkError) {-            Log.error("OmMap", "System.loadLibrary(weather_om) failed", e)-            false-        }+    val isAvailable: Boolean = NativeLoader.load("weather_om", "OmTilesNative")      /**      * Decode [variable] from the `.om` file at [omUrl] over the bounding box
web/src/main/java/com/vayunmathur/web/shields/ShieldsNative.kt
diff --git a/web/src/main/java/com/vayunmathur/web/shields/ShieldsNative.kt b/web/src/main/java/com/vayunmathur/web/shields/ShieldsNative.ktindex ab7ffcb522..c9e7983b72 100644--- a/web/src/main/java/com/vayunmathur/web/shields/ShieldsNative.kt+++ b/web/src/main/java/com/vayunmathur/web/shields/ShieldsNative.kt@@ -1,6 +1,7 @@ // PACKAGE STRUCTURE EXCEPTION (JNI): FQN frozen for native RegisterNatives/symbol mangling package com.vayunmathur.web.shields-import com.vayunmathur.library.log.Log++import com.vayunmathur.library.log.NativeLoader  /**  * JNI bridge to Brave's adblock engine (`libweb_shields.so`, built from@@ -13,13 +14,7 @@ import com.vayunmathur.library.log.Log  */ internal object ShieldsNative { -    val isAvailable: Boolean =-        runCatching {-            System.loadLibrary("web_shields")-            true-        }.onFailure { t ->-            Log.error("ShieldsNative", "System.loadLibrary(web_shields) failed", t)-        }.getOrDefault(false)+    val isAvailable: Boolean = NativeLoader.load("web_shields", "ShieldsNative")      /** Parses `filters` (Adblock Plus syntax) and returns an engine handle, or 0. */     external fun nativeCreate(filters: String, resourcesJson: String): Long
youpipe/src/main/java/com/vayunmathur/youpipe/nativeext/YouPipeNative.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/nativeext/YouPipeNative.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/nativeext/YouPipeNative.ktindex c91b5e4092..fa99433330 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/nativeext/YouPipeNative.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/nativeext/YouPipeNative.kt@@ -1,6 +1,8 @@ // PACKAGE STRUCTURE EXCEPTION (JNI) package com.vayunmathur.youpipe.nativeext +import com.vayunmathur.library.log.NativeLoader+ /**  * JNI surface of the Rust extractor (`youpipe/src/main/rust`).  *@@ -18,9 +20,7 @@ package com.vayunmathur.youpipe.nativeext  */ internal object YouPipeNative { -    init {-        System.loadLibrary("youpipe_extractor")-    }+    val available: Boolean = NativeLoader.load("youpipe_extractor", "YouPipeNative")      /** @param filter one of `videos`, `channels`, `playlists`, or null for everything. */     external fun search(