Skip to content

fix(android): narrow the replay and survey keep rules to the Gson models - #1032

Open
ioannisj wants to merge 2 commits into
mainfrom
fix/1031-narrow-consumer-keep-rules
Open

ioannisj wants to merge 2 commits into
mainfrom
fix/1031-narrow-consumer-keep-rules

Conversation

@ioannisj

@ioannisj ioannisj commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Fixes #1031

The Android AAR kept every class in com.posthog.surveys and com.posthog.internal.replay with all its members, so R8 couldn't shrink or rename any of them in apps that use the SDK.

Changes:

  • Replaces the 4 package-wide rules with per-class rules for the 22 classes that Gson reads or writes by field name (7 replay, 15 survey).
  • Ships those rules in the com.posthog:posthog jar at META-INF/proguard/posthog.pro, next to the classes they keep. R8 reads rules from the jar, so the AAR and a plain jar dependency both get them. The rules only keep classes, so they can't change behavior on the JVM; the posthog-server tests pass.
  • Leaves the SurveyProgress and StoredSurveyResponse rules as they are.
  • Adds PostHogKeepRulesTest. It walks the models with the SDK's own Gson instance and fails when posthog.pro misses a class that Gson handles by reflection, or names one that it doesn't.
  • Adds a .gitignore exception, since the existing proguard/ pattern ignored the new file.

On the reflection point in the issue, I checked all reflection in the SDK, not only Gson. Only Gson reaches these two packages. The other reflection (Class.forName, getMethod, Proxy) targets Compose, Firebase and the Compose survey delegate, which have their own rules that this PR doesn't touch. The enums in both packages are now renamed. That is safe because each one has a registered adapter that writes its value, and the new test fails if an enum ever falls back to Gson's name-based adapter. Replacing Gson is worth its own issue if we want it.

💚 How did you test it?

  • Minified release build of the sample app on a Pixel 8 emulator, pointed at a local mock server:
    • Surveys: a rating question and an open question showed up and I answered both. The survey sent event carried both responses.
    • Replay: meta, full snapshot (with wireframes), incremental snapshot and custom events all arrived, with the same event mix as a build with the old rules.
    • A throwaway build of the same app also serialized mutation adds, removes and updates plus mouse positions, and deserialized link, single choice and multiple choice questions with every nested model. All field names came through intact.
  • mapping.txt: the 22 classes keep their names and fields. The rest of both packages is renamed (trigger group models, enums, PostHogDisplay* classes).
  • An app with packaging.resources.excludes += "META-INF/**" still applies the rules.
  • ./gradlew :posthog:test :posthog-android:testReleaseUnitTest :posthog-server:test spotlessCheck apiCheck passes.
  • Removing the RRMousePosition rule makes PostHogKeepRulesTest fail.
  • Google's Android CLI (android analyze --analysis=optimization, version 1.1.16919734, R8 9.5.17-dev), main vs this branch:
    • posthog-android AAR: on main it warns "Contains keep rules which do not target library code" and lists the 4 package-wide rules as unused, since the classes live in the core jar. That warning is gone here. The AAR now has only the SurveyProgress and StoredSurveyResponse rules.
    • posthog jar: no rules on main. Here it keeps 22 of 364 classes, 41 of 3157 methods and 131 of 1222 fields (keep ratio 0.03).
    • Sample app release APK: dex size goes from 2,844,256 to 2,783,524 bytes (60,732 bytes smaller), and the tool's obfuscation estimate goes from 94.66% to 96.22%.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

DRI: @ioannisj
Autonomy: Human-driven (agent-assisted)

@ioannisj ioannisj self-assigned this Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

posthog-android Compliance Report

Date: 2026-12-03T08:54:40.444208+00:00
Duration: 511865ms

✅ All Tests Passed!

243/243 tests passed


Capture_V1 Tests

✅ 111/111 tests passed

