diff --git a/CHANGELOG.md b/CHANGELOG.md index 69ddbc590bf..f76059c48d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,13 @@ ## Unreleased +### Improvements + +- 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)) + ### Fixes +- 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)) - 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)) ### Dependencies diff --git a/sentry-android-core/proguard-rules.pro b/sentry-android-core/proguard-rules.pro index 34fc51f709a..ec12186cd19 100644 --- a/sentry-android-core/proguard-rules.pro +++ b/sentry-android-core/proguard-rules.pro @@ -1,13 +1,12 @@ ##---------------Begin: proguard configuration for android-core ---------- ##---------------Begin: proguard configuration for androidx.core ---------- --keep class androidx.core.app.FrameMetricsAggregator { (...); } --keep interface androidx.core.view.ScrollingView { *; } +-keepnames class androidx.core.app.FrameMetricsAggregator +-keepnames interface androidx.core.view.ScrollingView ##---------------End: proguard configuration for androidx.core ---------- ##---------------Begin: proguard configuration for androidx.lifecycle ---------- --keep interface androidx.lifecycle.DefaultLifecycleObserver { *; } --keep class androidx.lifecycle.ProcessLifecycleOwner { (...); } +-keepnames class androidx.lifecycle.ProcessLifecycleOwner ##---------------End: proguard configuration for androidx.lifecycle ---------- # To mitigate the issue on R8 site (https://issuetracker.google.com/issues/235733922) @@ -45,9 +44,6 @@ # disagree. Unused Throwables may still be removed, and retained Throwables may still be obfuscated. -keep,allowshrinking,allowobfuscation class * extends java.lang.Throwable -# Keep Classnames for integrations --keepnames class * implements io.sentry.Integration - -dontwarn io.sentry.apollo.SentryApolloInterceptor -keepnames class io.sentry.apollo.SentryApolloInterceptor diff --git a/sentry-android-fragment/proguard-rules.pro b/sentry-android-fragment/proguard-rules.pro index 71e58d6e8aa..b60107a5e23 100644 --- a/sentry-android-fragment/proguard-rules.pro +++ b/sentry-android-fragment/proguard-rules.pro @@ -1,7 +1,7 @@ ##---------------Begin: proguard configuration for Fragment ---------- # The Android SDK checks at runtime if these classes are available via Class.forName --keep class io.sentry.android.fragment.FragmentLifecycleIntegration { (...); } +-keepnames class io.sentry.android.fragment.FragmentLifecycleIntegration -keepnames class androidx.fragment.app.FragmentManager$FragmentLifecycleCallbacks # To ensure that stack traces is unambiguous diff --git a/sentry-android-ndk-outproc/proguard-rules.pro b/sentry-android-ndk-outproc/proguard-rules.pro index 022d5bb3a1e..1ae8ad4972c 100644 --- a/sentry-android-ndk-outproc/proguard-rules.pro +++ b/sentry-android-ndk-outproc/proguard-rules.pro @@ -1,6 +1,9 @@ ##---------------Begin: proguard configuration for NDK outproc ---------- # The Android SDK checks at runtime if this class is available via Class.forName --keep class io.sentry.android.ndk.outproc.SentryNdkOutproc { *; } +-keep class io.sentry.android.ndk.outproc.SentryNdkOutproc { + public static void init(...); + public static void close(); +} ##---------------End: proguard configuration for NDK outproc ---------- diff --git a/sentry-android-ndk/proguard-rules.pro b/sentry-android-ndk/proguard-rules.pro index db825e27b4d..e80adfa4fa3 100644 --- a/sentry-android-ndk/proguard-rules.pro +++ b/sentry-android-ndk/proguard-rules.pro @@ -1,7 +1,10 @@ ##---------------Begin: proguard configuration for NDK ---------- # The Android SDK checks at runtime if this class is available via Class.forName --keep class io.sentry.android.ndk.SentryNdk { *; } +-keep class io.sentry.android.ndk.SentryNdk { + public static void init(...); + public static void close(); +} # The JNI layer uses this class through reflection -keep class io.sentry.protocol.DebugImage { *; } diff --git a/sentry-android-replay/proguard-rules.pro b/sentry-android-replay/proguard-rules.pro index 90a78965e89..52f81053c34 100644 --- a/sentry-android-replay/proguard-rules.pro +++ b/sentry-android-replay/proguard-rules.pro @@ -4,7 +4,9 @@ # Rules to detect Images/Icons and mask them -dontwarn androidx.compose.ui.graphics.painter.Painter --keepnames class * extends androidx.compose.ui.graphics.painter.Painter +-dontwarn androidx.compose.ui.graphics.painter.ColorPainter +-dontwarn androidx.compose.ui.graphics.painter.BrushPainter +-dontwarn androidx.compose.ui.graphics.vector.VectorPainter -keepclasseswithmembernames class * { androidx.compose.ui.graphics.painter.Painter painter; } @@ -32,7 +34,7 @@ -dontwarn androidx.camera.view.PreviewView -keepnames class androidx.camera.view.PreviewView # Rules to walk the Compose Node tree. --keep class androidx.compose.ui.node.LayoutNode { +-keepclassmembers class androidx.compose.ui.node.LayoutNode { *** getChildren*(...); *** getOuterCoordinator*(...); *** getCollapsedSemantics*(...); diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt index a32c51174a8..7146f20464c 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt @@ -28,7 +28,10 @@ import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Rect import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ColorProducer +import androidx.compose.ui.graphics.painter.BrushPainter +import androidx.compose.ui.graphics.painter.ColorPainter import androidx.compose.ui.graphics.painter.Painter +import androidx.compose.ui.graphics.vector.VectorPainter import androidx.compose.ui.layout.LayoutCoordinates import androidx.compose.ui.layout.findRootCoordinates import androidx.compose.ui.node.LayoutNode @@ -111,20 +114,14 @@ internal fun LayoutNode.isMaskableImage(): Boolean? { } /** - * We heuristically check the known classes that are coming from local assets usually: - * [androidx.compose.ui.graphics.vector.VectorPainter] - * [androidx.compose.ui.graphics.painter.ColorPainter] - * [androidx.compose.ui.graphics.painter.BrushPainter] + * Only [VectorPainter], [ColorPainter], and [BrushPainter] are left unmasked, because they draw + * local vectors and fills. Every other Painter may draw a remote image, so it is masked. * * In theory, [androidx.compose.ui.graphics.painter.BitmapPainter] can also come from local assets, * but it can as well come from a network resource, so we preemptively mask it. */ -internal fun Painter.isMaskable(): Boolean { - val className = this::class.java.name - return !className.contains("Vector") && - !className.contains("Color") && - !className.contains("Brush") -} +internal fun Painter.isMaskable(): Boolean = + this !is VectorPainter && this !is ColorPainter && this !is BrushPainter /** * This method is necessary to mask text in Compose. diff --git a/sentry-android-replay/src/test/java/io/sentry/android/replay/util/NodesTest.kt b/sentry-android-replay/src/test/java/io/sentry/android/replay/util/NodesTest.kt new file mode 100644 index 00000000000..946ce877a73 --- /dev/null +++ b/sentry-android-replay/src/test/java/io/sentry/android/replay/util/NodesTest.kt @@ -0,0 +1,57 @@ +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") + +package io.sentry.android.replay.util + +import android.graphics.Bitmap +import androidx.compose.ui.geometry.Size +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.SolidColor +import androidx.compose.ui.graphics.asImageBitmap +import androidx.compose.ui.graphics.drawscope.DrawScope +import androidx.compose.ui.graphics.painter.BitmapPainter +import androidx.compose.ui.graphics.painter.BrushPainter +import androidx.compose.ui.graphics.painter.ColorPainter +import androidx.compose.ui.graphics.painter.Painter +import androidx.compose.ui.graphics.vector.VectorPainter +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.google.common.truth.Truth.assertThat +import kotlin.test.Test +import org.junit.runner.RunWith +import org.robolectric.annotation.Config + +@RunWith(AndroidJUnit4::class) +@Config(sdk = [35]) +class NodesTest { + + @Test + fun `VectorPainter is not maskable`() { + assertThat(VectorPainter().isMaskable()).isFalse() + } + + @Test + fun `ColorPainter is not maskable`() { + assertThat(ColorPainter(Color.Red).isMaskable()).isFalse() + } + + @Test + fun `BrushPainter is not maskable`() { + assertThat(BrushPainter(SolidColor(Color.Red)).isMaskable()).isFalse() + } + + @Test + fun `BitmapPainter is maskable`() { + val bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888).asImageBitmap() + assertThat(BitmapPainter(bitmap).isMaskable()).isTrue() + } + + @Test + fun `a custom Painter is maskable even when its name contains Color, Vector, or Brush`() { + assertThat(ColorVectorBrushPainter().isMaskable()).isTrue() + } + + private class ColorVectorBrushPainter : Painter() { + override val intrinsicSize: Size = Size.Unspecified + + override fun DrawScope.onDraw() = Unit + } +} diff --git a/sentry-android-timber/proguard-rules.pro b/sentry-android-timber/proguard-rules.pro index 87f033ce584..7bd6693d6bd 100644 --- a/sentry-android-timber/proguard-rules.pro +++ b/sentry-android-timber/proguard-rules.pro @@ -1,7 +1,7 @@ ##---------------Begin: proguard configuration for Timber ---------- # The Android SDK checks at runtime if these classes are available via Class.forName --keep class io.sentry.android.timber.SentryTimberIntegration { (...); } +-keepnames class io.sentry.android.timber.SentryTimberIntegration -keepnames class timber.log.Timber # To ensure that stack traces is unambiguous diff --git a/sentry-compose/proguard-rules.pro b/sentry-compose/proguard-rules.pro index 30473497a5f..2b8a6ca82b0 100644 --- a/sentry-compose/proguard-rules.pro +++ b/sentry-compose/proguard-rules.pro @@ -1,8 +1,8 @@ ##---------------Begin: proguard configuration for Compose ---------- # The Android SDK checks at runtime if these classes are available via Class.forName --keep class io.sentry.compose.gestures.ComposeGestureTargetLocator { (...); } --keep class io.sentry.compose.viewhierarchy.ComposeViewHierarchyExporter { (...); } +-keepnames class io.sentry.compose.gestures.ComposeGestureTargetLocator +-keepnames class io.sentry.compose.viewhierarchy.ComposeViewHierarchyExporter -keepnames interface androidx.compose.ui.node.Owner -keepclassmembers class androidx.compose.ui.node.LayoutNode { @@ -14,8 +14,8 @@ -keepnames class androidx.compose.foundation.ScrollingLayoutElement -keepnames class androidx.compose.foundation.ScrollingContainerElement -keepnames class androidx.compose.foundation.ScrollableAreaElement --keepnames class androidx.compose.ui.platform.TestTagElement { *; } --keepnames class io.sentry.compose.SentryModifier$SentryTagModifierNodeElement { *; } +-keepnames class androidx.compose.ui.platform.TestTagElement { java.lang.String tag; } +-keepnames class io.sentry.compose.SentryModifier$SentryTagModifierNodeElement { java.lang.String tag; } # R8 will warn about missing classes if people don't have androidx.compose-navigation on their # classpath, but this is fine, these classes are used in an internal class which is only used when