Repository navigation
fix(android): keep span constructors that are looked up by reflection - #840
Open
Junveloper wants to merge 1 commit into
Open
Junveloper wants to merge 1 commit into
Junveloper wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The narrow rule matches the reflective constructor signature and is correctly exported, with no blocking issues identified.
No open issues 🎉
What changed in this PR
Fixes #839 by preserving Android span constructors used through reflection, preventing NoSuchMethodException crashes in R8-minified builds.
Changes:
- Adds a targeted consumer rule that preserves
(HtmlStyle)constructors without keeping otherwise unused span classes. - Exports the rule so consuming Android apps receive it automatically.
| File | Description |
|---|---|
android/consumer-rules.pro |
Preserves span constructors used by reflective style creation. |
android/build.gradle |
Registers the consumer rules for consuming apps. |
Review effort: Balanced
This branch has not been deployed
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.
Summary
Fixes #839.
InlineStylesandParagraphStylescreate spans withtype.getDeclaredConstructor(HtmlStyle::class.java). R8 can't see that reflective use, so in a minified release build it can inline a span's(HtmlStyle)constructor into its only direct caller (rebuildWithStyle) and remove it. Typing with that style active then crashes withNoSuchMethodException: <span>.<init> [class <HtmlStyle>]. In the example app this hits Bold, Italic, Underline and Strikethrough.This adds
android/consumer-rules.pro, which keeps the(HtmlStyle)constructor of every class incom.swmansion.enriched.textinput.spans, and exports it withconsumerProguardFiles. Apps that enable R8 pick it up automatically. The rule only keeps constructors, so span class names are still obfuscated, and a class R8 removes as unused is still removed.android/is already in the package'sfiles, so the new file is published.Test Plan
R8 isn't enabled in the example app, so turn it on for the test without committing it:
apps/example/android/app/build.gradle, setenableProguardInReleaseBuilds = true.main, build and install a release build:cd apps/example/android && ./gradlew :app:assembleRelease -PreactNativeArchitectures=arm64-v8a, thenadb install -r app/build/outputs/apk/release/app-release.apk.NoSuchMethodExceptioninInlineStyles.setSpan. The same happens for I, U and S.On this branch, R8's merged configuration (
app/build/outputs/mapping/release/configuration.txt) includes the rule from the library's exported consumer ProGuard file. The mapping keeps<init>(HtmlStyle)on all 14 span classes that have one, against 10 onmain.Checked on an Android 16 emulator (API 36, arm64). The same rule, added at app level, also fixes the crash in our production Expo app on 1.1.1.
Screenshots / Videos
On
mainwith R8 on, the app crashes at the first styled character. The trace is in #839. On this branch, the input shows the text typed with Bold, Italic, Underline and Strikethrough each active in turn, each styled correctly, and the app keeps running.Compatibility
iOS and web don't use R8 or this reflection.
Checklist
The Maestro suite installs the example app with
yarn example android, a debug build with R8 off, so it can't cover this. I didn't run it.