View Details
Test Status Duration
Redirects And Library Identity.Follows Same Origin 307 ✅ 2415ms
Redirects And Library Identity.Does Not Follow Cross Origin Redirect ✅ 4030ms
Redirects And Library Identity.Treats 302 As Terminal ✅ 4029ms
Redirects And Library Identity.Follows At Most Five Redirects ✅ 5030ms
Redirects And Library Identity.Sends Lib And Lib Version Per Event ✅ 24ms
Request Size.Batches Close Before Byte Target ✅ 5296ms
Endpoint And Method.Targets V1 Endpoint ✅ 33ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 22ms
Required Headers.Has Authorization Bearer Header ✅ 22ms
Required Headers.Has Content Type Json ✅ 24ms
Required Headers.Has Posthog Sdk Info Format ✅ 21ms
Required Headers.Has Posthog Attempt Header ✅ 20ms
Required Headers.Has Posthog Request Id ✅ 24ms
Required Headers.Has Posthog Request Timestamp ✅ 24ms
Required Headers.Has User Agent ✅ 21ms
Body Format.Body Has Created At And Batch ✅ 21ms
Body Format.No Api Key In Body ✅ 21ms
Body Format.No Sent At In Body ✅ 22ms
Event Format.Event Has Required Root Fields ✅ 22ms
Event Format.Event Uuid Is Valid ✅ 21ms
Event Format.Event Timestamp Is Rfc3339 ✅ 24ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 22ms
Event Format.Distinct Id Is String ✅ 17ms
Event Format.Distinct Id At Root Not Properties ✅ 22ms
Event Format.Custom Properties Preserved ✅ 24ms
Event Format.Set Properties Preserved ✅ 18ms
Event Format.Set Once Properties Preserved ✅ 21ms
Event Format.Groups Properties Preserved ✅ 30ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 23ms
Event Format.Event Has Required Root Fields Batch ✅ 40ms
Event Format.Event Uuid Is Valid Batch ✅ 24ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 26ms
Event Format.Distinct Id Is String Batch ✅ 30ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 27ms
Event Format.Custom Properties Preserved Batch ✅ 30ms
Event Format.Set Properties Preserved Batch ✅ 26ms
Event Format.Set Once Properties Preserved Batch ✅ 30ms
Event Format.Groups Properties Preserved Batch ✅ 27ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 27ms
Batch Behavior.Multiple Events In Single Batch ✅ 35ms
Batch Behavior.Batch Envelope Smoke ✅ 28ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 10ms
Batch Behavior.Flush At Triggers Batch ✅ 1018ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 17ms
Deduplication.Generates Unique Uuids ✅ 32ms
Deduplication.Different Events Same Content Different Uuids ✅ 22ms
Deduplication.Preserves Uuid On Retry ✅ 7014ms
Deduplication.Preserves Timestamp On Retry ✅ 7016ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 7018ms
Deduplication.No Duplicate Events In Batch ✅ 29ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 17ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 12024ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 7016ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 2029ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 7017ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 15ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 20ms
Response Format Validation.Success No Retry After When All Ok ✅ 23ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 2010ms
Response Format Validation.Success No Retry After When Drop Only ✅ 17ms
Response Format Validation.Response Echoes Request Id ✅ 16ms
Retry Behavior.Retries On 408 ✅ 7013ms
Retry Behavior.Retries On 500 ✅ 7016ms
Retry Behavior.Retries On 503 ✅ 7021ms
Retry Behavior.Retries On 504 ✅ 7013ms
Retry Behavior.Retries On 404 ✅ 7016ms
Retry Behavior.Retries On 429 ✅ 6016ms
Retry Behavior.Retries On 502 ✅ 7015ms
Retry Behavior.Retryable Errors Have Retry After ✅ 4012ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 10025ms
Retry Behavior.Does Not Retry On 400 ✅ 4017ms
Retry Behavior.Does Not Retry On 401 ✅ 4017ms
Retry Behavior.Does Not Retry On 402 ✅ 4017ms
Retry Behavior.Does Not Retry On 413 ✅ 4019ms
Retry Behavior.Does Not Retry On 415 ✅ 4020ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 4018ms
Retry Behavior.Implements Backoff ✅ 14020ms
Partial Batch Handling.Handles 200 Full Success ✅ 2018ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3024ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3024ms
Partial Batch Handling.Does Not Retry Warning Events ✅ 3023ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 7016ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 7016ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 7016ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 7016ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 7017ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 7016ms
Partial Batch Handling.Respects Retry After On Partial ✅ 7022ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3021ms
Partial Batch Handling.Mixed Ok Drop Warning No Retry ✅ 3025ms
Compression.Sends Gzip Content Encoding ✅ 14ms
Compression.No Content Encoding When Disabled ✅ 13ms
Compression.Compressed Body Is Decompressible ✅ 17ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 4017ms
Event Options.Cookieless Mode Override ✅ 14ms
Event Options.Disable Skew Correction Override ✅ 13ms
Event Options.Process Person Profile Override ✅ 16ms
Event Options.Product Tour Id Override ✅ 13ms
Event Options.Unset Options Omitted ✅ 16ms
Event Options.Options Override In Batch ✅ 20ms
Event Options.Unknown Option Passes Through ✅ 17ms
Event Options.Option Value Is Not Converted ✅ 18ms
Event Options.Lenient Bool Option Values Are Not Converted ✅ 13ms
Event Options.Option Wins Over Legacy Property ✅ 14ms
Event Options.Legacy Property Fills Unset Option ✅ 15ms
Event Options.Null Option Falls Back To Legacy Property ✅ 15ms
Property Layers.Caller Property Beats Registered Property ✅ 15ms
Property Layers.Before Send Runs Last ✅ 15ms
Fail Open.Empty Api Key Sends Nothing ✅ 3012ms
Fail Open.Personal Api Key Sends Nothing ✅ 3011ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 16ms

