Skip to content

[CDX-494] Keep Moshi core runtime classes to fix R8 full-mode JSON crash - #171

Merged
HHHindawy merged 1 commit into
masterfrom
cdx-494-android-sdk-events-not-coming-due-to-constructorio-init
Jul 22, 2026
Merged

[CDX-494] Keep Moshi core runtime classes to fix R8 full-mode JSON crash#171
HHHindawy merged 1 commit into
masterfrom
cdx-494-android-sdk-events-not-coming-due-to-constructorio-init

Conversation

@HHHindawy

Copy link
Copy Markdown
Contributor

Summary

  • Add -keep rules for com.squareup.moshi.** core runtime classes/interfaces to library/consumer-rules.pro and library/proguard-rules.pro.
  • Add -keepattributes *Annotation*, Signature, EnclosingMethod, InnerClasses so Moshi's reflective adapter discovery keeps working under obfuscation.
  • Add -dontwarn com.squareup.moshi.**.

Why?

R8 (especially full mode) can rename or horizontally merge JsonReader/JsonAdapter. This breaks the identity check AdapterMethodsFactory runs on @FromJson/@ToJson parameter types, crashing release builds with Unexpected signature ... fromJson(ya.m, ya.h, ya.h). The prior rules only kept Moshi annotations and annotated members, not the runtime classes themselves.

  • consumer-rules.pro — ships inside the AAR and applies to the consuming app's R8 build. This protects Moshi when someone else's app uses our library.
  • proguard-rules.pro — applies when this library itself is built/minified (and in the library's own tests/benchmarks). This protects Moshi when our own code is run through R8.

@HHHindawy
HHHindawy requested review from a team and Mudaafi July 22, 2026 08:39
@HHHindawy
HHHindawy requested a review from a team as a code owner July 22, 2026 08:39
Copilot AI review requested due to automatic review settings July 22, 2026 08:39

@constructor-claude-bedrock constructor-claude-bedrock 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.

Code Review

This PR adds ProGuard/R8 keep rules for Moshi's core runtime classes to fix a crash in R8 full-mode release builds. The fix is correct in principle, but there are a few issues in proguard-rules.pro worth addressing — a pre-existing duplicate -dontwarn okio.** and new -keepattributes rules that partially overlap with already-present ones.

Inline comments: 3 discussions added

Overall Assessment: ⚠️ Needs Work

@@ -11,6 +11,15 @@
-dontwarn okio.**

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Important Issue: There are now two -dontwarn okio.** lines in this file (lines 8 and 11). The second one was already present before this PR and the new block was inserted just after it, but the original line 11 is a leftover duplicate that was never cleaned up. While ProGuard/R8 silently deduplicates these, it is confusing and makes the file harder to maintain. Please remove the duplicate so each directive appears exactly once.

Comment thread library/proguard-rules.pro
# horizontally merge JsonReader/JsonAdapter, which breaks the identity check
# Moshi's AdapterMethodsFactory performs on @FromJson/@ToJson parameter types
# (crash: "Unexpected signature ... fromJson(ya.m, ya.h, ya.h)").
-keep class com.squareup.moshi.** { *; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: -keep class com.squareup.moshi.** { *; } already implies all interfaces inside that package, so the separate -keep interface com.squareup.moshi.** { *; } line is technically redundant (same pattern applies in proguard-rules.pro). This is a very minor style point — the duplicate rule causes no functional harm — but removing it keeps the rules files concise and avoids giving future readers the false impression that interfaces require special treatment beyond what the class wildcard already covers.

Copilot AI 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.

Pull request overview

Adds ProGuard/R8 configuration to ensure Moshi reflective adapter discovery and @FromJson/@ToJson adapter method signature handling remain stable under R8 full-mode (avoiding crashes caused by renaming/optimization such as horizontal class merging).

Changes:

  • Adds -keepattributes to preserve annotation + signature (and related) metadata used by Moshi reflection.
  • Adds -keep rules for Moshi runtime types to prevent R8 optimizations that can break reflective adapter method signature expectations.
  • Adds -dontwarn com.squareup.moshi.** to suppress Moshi-related shrinker warnings.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
library/proguard-rules.pro Adds Moshi keep/attribute rules for when the library itself is minified.
library/consumer-rules.pro Adds Moshi keep/attribute rules shipped in the AAR to protect consuming apps under R8.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread library/proguard-rules.pro
Comment on lines +5 to +6
# Keep annotation/generic-signature metadata that Moshi reads reflectively at runtime.
-keepattributes *Annotation*, Signature, EnclosingMethod, InnerClasses
Comment on lines +12 to +13
-keep class com.squareup.moshi.** { *; }
-keep interface com.squareup.moshi.** { *; }
Comment on lines +19 to +20
-keep class com.squareup.moshi.** { *; }
-keep interface com.squareup.moshi.** { *; }
@HHHindawy
HHHindawy merged commit 9a8316d into master Jul 22, 2026
2 checks passed
@HHHindawy
HHHindawy deleted the cdx-494-android-sdk-events-not-coming-due-to-constructorio-init branch July 22, 2026 15:52
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.

3 participants