Skip to content

Fix SIGABRT on FindClass due to minification #114

Open
ryannickel-autodesk wants to merge 1 commit intosquare:mainfrom
ryannickel-autodesk:main
Open

Fix SIGABRT on FindClass due to minification #114
ryannickel-autodesk wants to merge 1 commit intosquare:mainfrom
ryannickel-autodesk:main

Conversation

@ryannickel-autodesk
Copy link

Problem

When an Android app using this library enables R8 minification/obfuscation (typical for release builds), the JNI native code in ZstdKmp.cpp crashes because it uses string-based reflection (FindClass, GetFieldID) to look up Kotlin classes and fields by name — names that R8 has renamed.

Fix

  1. Removed the JniZstd C++ class, its constructor, and the createJniZstd() function which contained string based reflection calls. Replaced jniZstdPointer with jniLibraryLoaded (a lazy Unit that calls loadNativeLibrary() exactly once, thread-safely).
  2. Changed compressStream2 and decompressStream to return a jlongArray of 3 elements [result, inputBytesProcessed, outputBytesProcessed] instead of returning a single jlong and writing byte counts back to Java fields via SetIntField. This eliminates the need for field ID lookups entirely. Added StreamResult class which is a Kotlin wrapper around the LongArray returned by native stream functions. This could be removed and instead just access the result via the indices, but I thought this made the code a bit clearer.
  3. Added ProGuard consumer rules that keeps classes with native methods from being renamed by R8. JNI function names in the compiled .so are derived from fully-qualified class and method names (e.g., Java_com_squareup_zstd_JniZstdCompressor_compressStream2). If R8 renames those classes, the JNI linkage breaks with UnsatisfiedLinkError. This is a separate problem from the FindClass/GetFieldID issue and can only be solved with keep rules. Added consumerProguardFiles to the Android defaultConfig so the keep rules are automatically applied to any app that depends on this library.

Fixes Issue #108

Testing

  • All existing unit tests pass
  • Reproduced SIGABRT in our Android app in builds where minification is enabled
  • Published zstd-kmp with this fix to testMaven, consumed package with this fix in our Android app, verified that the SIGABRT no longer occurs in builds where minification is enabled

Add consumer ProGuard files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant