log: NativeLoader logging must never throw
- Commit
7c609e5dd2f9a14131f80ca68ba7d14ce62b700e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 22:30 UTC (16 days ago)
- Parents
5141fc0
CI on d8071e071 revealed the guard's own logging broke it on host JVMs: NativeLoader.load caught UnsatisfiedLinkError, then Log.error reached android.util.Log, which on plain (non- Robolectric) unit tests is an unmocked stub that throws. The RuntimeException escaped load() inside each <Feature>Native class initializer, so first touch threw ExceptionInInitializerError and every later touch NoClassDefFoundError - the opposite of what the guard is for. e2ee-openpgp, e2ee-p2p (surfacing as a misleading 'host lib unavailable'), office, share, securemail, astronomy, code, vpn, findfamily and measure all failed this way; at 494d0c57d the old wrappers caught the link error without touching Android Log. All NativeLoader logging now goes through runCatching helpers: same messages on Android, and load() can never throw out of a class initializer again. NativeLoaderHostJvmTest pins it on the plain host JVM (stub android.jar), exactly the environment where the logger throws. Verified: :library:log 5/5, :library:e2ee-openpgp 10/10, :library:e2ee-p2p 14/14, :office 1063/1063 (incl. the fraction format test this was masking), :share 225/225.
Diff
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.ktindex a3b3aa8538..6171efe9ce 100644--- a/library/log/src/main/java/com/vayunmathur/library/log/NativeLoader.kt+++ b/library/log/src/main/java/com/vayunmathur/library/log/NativeLoader.kt@@ -18,6 +18,11 @@ package com.vayunmathur.library.log * 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.+ * The logging itself is guarded too: on a plain host JVM (unit tests+ * without Robolectric) `android.util.Log` is an unmocked stub that+ * throws, and a logger failure must never escape [load] โ otherwise+ * the very call meant to stop `ExceptionInInitializerError` in a+ * wrapper's class init becomes its cause. * * Lives in `:library:log` because that is the one module every app * and library module already depends on; JNI-frozen wrapper classes@@ -34,15 +39,28 @@ object NativeLoader { */ fun load(name: String, tag: String): Boolean = try { System.loadLibrary(name)- Log.status(tag, "lib$name loaded")+ logStatus(tag, "lib$name loaded") true } catch (failure: Throwable) { if (failure.message?.contains("already loaded", ignoreCase = true) == true) {- Log.status(tag, "lib$name already loaded")+ logStatus(tag, "lib$name already loaded") true } else {- Log.error(tag, "System.loadLibrary($name) failed", failure)+ logError(tag, "System.loadLibrary($name) failed", failure) false } }++ // Logging must never throw out of [load]: `Log` delegates to+ // `android.util.Log`, which throws on a host JVM without Robolectric+ // (and could throw for other stub/shadow reasons). Swallowing the+ // logger's failure keeps the load outcome โ the return value โ as+ // the only signal a wrapper's class init depends on.+ private fun logStatus(tag: String, content: String) {+ runCatching { Log.status(tag, content) }+ }++ private fun logError(tag: String, content: String, failure: Throwable) {+ runCatching { Log.error(tag, content, failure) }+ } }
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.ktindex 296b293ac5..942121b3ee 100644--- a/library/log/src/test/java/com/vayunmathur/library/log/NativeLoaderTest.kt+++ b/library/log/src/test/java/com/vayunmathur/library/log/NativeLoaderTest.kt@@ -26,3 +26,22 @@ class NativeLoaderTest { } } }++/**+ * The regression this guards: consumer modules run plain host-JVM unit+ * tests WITHOUT Robolectric, where `android.util.Log` is an unmocked+ * stub that throws on every call. Routing the wrappers through+ * [NativeLoader] in 086ec10fa put `Log.error` on that path, so a failed+ * load threw out of `load()` and poisoned each wrapper's class init+ * (`ExceptionInInitializerError`, then `NoClassDefFoundError`).+ * Without the Robolectric runner this class runs against the stub+ * android.jar, reproducing that environment: logging does fail here,+ * and `load()` must still return false without throwing.+ */+class NativeLoaderHostJvmTest {++ @Test+ fun missingLibraryReturnsFalseEvenWhenLoggingThrows() {+ assertFalse(NativeLoader.load("definitely_not_a_real_library_zzz", "NativeLoaderHostJvmTest"))+ }+}