Skip to content

ADFA-4647 | Let R8 shrink StreamEx instead of keeping it whole - #2157

Merged
davidschachterADFA merged 6 commits into
stagefrom
ADFA-4647-shrink-streamex
Oct 29, 2026
Merged

davidschachterADFA merged 6 commits into
stagefrom
ADFA-4647-shrink-streamex

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

ADFA-4647

Description

Drops -keep class one.util.streamex.** { *; } from app/proguard-rules.pro so R8 keeps only the StreamEx code that is actually used. APK shrinks by 379,616 bytes.

Commits

  1. 7564abdc6 - the StreamEx change itself.
  2. 2a2af3e97 - comment-only fixes from code review: the lsp/kotlin keep block no longer claims everything is kept whole; the StreamEx note names the real (relocated) callers and the rule that keeps them, and says to re-check on a jar bump; and a note on why the kotlin.reflect / kotlin.script / kotlinx.coroutines.internal keeps stay even though kotlin.** subsumes them today (ADFA-5164 plans to narrow kotlin.**).
  3. 0b125ca09 - subprojects/kotlin-analysis-api/consumer-rules.pro: all 199 -keep class com.intellij.* rules matched no class, because the jar relocates IntelliJ to org.jetbrains.kotlin.com.intellij while its XML descriptors keep the original names. Prefixed them and added the step to the generation procedure. 335 of 352 rules now name a class in the jar (was 141). The bump procedure now also says to re-check the StreamEx note.