Capture_Ai_V1 Tests

✅ 115/115 tests passed

View Details
Test Status Duration
Redirects And Library Identity.Follows Same Origin 307 ✅ 2025ms
Redirects And Library Identity.Does Not Follow Cross Origin Redirect ✅ 4017ms
Redirects And Library Identity.Treats 302 As Terminal ✅ 4015ms
Redirects And Library Identity.Follows At Most Five Redirects ✅ 5017ms
Redirects And Library Identity.Sends Lib And Lib Version Per Event ✅ 15ms
Endpoint And Method.Targets V1 Endpoint ✅ 13ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 13ms
Endpoint And Method.Capture Does Not Reroute Ai Named Events ✅ 13ms
Endpoint And Method.Capture Ai Does Not Reroute Non Ai Events ✅ 14ms
Lane Isolation.Failing Ai Lane Does Not Delay Analytics ✅ 7019ms
Required Headers.Has Authorization Bearer Header ✅ 15ms
Required Headers.Has Content Type Json ✅ 14ms
Required Headers.Has Posthog Sdk Info Format ✅ 14ms
Required Headers.Has Posthog Attempt Header ✅ 15ms
Required Headers.Has Posthog Request Id ✅ 12ms
Required Headers.Has Posthog Request Timestamp ✅ 13ms
Required Headers.Has User Agent ✅ 15ms
Body Format.Body Has Created At And Batch ✅ 17ms
Body Format.No Api Key In Body ✅ 19ms
Body Format.No Sent At In Body ✅ 16ms
Event Format.Event Has Required Root Fields ✅ 14ms
Event Format.Event Uuid Is Valid ✅ 14ms
Event Format.Event Timestamp Is Rfc3339 ✅ 15ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 14ms
Event Format.Distinct Id Is String ✅ 14ms
Event Format.Distinct Id At Root Not Properties ✅ 16ms
Event Format.Custom Properties Preserved ✅ 16ms
Event Format.Set Properties Preserved ✅ 15ms
Event Format.Set Once Properties Preserved ✅ 14ms
Event Format.Groups Properties Preserved ✅ 15ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 14ms
Event Format.Event Has Required Root Fields Batch ✅ 19ms
Event Format.Event Uuid Is Valid Batch ✅ 35ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 21ms
Event Format.Distinct Id Is String Batch ✅ 20ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 19ms
Event Format.Custom Properties Preserved Batch ✅ 20ms
Event Format.Set Properties Preserved Batch ✅ 22ms
Event Format.Set Once Properties Preserved Batch ✅ 22ms
Event Format.Groups Properties Preserved Batch ✅ 20ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 17ms
Batch Behavior.Multiple Events In Single Batch ✅ 23ms
Batch Behavior.Batch Envelope Smoke ✅ 24ms
Batch Behavior.Flush At Triggers Batch ✅ 1016ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 14ms
Deduplication.Generates Unique Uuids ✅ 31ms
Deduplication.Different Events Same Content Different Uuids ✅ 18ms
Deduplication.Preserves Uuid On Retry ✅ 7013ms
Deduplication.Preserves Timestamp On Retry ✅ 7015ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 7014ms
Deduplication.No Duplicate Events In Batch ✅ 24ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 13ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 12022ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 7015ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 2027ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 7015ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 14ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 17ms
Response Format Validation.Success No Retry After When All Ok ✅ 19ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 2009ms
Response Format Validation.Success No Retry After When Drop Only ✅ 15ms
Response Format Validation.Response Echoes Request Id ✅ 14ms
Retry Behavior.Retries On 408 ✅ 7014ms
Retry Behavior.Retries On 500 ✅ 7014ms
Retry Behavior.Retries On 503 ✅ 7019ms
Retry Behavior.Retries On 504 ✅ 7015ms
Retry Behavior.Retries On 404 ✅ 7015ms
Retry Behavior.Retries On 429 ✅ 7015ms
Retry Behavior.Retries On 502 ✅ 6015ms
Retry Behavior.Retryable Errors Have Retry After ✅ 4013ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 10025ms
Retry Behavior.Does Not Retry On 400 ✅ 4017ms
Retry Behavior.Does Not Retry On 401 ✅ 4017ms
Retry Behavior.Does Not Retry On 402 ✅ 4017ms
Retry Behavior.Does Not Retry On 413 ✅ 4019ms
Retry Behavior.Does Not Retry On 415 ✅ 4014ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 4017ms
Retry Behavior.Implements Backoff ✅ 14026ms
Partial Batch Handling.Handles 200 Full Success ✅ 2016ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3023ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3021ms
Partial Batch Handling.Does Not Retry Warning Events ✅ 3022ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 7015ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 7015ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 7015ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 7015ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 7015ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 7015ms
Partial Batch Handling.Respects Retry After On Partial ✅ 7020ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3022ms
Partial Batch Handling.Mixed Ok Drop Warning No Retry ✅ 3024ms
Compression.Sends Gzip Content Encoding ✅ 14ms
Compression.No Content Encoding When Disabled ✅ 13ms
Compression.Compressed Body Is Decompressible ✅ 12ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 4013ms
Event Options.Cookieless Mode Override ✅ 13ms
Event Options.Disable Skew Correction Override ✅ 13ms
Event Options.Process Person Profile Override ✅ 14ms
Event Options.Product Tour Id Override ✅ 13ms
Event Options.Unset Options Omitted ✅ 12ms
Event Options.Options Override In Batch ✅ 17ms
Event Options.Unknown Option Passes Through ✅ 14ms
Event Options.Option Value Is Not Converted ✅ 14ms
Event Options.Lenient Bool Option Values Are Not Converted ✅ 13ms
Event Options.Option Wins Over Legacy Property ✅ 15ms
Event Options.Legacy Property Fills Unset Option ✅ 19ms
Event Options.Null Option Falls Back To Legacy Property ✅ 18ms
Event Size.Event Over Ceiling Is Not Sent ✅ 3244ms
Event Size.Over Ceiling Drop Is Reported Or Logged Without Content ✅ 2048ms
Event Size.Batches Close Before Byte Target ✅ 6068ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 16ms
Property Layers.Caller Property Beats Registered Property ✅ 14ms
Property Layers.Before Send Runs Last ✅ 14ms
Fail Open.Empty Api Key Sends Nothing ✅ 1009ms
Fail Open.Personal Api Key Sends Nothing ✅ 1009ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 27ms
Request Payload.Flags Request Uses V2 Query Param ✅ 16ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 16ms
Request Payload.Flags Request Omits Authorization Header ✅ 16ms
Request Payload.Token In Flags Body Matches Init ✅ 18ms
Request Payload.Groups Round Trip ✅ 16ms
Request Payload.Groups Default To Empty Object ✅ 13ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 15ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 13ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 15ms
Request Lifecycle.No Flags Request On Init Alone ✅ 5ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 15ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 21ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 17ms
Retry Behavior.Retries Flags On 502 ✅ 317ms
Retry Behavior.Retries Flags On 504 ✅ 318ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 20ms

