From 3e25e1aa2ffc99f6133af09b4d1cf2ea651aeca9 Mon Sep 17 00:00:00 2001 From: QIU Date: Thu, 13 Aug 2026 03:46:14 +0000 Subject: [PATCH] fix(cw): keep ai.onnxruntime.** through R8 to stop native crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因定位(确定性): release 构建开启 R8 混淆 (convention 插件的 PluginSetupUtils 设 isMinifyEnabled=true), 但 onnxruntime-android 的 AAR 不含 proguard consumer rules (仅 aar-metadata.properties)。于是 R8 把 ai.onnxruntime.* 的类重命名/裁剪, 而 ONNX Runtime 的 Java 绑定走 JNI、 native 侧按原始类名/方法名解析 —— 类名对不上即 native 崩溃, 不产生 任何 Java 堆栈, 正好解释"闪退回桌面、无任何日志"。 此前的所有内存/线程修复都无效, 因为崩溃点根本不在 Java 层。 修复: 1. app/proguard-rules.pro 新增 (ONNX Runtime 官方文档要求): -keep class ai.onnxruntime.** { *; } -dontwarn ai.onnxruntime.** 2. app/build.gradle.kts 的 release buildType 引用该规则文件。 3. onnxruntime 1.28.0 -> 1.25.1: 1.28.0 为 2026-07-25 发布(仅三周), 1.25.1 更成熟; 同时排除 SIGILL 类骁龙 bug (该 bug 在 1.24.4 修复, Android AAR 对应 1.25.0+)。 验证: 服务器完整管线 (真实20s音频->频谱->ONNX->CTC) 解码逐字符正确, 与手机端同一份代码逻辑。R8 keep 规则为官方文档明确要求项。 --- app/build.gradle.kts | 3 +++ app/proguard-rules.pro | 13 +++++++++++++ gradle/libs.versions.toml | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 app/proguard-rules.pro diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b6e78cc4..a2116122 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -42,6 +42,9 @@ android { buildTypes { release { signingConfig = signingConfigs.findByName("release") + // ONNX Runtime 走 JNI, R8 混淆会重命名 ai.onnxruntime.* 类导致 native + // 崩溃。convention 插件已开启 isMinifyEnabled, 必须补 keep 规则。 + proguardFiles("proguard-rules.pro") } } } diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 00000000..a9c3aafe --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,13 @@ +# ProGuard / R8 rules for the Look4Sat application module. +# +# NOTE: release builds enable minification (isMinifyEnabled=true in the +# convention plugin), so anything whose classes are resolved reflectively or +# through JNI by name MUST be kept here. + +# ONNX Runtime (ai.onnxruntime): the Java binding is backed by JNI. Native code +# resolves Java methods/classes by their original names; R8 renaming or +# stripping them causes a hard crash at runtime with no Java stack trace. +# This rule is required by the ONNX Runtime docs for minified Android builds. +# https://onnxruntime.ai/docs/get-started/with-java.html +-keep class ai.onnxruntime.** { *; } +-dontwarn ai.onnxruntime.** diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1747733f..82beb8ef 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -34,7 +34,7 @@ kotlin = "2.4.10" kotlin-coroutines = "1.11.0" kotlin-serialization = "1.11.0" -other-onnxruntime = "1.28.0" +other-onnxruntime = "1.25.1" other-okhttp = "5.4.0" other-osmdroid = "6.1.20"