Fix SIGABRT on FindClass due to minification #114
Open
ryannickel-autodesk wants to merge 1 commit intosquare:mainfrom
Open
Fix SIGABRT on FindClass due to minification #114ryannickel-autodesk wants to merge 1 commit intosquare:mainfrom
ryannickel-autodesk wants to merge 1 commit intosquare:mainfrom
Conversation
Add consumer ProGuard files
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.
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
createJniZstd()function which contained string based reflection calls. ReplacedjniZstdPointerwithjniLibraryLoaded(a lazy Unit that callsloadNativeLibrary()exactly once, thread-safely).compressStream2anddecompressStreamto return ajlongArrayof 3 elements[result, inputBytesProcessed, outputBytesProcessed]instead of returning a singlejlongand writing byte counts back to Java fields viaSetIntField. This eliminates the need for field ID lookups entirely. AddedStreamResultclass which is a Kotlin wrapper around theLongArrayreturned 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.Java_com_squareup_zstd_JniZstdCompressor_compressStream2). If R8 renames those classes, the JNI linkage breaks with UnsatisfiedLinkError. This is a separate problem from theFindClass/GetFieldIDissue and can only be solved with keep rules. AddedconsumerProguardFilesto the Android defaultConfig so the keep rules are automatically applied to any app that depends on this library.Fixes Issue #108
Testing
zstd-kmpwith this fix totestMaven, consumed package with this fix in our Android app, verified that the SIGABRT no longer occurs in builds where minification is enabled