Skip to content

Commit b2f01d0

Browse files
runningcodeclaude
andauthored
ref(android): Narrow consumer keep rules to what the SDK reads (#6779)
* ref(android): Narrow consumer keep rules to what the SDK reads Classes that are only checked by name now keep just their name, the NDK entry points keep only init() and close(), replay keeps the names of the three Painters isMaskable() leaves unmasked, LayoutNode keeps only the reflected members, and the Compose tag elements keep only their tag field. The app-wide Integration keepnames is dropped, since integration names are string literals now. Refs #6771 Refs JAVA-856 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * changelog * fix(replay): Unmask Compose painters by type, not by class name isMaskable() left any Painter unmasked whose class name contained Vector, Color, or Brush. Once R8 renames an app's painter, that check gave different results in debug and release. Checking the three AndroidX types directly behaves the same in both, and drops their keep rules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 589a09e commit b2f01d0

10 files changed

Lines changed: 87 additions & 27 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
### Improvements
1111

1212
- Android: Remove consumer keep rules that had no effect, so R8 can optimize more of your app ([#6778](https://github.com/getsentry/sentry-java/pull/6778))
13+
- Android: Narrow consumer keep rules to the classes and members the SDK reads by reflection, so R8 can optimize more of your app ([#6779](https://github.com/getsentry/sentry-java/pull/6779))
1314

1415
### Fixes
1516

17+
- Session Replay: Mask Compose images the same way in debug and release builds. Only `VectorPainter`, `ColorPainter`, and `BrushPainter` are left unmasked, instead of any Painter whose class name contains `Vector`, `Color`, or `Brush` ([#6779](https://github.com/getsentry/sentry-java/pull/6779))
1618
- Android: A crash long after `Sentry.init` is no longer marked as a startup crash when the device slept in between, because the time since init now counts deep sleep ([#6738](https://github.com/getsentry/sentry-java/pull/6738))
1719
- Session Replay: Replays recovered after a crash no longer list `"null"` as a screen name when the last named frame leaves the buffer ([#6827](https://github.com/getsentry/sentry-java/pull/6827))
1820

‎sentry-android-core/proguard-rules.pro‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
##---------------Begin: proguard configuration for android-core ----------
22

33
##---------------Begin: proguard configuration for androidx.core ----------
4-
-keep class androidx.core.app.FrameMetricsAggregator { <init>(...); }
5-
-keep interface androidx.core.view.ScrollingView { *; }
4+
-keepnames class androidx.core.app.FrameMetricsAggregator
5+
-keepnames interface androidx.core.view.ScrollingView
66
##---------------End: proguard configuration for androidx.core ----------
77

88
##---------------Begin: proguard configuration for androidx.lifecycle ----------
9-
-keep interface androidx.lifecycle.DefaultLifecycleObserver { *; }
10-
-keep class androidx.lifecycle.ProcessLifecycleOwner { <init>(...); }
9+
-keepnames class androidx.lifecycle.ProcessLifecycleOwner
1110
##---------------End: proguard configuration for androidx.lifecycle ----------
1211

1312
# To mitigate the issue on R8 site (https://issuetracker.google.com/issues/235733922)
@@ -45,9 +44,6 @@
4544
# disagree. Unused Throwables may still be removed, and retained Throwables may still be obfuscated.
4645
-keep,allowshrinking,allowobfuscation class * extends java.lang.Throwable
4746

48-
# Keep Classnames for integrations
49-
-keepnames class * implements io.sentry.Integration
50-
5147
# SentryClient logs the processor's class name when it throws
5248
-keepnames class io.sentry.android.core.ScreenshotEventProcessor
5349
-keepnames class io.sentry.android.core.ViewHierarchyEventProcessor

‎sentry-android-fragment/proguard-rules.pro‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##---------------Begin: proguard configuration for Fragment ----------
22

33
# The Android SDK checks at runtime if these classes are available via Class.forName
4-
-keep class io.sentry.android.fragment.FragmentLifecycleIntegration { <init>(...); }
4+
-keepnames class io.sentry.android.fragment.FragmentLifecycleIntegration
55
-keepnames class androidx.fragment.app.FragmentManager$FragmentLifecycleCallbacks
66

77
# To ensure that stack traces is unambiguous
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
##---------------Begin: proguard configuration for NDK outproc ----------
22

33
# The Android SDK checks at runtime if this class is available via Class.forName
4-
-keep class io.sentry.android.ndk.outproc.SentryNdkOutproc { *; }
4+
-keep class io.sentry.android.ndk.outproc.SentryNdkOutproc {
5+
public static void init(...);
6+
public static void close();
7+
}
58

69
##---------------End: proguard configuration for NDK outproc ----------

‎sentry-android-ndk/proguard-rules.pro‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
##---------------Begin: proguard configuration for NDK ----------
22

33
# The Android SDK checks at runtime if this class is available via Class.forName
4-
-keep class io.sentry.android.ndk.SentryNdk { *; }
4+
-keep class io.sentry.android.ndk.SentryNdk {
5+
public static void init(...);
6+
public static void close();
7+
}
58

69
# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
710
-keepclasseswithmembernames,includedescriptorclasses class io.sentry.** {

‎sentry-android-replay/proguard-rules.pro‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
# Rules to detect Images/Icons and mask them
66
-dontwarn androidx.compose.ui.graphics.painter.Painter
7-
-keepnames class * extends androidx.compose.ui.graphics.painter.Painter
7+
-dontwarn androidx.compose.ui.graphics.painter.ColorPainter
8+
-dontwarn androidx.compose.ui.graphics.painter.BrushPainter
9+
-dontwarn androidx.compose.ui.graphics.vector.VectorPainter
810
-keepclasseswithmembernames class * {
911
androidx.compose.ui.graphics.painter.Painter painter;
1012
}
@@ -30,7 +32,7 @@
3032
-dontwarn androidx.camera.view.PreviewView
3133
-keepnames class androidx.camera.view.PreviewView
3234
# Rules to walk the Compose Node tree.
33-
-keep class androidx.compose.ui.node.LayoutNode {
35+
-keepclassmembers class androidx.compose.ui.node.LayoutNode {
3436
*** getChildren*(...);
3537
*** getOuterCoordinator*(...);
3638
*** getCollapsedSemantics*(...);

‎sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import androidx.compose.ui.geometry.Offset
2828
import androidx.compose.ui.geometry.Rect
2929
import androidx.compose.ui.graphics.Color
3030
import androidx.compose.ui.graphics.ColorProducer
31+
import androidx.compose.ui.graphics.painter.BrushPainter
32+
import androidx.compose.ui.graphics.painter.ColorPainter
3133
import androidx.compose.ui.graphics.painter.Painter
34+
import androidx.compose.ui.graphics.vector.VectorPainter
3235
import androidx.compose.ui.layout.LayoutCoordinates
3336
import androidx.compose.ui.layout.findRootCoordinates
3437
import androidx.compose.ui.node.LayoutNode
@@ -111,20 +114,14 @@ internal fun LayoutNode.isMaskableImage(): Boolean? {
111114
}
112115

113116
/**
114-
* We heuristically check the known classes that are coming from local assets usually:
115-
* [androidx.compose.ui.graphics.vector.VectorPainter]
116-
* [androidx.compose.ui.graphics.painter.ColorPainter]
117-
* [androidx.compose.ui.graphics.painter.BrushPainter]
117+
* Only [VectorPainter], [ColorPainter], and [BrushPainter] are left unmasked, because they draw
118+
* local vectors and fills. Every other Painter may draw a remote image, so it is masked.
118119
*
119120
* In theory, [androidx.compose.ui.graphics.painter.BitmapPainter] can also come from local assets,
120121
* but it can as well come from a network resource, so we preemptively mask it.
121122
*/
122-
internal fun Painter.isMaskable(): Boolean {
123-
val className = this::class.java.name
124-
return !className.contains("Vector") &&
125-
!className.contains("Color") &&
126-
!className.contains("Brush")
127-
}
123+
internal fun Painter.isMaskable(): Boolean =
124+
this !is VectorPainter && this !is ColorPainter && this !is BrushPainter
128125

129126
/**
130127
* This method is necessary to mask text in Compose.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
2+
3+
package io.sentry.android.replay.util
4+
5+
import android.graphics.Bitmap
6+
import androidx.compose.ui.geometry.Size
7+
import androidx.compose.ui.graphics.Color
8+
import androidx.compose.ui.graphics.SolidColor
9+
import androidx.compose.ui.graphics.asImageBitmap
10+
import androidx.compose.ui.graphics.drawscope.DrawScope
11+
import androidx.compose.ui.graphics.painter.BitmapPainter
12+
import androidx.compose.ui.graphics.painter.BrushPainter
13+
import androidx.compose.ui.graphics.painter.ColorPainter
14+
import androidx.compose.ui.graphics.painter.Painter
15+
import androidx.compose.ui.graphics.vector.VectorPainter
16+
import androidx.test.ext.junit.runners.AndroidJUnit4
17+
import com.google.common.truth.Truth.assertThat
18+
import kotlin.test.Test
19+
import org.junit.runner.RunWith
20+
import org.robolectric.annotation.Config
21+
22+
@RunWith(AndroidJUnit4::class)
23+
@Config(sdk = [35])
24+
class NodesTest {
25+
26+
@Test
27+
fun `VectorPainter is not maskable`() {
28+
assertThat(VectorPainter().isMaskable()).isFalse()
29+
}
30+
31+
@Test
32+
fun `ColorPainter is not maskable`() {
33+
assertThat(ColorPainter(Color.Red).isMaskable()).isFalse()
34+
}
35+
36+
@Test
37+
fun `BrushPainter is not maskable`() {
38+
assertThat(BrushPainter(SolidColor(Color.Red)).isMaskable()).isFalse()
39+
}
40+
41+
@Test
42+
fun `BitmapPainter is maskable`() {
43+
val bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888).asImageBitmap()
44+
assertThat(BitmapPainter(bitmap).isMaskable()).isTrue()
45+
}
46+
47+
@Test
48+
fun `a custom Painter is maskable even when its name contains Color, Vector, or Brush`() {
49+
assertThat(ColorVectorBrushPainter().isMaskable()).isTrue()
50+
}
51+
52+
private class ColorVectorBrushPainter : Painter() {
53+
override val intrinsicSize: Size = Size.Unspecified
54+
55+
override fun DrawScope.onDraw() = Unit
56+
}
57+
}

‎sentry-android-timber/proguard-rules.pro‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##---------------Begin: proguard configuration for Timber ----------
22

33
# The Android SDK checks at runtime if these classes are available via Class.forName
4-
-keep class io.sentry.android.timber.SentryTimberIntegration { <init>(...); }
4+
-keepnames class io.sentry.android.timber.SentryTimberIntegration
55
-keepnames class timber.log.Timber
66

77
# To ensure that stack traces is unambiguous

‎sentry-compose/proguard-rules.pro‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
##---------------Begin: proguard configuration for Compose ----------
22

33
# The Android SDK checks at runtime if these classes are available via Class.forName
4-
-keep class io.sentry.compose.gestures.ComposeGestureTargetLocator { <init>(...); }
5-
-keep class io.sentry.compose.viewhierarchy.ComposeViewHierarchyExporter { <init>(...); }
4+
-keepnames class io.sentry.compose.gestures.ComposeGestureTargetLocator
5+
-keepnames class io.sentry.compose.viewhierarchy.ComposeViewHierarchyExporter
66

77
-keepnames interface androidx.compose.ui.node.Owner
88

@@ -11,8 +11,8 @@
1111
-keepnames class androidx.compose.foundation.ScrollingLayoutElement
1212
-keepnames class androidx.compose.foundation.ScrollingContainerElement
1313
-keepnames class androidx.compose.foundation.ScrollableAreaElement
14-
-keepnames class androidx.compose.ui.platform.TestTagElement { *; }
15-
-keepnames class io.sentry.compose.SentryModifier$SentryTagModifierNodeElement { *; }
14+
-keepnames class androidx.compose.ui.platform.TestTagElement { java.lang.String tag; }
15+
-keepnames class io.sentry.compose.SentryModifier$SentryTagModifierNodeElement { java.lang.String tag; }
1616

1717
# R8 will warn about missing classes if people don't have androidx.compose-navigation on their
1818
# classpath, but this is fine, these classes are used in an internal class which is only used when

0 commit comments

Comments
 (0)