Replace JNI string conversion workarounds with direct NewStringUTF and GetStringUTFChars to fix FindClass crash on background threads. - #3566
Merged
Conversation
…d GetStringUTFChars to fix FindClass crash on background threads.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3566 +/- ##
==========================================
- Coverage 81.34% 81.33% -0.02%
==========================================
Files 653 653
Lines 77243 77243
Branches 21919 21919
==========================================
- Hits 62837 62825 -12
- Misses 9975 9981 +6
- Partials 4431 4437 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
shlzxjp
approved these changes
Jul 13, 2026
leiyue123
added a commit
that referenced
this pull request
Jul 14, 2026
…d GetStringUTFChars to fix FindClass crash on background threads. (#3566)
leiyue123
added a commit
that referenced
this pull request
Jul 14, 2026
…d GetStringUTFChars to fix FindClass crash on background threads. (#3566)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
SafeConvertToJString和SafeConvertToStdString使用static懒初始化缓存java/lang/String类,通过FindClass查找。如果首次调用发生在后台线程(如ThreadPoolExecutor),FindClass可能因为线程的 ClassLoader 上下文不完整而返回 null,触发 CheckJNI abort(java_class == null)。相关 issue:#3104、#3175
根因
原实现为了规避 Android 4.x Dalvik 时代
NewStringUTF对 4 字节 UTF-8 字符的崩溃问题,采用了绕路方案:通过FindClass→GetMethodID→NewObject来构造 Java String。但FindClass在不同线程上行为不可靠。libpag 当前 minSdk 为 21(Android 5.0+),运行时已切换为 ART,该
NewStringUTFbug 已在 ART 中修复。修复
SafeConvertToJString:直接用env->NewStringUTF()替代 Java 构造器绕路SafeConvertToStdString:直接用GetStringUTFChars/ReleaseStringUTFChars替代 JavagetBytes()回调改动量:2 文件,+5 -40 行。消除所有
static FindClass依赖,在任何线程调用都安全。