Skip to content

fix(android): Preserve custom Throwable identities - #5881

Open
sentry-junior[bot] wants to merge 2 commits into
mainfrom
fix/android-preserve-throwable-identities
Open

fix(android): Preserve custom Throwable identities#5881
sentry-junior[bot] wants to merge 2 commits into
mainfrom
fix/android-preserve-throwable-identities

Conversation

@sentry-junior

Copy link
Copy Markdown
Contributor

Prevent R8 horizontal class merging from collapsing unrelated custom Throwable subclasses into one runtime type. This keeps captured exception types aligned with retraced constructor frames and avoids incorrect issue titles or grouping.

Consumer ProGuard rule

sentry-android-core now exports a broad rule that preserves distinct throwable class identities while still allowing unused classes to be removed and retained classes to be obfuscated:

-keep,allowshrinking,allowobfuscation class * extends java.lang.Throwable

The rule follows the mitigation recommended by the R8 maintainers.

Requested by Roman Zavarnitsyn.

--

View Junior SessionSentry

sentry-juniorBotand others added 2 commits July 31, 2026 07:47
Co-Authored-By: Roman Zavarnitsyn <roman.zavarnitsyn@sentry.io>
@sentry

sentryBot commented Jul 31, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App NameApp IDVersionConfiguration
SDK Sizeio.sentry.tests.size8.51.0 (1)release

⚙️ sentry-android Build Distribution Settings

@romtsn
romtsn marked this pull request as ready for review July 31, 2026 07:58
@romtsnromtsn added the ship-it PR is ready to merge from a reviewer perspective label Jul 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Performance metrics 🚀

PlainWith SentryDiff
Startup time382.20 ms455.94 ms73.74 ms
Size0 B0 B0 B

Baseline results on branch: main

Startup times

RevisionPlainWith SentryDiff
6b019b7343.31 ms417.23 ms73.91 ms
d15471f342.08 ms415.44 ms73.35 ms
8687935332.52 ms362.23 ms29.71 ms
5b1a06b352.27 ms413.70 ms61.43 ms
91bb874314.47 ms440.00 ms125.53 ms
0ee65e9321.06 ms361.24 ms40.18 ms
e63ad34323.67 ms390.33 ms66.67 ms
33a08cc267.08 ms340.45 ms73.37 ms
27d7cf8397.90 ms498.65 ms100.75 ms
ee747ae405.43 ms485.70 ms80.28 ms

App size

RevisionPlainWith SentryDiff
6b019b70 B0 B0 B
d15471f1.58 MiB2.13 MiB559.54 KiB
86879351.58 MiB2.19 MiB619.17 KiB
5b1a06b0 B0 B0 B
91bb8741.58 MiB2.13 MiB559.07 KiB
0ee65e90 B0 B0 B
e63ad340 B0 B0 B
33a08cc1.58 MiB2.12 MiB555.28 KiB
27d7cf81.58 MiB2.12 MiB549.42 KiB
ee747ae1.58 MiB2.10 MiB530.95 KiB

# Preserve distinct runtime identities for custom Throwables. R8 horizontal class merging can
# otherwise merge unrelated exception classes, causing the runtime type and retraced frames to
# disagree. Unused Throwables may still be removed, and retained Throwables may still be obfuscated.
-keep,allowshrinking,allowobfuscation class * extends java.lang.Throwable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be just -keepnames (which should be short for Short for -keep,allowshrinking)?

Suggested change
-keep,allowshrinking,allowobfuscation class * extends java.lang.Throwable
-keepnames class * extends java.lang.Throwable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea here is to allow obfuscating the class as well, and keepnames would disable that

# Preserve distinct runtime identities for custom Throwables. R8 horizontal class merging can
# otherwise merge unrelated exception classes, causing the runtime type and retraced frames to
# disagree. Unused Throwables may still be removed, and retained Throwables may still be obfuscated.
-keep,allowshrinking,allowobfuscation class * extends java.lang.Throwable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely need to check that R8 doesn't flag us here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it likely does, but there's no other way to work this around, so 🤷 every crash reporter is prone to this issue. But I will check with the R8 config analyzer

@runningcoderunningcode left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice find!

# https://developer.android.com/studio/build/shrink-code#decode-stack-trace
-keepattributes LineNumberTable,SourceFile

# Preserve distinct runtime identities for custom Throwables. R8 horizontal class merging can

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You said in the PR description that this was recommended by the R8 maintainers, do you have a link to that so that we can reference it in this comment?

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ship-itPR is ready to merge from a reviewer perspective

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@runningcode@markushi@romtsn