@posthog

posthog Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

PostHog Review · Deep · reviewed 555861a

Nothing to post.

Step Status Result
Prepare the diff done 4 files, 1 chunk
Pick perspectives done 2 perspectives
Run review passes done 0 issues
Merge overlapping findings done 0 → 0
Validate findings done
Publish done Nothing to post

A happy dog on a sunny path

@ioannisj
ioannisj marked this pull request as ready for review December 3, 2026 09:59
@ioannisj
ioannisj requested a review from a team as a code owner December 3, 2026 09:59
@veria-ai

veria-ai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@posthog posthog Bot 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.

PostHog Review · Standard · reviewed 555861a: 1 Should fix.

New pushes to this pull request won't start an automatic review. Comment @posthog review to review new changes.

@marandaneto

Copy link
Copy Markdown
Member

we are just moving the rules from A to B, or am I missing something?

@@ -0,0 +1,25 @@
# Session replay: Gson serializes these by field name. RREvent subclasses only inherit its fields.

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.

minify works with differet tools, proguard, r8 and dexguard
do all those tools read from this file as well? do we need a min. version for each tool? i do know that at least jar has a min. AGP support, but we are probably past this min. version, worth checkin tho

@marandaneto
marandaneto requested a review from a team December 3, 2026 15:31

This branch has not been deployed

No deployments
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.

Narrow the package-wide consumer keep rules for replay and surveys

2 participants