Details

  • Where StreamEx comes from. No CoGo code uses it. StreamEx 0.7.2 is shaded into the Kotlin Analysis API jar (kt-android.jar). Its only callers are 10 IntelliJ Java PSI classes in that jar (PsiSubstitutorImpl, PsiImplUtil, InferenceSession, ...), used for Kotlin-Java interop.
  • Why it's safe to shrink. Those 10 callers are already kept whole by -keep class org.jetbrains.kotlin.** { *; }, so R8 traces every StreamEx method they call. StreamEx's only reflection is Array.newInstance in toArray, which needs no keep rule. The blanket keep was added preemptively in ADFA-3604 (URGENT ADFA-3604: Fix R8 shrink/optimize crashes live on stage #1609), not for an observed StreamEx crash.
  • Rejected: removing StreamEx entirely. That means patching 10 upstream IntelliJ files in the kotlin-android fork on every Kotlin bump. Not worth it for the ~69 KB of dex the shrunk StreamEx still takes (standalone R8 estimate).

Measurements

:app:assembleV8Profileable (R8 shrinking as in release; signed with the debug key so it installs), built with and without the keep rule:

Before After Delta
APK 182,227,224 B 181,847,608 B -379,616 B
Total dex 99,181,552 B 98,801,984 B -379,568 B
StreamEx classes 776 124 -652
StreamEx methods 4,910 895 -4,015

All 19 StreamEx methods referenced by the 10 callers are present in the shrunk dex (9 declared on the receiver class, 10 resolved via AbstractStreamEx), checked with dexdump against javap of the callers.

Testing

  • Built and inspected the shrunk APK as above.
  • On-device (Pixel 6 Pro, Android 17), with the shrunk profileable APK installed over a debug build, ran the ticket's Steps to QA in a temporary Kotlin file in an existing project:
    • Java generics: the only diagnostic was Initializer type mismatch: expected 'Int', actual 'String'. on val wrong: Int = list.get(0); ArrayList, Collections.sort, HashMap.computeIfAbsent { } resolved cleanly.
    • Completion on list. listed add(e: E), addAll(...), get(index: Int): E.
    • Completion on savedInstanceState?.getStr listed Bundle.getString(key: String?): String? and getString(key: String?, defaultValue: String!).
    • Logcat over the whole session: no mention of one.util.streamex, no NoSuchMethodError / NoClassDefFoundError / ClassNotFoundException, no crash or ANR.
    • Java records (asked for in review; LightRecordComponentModifierList is a StreamEx caller): with a Java record QaRecord(int count, String name) in the project, Kotlin resolved r.count() and r.name().length, completion on r. listed count()/count and name()/name, and val wrong: Int = r.name() gave Initializer type mismatch: expected 'Int', actual 'String!'. Logcat: no StreamEx mention, linkage error, crash or ANR. (A Java file added from outside the IDE is only picked up after the Kotlin analysis session restarts; that is existing behavior, not this change.)
  • Commits 2-3 are expected to leave the dex unchanged, since org.jetbrains.kotlin.** already keeps every class the relocated rules name. Rebuilt the profileable APK at 0b125ca09 and compared it with the 7564abdc6 build: same APK size, same 86,861 classes, identical string tables, and identical bytecode in every method except AppEventsIndex.<clinit>. That class is the EventBus index generated by kapt; it reran because consumer-rules.pro ships inside the library, and its processor emitted the same 59 entries in a different order.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GSHUiR3DcJQeGTE2G8kmch

StreamEx is shaded into the Kotlin Analysis API jar; its only callers
are 10 IntelliJ Java PSI classes there, already kept whole by the
org.jetbrains.kotlin.** rule. Its only reflection is Array.newInstance,
so R8 can trace what those callers use. The blanket keep was added
preemptively in ADFA-3604, not for an observed crash.

Measured on :app:assembleV8Profileable: StreamEx drops from 776 classes
/ 4,910 methods to 124 / 895, and the APK shrinks by 379,616 bytes. All
19 StreamEx methods the callers reference remain in the dex.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GSHUiR3DcJQeGTE2G8kmch

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: a8c09b55-d0b7-4984-bd59-dedcdcd52003
Commits

Reviewing files that changed from the base of the PR and between b1ee69f and d9dd367.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d1b9900b-6f6a-4e6e-8783-22bfb50211b9
Commits

Reviewing files that changed from the base of the PR and between 7564abd and 8162995.

Files selected for processing: 2
  • app/proguard-rules.pro
  • subprojects/kotlin-analysis-api/consumer-rules.pro

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Summary
  • Removed the blanket StreamEx keep rule so R8 can remove unused StreamEx code. The gnu.trove keep rule remains.
  • The PR reports that :app:assembleV8Profileable produced an APK 379,616 bytes smaller, with 652 fewer StreamEx classes and 4,015 fewer StreamEx methods. It also reports that all 19 referenced StreamEx methods remained.
  • Updated IntelliJ keep rules to use the relocated org.jetbrains.kotlin.com.intellij package names. The rules preserve classes referenced by plugin descriptors and ServiceLoader entries.
  • Risk: R8 may remove StreamEx code accessed through reflection or other patterns it cannot trace. The change depends on StreamEx callers remaining under org.jetbrains.kotlin.** and not using name-based reflection. The PR reports successful on-device checks for Kotlin-Java interop, completion, and Java records.

The app ProGuard rules no longer keep the entire one.util.streamex package. The Kotlin Analysis API consumer rules use relocated IntelliJ package names. Comments document broader keep rules and regeneration notes.

Changes: R8 keep rule updates

Layer / File(s) Summary
Update app keep rules
app/proguard-rules.pro
The whole-package one.util.streamex keep rule was removed. Comments describe R8 tracing StreamEx callers and identify Kotlin and coroutine keeps covered by broader rules.
Update Analysis API consumer rules
subprojects/kotlin-analysis-api/consumer-rules.pro
The listed IntelliJ keep rules now use org.jetbrains.kotlin.com.intellij package names. Regeneration notes document the relocation and include a reminder about the StreamEx note.

Priority: Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Suggested reviewers: daniel-adfa

Merge Risk: Minimal · previous review 81629

The relocated rules match the packaged Analysis API classes. No identified issue blocks merging after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: allowing R8 to shrink StreamEx instead of keeping the entire package.
Description check Passed The description directly explains the StreamEx keep-rule removal, the resulting size reduction, supporting measurements, testing, and related rule updates.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 0 files. (2 skipped: 2 unsupported.)

Finishing Touches
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks the rules in place
And hops past StreamEx’s package trace
Relocated names now match the keep
While Kotlin’s notes record the leap
The rabbit curls up, calm and neat.

Comment @coderabbitai help to get the list of available commands.

davidschachterADFA and others added 2 commits October 16, 2026 20:25
Comment-only; no rule changes.

- The lsp/kotlin block said every runtime dependency is kept whole;
  StreamEx is now the exception.
- The StreamEx note said its callers are "Java PSI classes kept above".
  They are relocated under org.jetbrains.kotlin.com.intellij, one is
  codeInsight, and the rule keeping them is org.jetbrains.kotlin.**.
  The note now also says to re-check this on a jar bump.
- kotlin.reflect, kotlin.script and kotlinx.coroutines.internal are
  subsumed by the kotlin.** / kotlinx.coroutines.** keeps added for
  plugins (ADFA-5156), but ADFA-5164 plans to narrow those. Say why the
  three LSP keeps stay, so they aren't removed as redundant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GSHUiR3DcJQeGTE2G8kmch
The Kotlin Analysis API jar relocates IntelliJ from com.intellij to
org.jetbrains.kotlin.com.intellij, but its XML descriptors keep the
original names, and the generated list copied those. All 199
"-keep class com.intellij.*" rules matched no class: the jar has 0
classes under com/intellij and 6,102 under
org/jetbrains/kotlin/com/intellij. Only the org.jetbrains.kotlin.**
blanket keep in app/proguard-rules.pro protected these classes.

Prefix the 199 entries with "org.jetbrains.kotlin." and add the step
to the generation procedure. 335 of the 352 rules now name a class in
the jar (was 141). The other 17 name classes the descriptors mention
but the jar doesn't contain (5 IntelliJ, 9 org.jetbrains.*, and 3
jaxp.xml.stream ServiceLoader interfaces that live in another module);
they are left as generated.

No dex change expected while the blanket keep stays: it already keeps
every one of these classes.

Also: the version-bump steps now say to re-check the StreamEx note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GSHUiR3DcJQeGTE2G8kmch

@itsaky-adfa itsaky-adfa 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.

Verified against kt-android.jar (pinned sha256): StreamEx's only callers are the 10 relocated IntelliJ classes kept by org.jetbrains.kotlin.**, and no reflection, native, descriptor, serialization or plugin path reaches StreamEx. The relocated consumer-rules.pro entries only add matches to classes already kept.

@davidschachterADFA
davidschachterADFA merged commit 8302d5a into stage Oct 29, 2026
6 checks passed
@davidschachterADFA
davidschachterADFA deleted the ADFA-4647-shrink-streamex branch October 29, 2026 15:26
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.

2 participants