Skip to content

Commit 6937bd5

Browse files
committed
feat(nav3): Add the SerialName back stack mapper
1 parent 19c6402 commit 6937bd5

26 files changed

Lines changed: 3348 additions & 43 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
## Unreleased
99

10+
### Features
11+
12+
- Nav3: Add `BackStackEntryMapper.SerialName`, a convenience mapper that names back stack entries after their kotlinx.serialization serial names ([#6703](https://github.com/getsentry/sentry-java/pull/6703))
13+
1014
### Fixes
1115

1216
- Session Replay: Mask Compose images loaded with Coil 3.1+ `AsyncImage` when `maskAllImages` is enabled ([#6759](https://github.com/getsentry/sentry-java/pull/6759))
@@ -17,6 +21,7 @@
1721

1822
### Dependencies
1923

24+
- Nav3: `sentry-android-navigation3` now depends on `kotlinx-serialization-core` 1.7.3 or later, the version every Nav3 release already depends on, so it never raises a Nav3 app's version ([#6703](https://github.com/getsentry/sentry-java/pull/6703))
2025
- Nav3: `sentry-android-navigation3` no longer declares a dependency on `androidx.lifecycle:lifecycle-runtime-compose`. Nav3 apps already depend on it via `navigation3-ui`, so no app changes are necessary ([#6699](https://github.com/getsentry/sentry-java/pull/6699))
2126

2227
## 8.65.1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Dependency test matrices, registered as the `matrixLibs` catalog: compile against the oldest
2+
# supported version and test against newer releases too.
3+
#
4+
# Kept apart from libs.versions.toml so that each matrix's tests can run on PRs that change its
5+
# versions, without also running on every unrelated dependency bump.
6+
#
7+
# Each matrix <name> has a <name>-floor version key: the oldest version we support, which we
8+
# compile and test against. Only changed by hand, in a reviewed change. No library entry references
9+
# it, so Dependabot never bumps it. Builds apply it explicitly (e.g., `version { strictly(...) }`)
10+
# instead.
11+
#
12+
# Never give a floor key a library entry with `version.ref`, and never protect it with a Dependabot
13+
# `ignore` rule: ignore rules match a dependency's group and name, so they would also stop
14+
# Dependabot from bumping other versions of the same module.
15+
16+
[versions]
17+
# The oldest kotlinx.serialization version sentry-android-navigation3 supports, which it compiles and
18+
# tests against. Before changing it, read the rules under "Version floor" in
19+
# sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/README.md.
20+
kotlinxSerialization-floor = "1.7.3"
21+
22+
[libraries]
23+
# Applied with the floor version by the builds that use it.
24+
kotlinxSerialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core" }

‎gradle/libs.versions.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ kotlin-spring = { id = "org.jetbrains.kotlin.plugin.spring", version.ref = "kotl
7070
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
7171
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
7272
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
73+
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
7374
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
7475
buildconfig = { id = "com.github.gmazzo.buildconfig", version = "5.6.5" }
7576
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }

‎sentry-android-navigation3/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Use `SentryNavEffect` alongside your `NavDisplay` to record navigation transacti
66
users move around your app. Configure the data you want captured via `SentryNavOptions`.
77

88
Because Nav3 back stacks can have arbitrary key types, host apps need to provide `SentryNavEffect` with a mapping between their back stack
9-
keys and the data Sentry should display for each (see `BackStackEntryMapper`).
9+
keys and the data Sentry should display for each (see `BackStackEntryMapper` and the built-in `SerialName` mapper).
1010

1111
See the [Navigation for Android docs](https://docs.sentry.io/platforms/android/integrations/navigation/) for additional details, including
1212
configuration, supported setups, and limitations.

‎sentry-android-navigation3/api/sentry-android-navigation3.api‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
public abstract interface class io/sentry/compose/navigation3/BackStackEntryMapper {
2+
public static final field Companion Lio/sentry/compose/navigation3/BackStackEntryMapper$Companion;
23
public abstract fun map (Ljava/lang/Object;)Lio/sentry/compose/navigation3/SentryBackStackEntry;
34
}
45

6+
public final class io/sentry/compose/navigation3/BackStackEntryMapper$Companion {
7+
public final fun getSerialName ()Lio/sentry/compose/navigation3/BackStackEntryMapper;
8+
}
9+
510
public final class io/sentry/compose/navigation3/BuildConfig {
611
public static final field BUILD_TYPE Ljava/lang/String;
712
public static final field DEBUG Z

‎sentry-android-navigation3/build.gradle.kts‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
alias(libs.plugins.kotlin.android)
88
alias(libs.plugins.kotlin.compose)
99
alias(libs.plugins.gradle.versions)
10+
alias(libs.plugins.kotlin.serialization)
1011
id("io.sentry.detekt")
1112
id("io.sentry.spotless")
1213
id("io.sentry.jdk-target")
@@ -15,6 +16,8 @@ plugins {
1516
id("io.sentry.robolectric")
1617
}
1718

19+
val kotlinxSerializationFloor = matrixLibs.versions.kotlinxSerialization.floor.get()
20+
1821
android {
1922
compileSdk = libs.versions.compileSdk.get().toInt()
2023
namespace = "io.sentry.compose.navigation3"
@@ -45,6 +48,8 @@ android {
4548
// AGP 9.5 can't create the Compose Preview task without it, which breaks any
4649
// build that creates every task, such as an LSP's Gradle import.
4750
isIncludeAndroidResources = true
51+
// Lets tests check that MIN_KOTLINX_SERIALIZATION_VERSION matches the catalog's floor.
52+
all { it.systemProperty("nav3.kotlinxSerializationFloor", kotlinxSerializationFloor) }
4853
}
4954
}
5055

@@ -72,12 +77,34 @@ android {
7277

7378
kotlin { explicitApi() }
7479

80+
// Unit tests run against the kotlinx.serialization floor by default. To run them against a newer
81+
// version, as apps may ship a newer kotlinx.serialization dependency than
82+
// sentry-android-navigation3 compiles against, pass -Pnav3TestKotlinxVersion=<version>.
83+
val testKotlinxVersion =
84+
providers.gradleProperty("nav3TestKotlinxVersion").orNull ?: kotlinxSerializationFloor
85+
7586
dependencies {
7687
implementation(projects.sentry)
7788
compileOnly(libs.androidx.lifecycle.runtime.compose)
7889

7990
compileOnly(libs.androidx.compose.runtime)
8091
compileOnly(libs.androidx.compose.ui)
92+
// Every Nav3 release depends on kotlinx.serialization (navigation3-runtime declares the floor,
93+
// 1.7.3, directly), so this never raises a Nav3 app's version. It guarantees the built-in
94+
// mappers' classes are present for apps that use this module without Nav3. A minimum, as the
95+
// version is
96+
// published: strictly() would force it on apps.
97+
implementation(matrixLibs.kotlinxSerialization.core) {
98+
version { require(kotlinxSerializationFloor) }
99+
}
100+
// Compiles against exactly the floor. Strict, as other compile dependencies (e.g., Compose UI
101+
// via savedstate) also bring in kotlinx.serialization. With a mere minimum, a bump of one of
102+
// those could silently raise the version we compile against, and the same source could then
103+
// compile to calls that the floor version's runtime lacks. compileOnly, so it isn't published.
104+
// verifyKotlinxSerializationCompileVersion checks the result.
105+
compileOnly(matrixLibs.kotlinxSerialization.core) {
106+
version { strictly(kotlinxSerializationFloor) }
107+
}
81108

82109
testImplementation(projects.sentryAndroidCore)
83110
testImplementation(projects.sentryTestSupport)
@@ -88,11 +115,137 @@ dependencies {
88115
testImplementation(libs.androidx.viewpager2)
89116
testImplementation(libs.google.truth)
90117
testImplementation(libs.kotlin.test.junit)
118+
testImplementation(matrixLibs.kotlinxSerialization.core) {
119+
// A minimum, not strictly(), so that a test dependency that needs a newer version (e.g., a
120+
// future Nav3) raises the resolved version instead of being silently downgraded, and
121+
// verifyKotlinxSerializationTestVersion fails.
122+
version { require(testKotlinxVersion) }
123+
}
91124
testImplementation(libs.mockito.core)
92125
testImplementation(libs.mockito.kotlin)
93126
testImplementation(libs.roboelectric)
94127
}
95128

129+
/**
130+
* Registers a task that fails the build if [configurationName] resolves kotlinx-serialization-core
131+
* to any version other than [expectedVersion], naming the dependencies that requested another one.
132+
* [problem] describes why the version matters, and [fix] what to do if it's wrong.
133+
*/
134+
fun registerKotlinxSerializationVersionCheck(
135+
name: String,
136+
configurationName: String,
137+
expectedVersion: String,
138+
problem: String,
139+
fix: String,
140+
) =
141+
tasks.register(name) {
142+
val resolution =
143+
configurations.named(configurationName).flatMap { classpath ->
144+
classpath.incoming.resolutionResult.rootComponent.map { root ->
145+
val resolved = sortedSetOf<String>()
146+
val requests = sortedSetOf<String>()
147+
val visited = mutableSetOf<ResolvedComponentResult>()
148+
fun visit(component: ResolvedComponentResult) {
149+
if (!visited.add(component)) return
150+
val id = component.id
151+
if (
152+
id is ModuleComponentIdentifier &&
153+
id.group == "org.jetbrains.kotlinx" &&
154+
id.module.startsWith("kotlinx-serialization-core")
155+
) {
156+
resolved += "${id.module}:${id.version}"
157+
}
158+
for (dependency in component.dependencies) {
159+
val requested = dependency.requested.displayName
160+
if (
161+
"org.jetbrains.kotlinx:kotlinx-serialization-core" in requested &&
162+
!dependency.isConstraint &&
163+
!requested.endsWith(":$expectedVersion") &&
164+
// kotlinx-serialization-core requests its own -jvm variant.
165+
!id.displayName.startsWith("org.jetbrains.kotlinx:kotlinx-serialization-core")
166+
) {
167+
requests += "${dependency.from.id.displayName} requests $requested"
168+
}
169+
when (dependency) {
170+
is ResolvedDependencyResult -> visit(dependency.selected)
171+
is UnresolvedDependencyResult ->
172+
if ("kotlinx-serialization-core" in requested) {
173+
resolved += "$requested (unresolved)"
174+
}
175+
}
176+
}
177+
}
178+
visit(root)
179+
listOf(resolved.toList(), requests.toList())
180+
}
181+
}
182+
inputs.property("expectedVersion", expectedVersion)
183+
inputs.property("resolution", resolution)
184+
doLast {
185+
val (resolved, requests) = resolution.get()
186+
val unexpected = resolved.filterNot { it.endsWith(":$expectedVersion") }
187+
check(resolved.isNotEmpty() && unexpected.isEmpty()) {
188+
"$problem, but $configurationName resolved " +
189+
"${resolved.ifEmpty { listOf("no kotlinx-serialization-core") }}" +
190+
(if (requests.isEmpty()) "" else " (${requests.joinToString("; ")})") +
191+
". $fix"
192+
}
193+
}
194+
}
195+
196+
/**
197+
* Fails the build if the release compile classpath resolves kotlinx.serialization to any version
198+
* other than the floor, e.g., because another dependency declared a conflicting strict version or a
199+
* resolution rule forced one. We must compile against the floor, as that's the oldest runtime the
200+
* module promises to work with.
201+
*/
202+
val verifyKotlinxSerializationCompileVersion =
203+
registerKotlinxSerializationVersionCheck(
204+
name = "verifyKotlinxSerializationCompileVersion",
205+
configurationName = "releaseCompileClasspath",
206+
expectedVersion = kotlinxSerializationFloor,
207+
problem =
208+
"sentry-android-navigation3 must compile against kotlinx.serialization " +
209+
"$kotlinxSerializationFloor (the kotlinxSerialization-floor in " +
210+
"gradle/dependency-matrix.versions.toml)",
211+
fix = "Find what changed the version, e.g., a strict version or a resolution rule.",
212+
)
213+
214+
/**
215+
* Fails the build if the unit tests would run against a kotlinx.serialization version other than
216+
* the one under test, e.g., because a test dependency such as Nav3 now requires a newer one. The
217+
* tests would then not test that version, and a pin that downgraded the dependency instead would
218+
* run it on a version it doesn't support, which no real app can have.
219+
*/
220+
val verifyKotlinxSerializationTestVersion =
221+
registerKotlinxSerializationVersionCheck(
222+
name = "verifyKotlinxSerializationTestVersion",
223+
configurationName = "releaseUnitTestRuntimeClasspath",
224+
expectedVersion = testKotlinxVersion,
225+
problem = "The unit tests must run against kotlinx.serialization $testKotlinxVersion",
226+
fix =
227+
"If a test dependency (e.g., navigation3-runtime) now requires a newer version, test the " +
228+
"floor with the newest release of that dependency that accepts the floor, as apps on " +
229+
"older releases still run the floor. Raise kotlinxSerialization-floor only to drop " +
230+
"support for those apps. See \"Version floor\" in the kotlinx package README.",
231+
)
232+
233+
tasks
234+
.named { it == "compileReleaseKotlin" }
235+
.configureEach {
236+
dependsOn(verifyKotlinxSerializationCompileVersion)
237+
}
238+
239+
tasks
240+
.named { it == "testReleaseUnitTest" }
241+
.configureEach {
242+
dependsOn(verifyKotlinxSerializationTestVersion)
243+
}
244+
245+
tasks.named("check") {
246+
dependsOn(verifyKotlinxSerializationCompileVersion, verifyKotlinxSerializationTestVersion)
247+
}
248+
96249
tasks.withType<Detekt>().configureEach {
97250
// Target version of the generated JVM bytecode. It is used for type resolution.
98251
jvmTarget = JavaVersion.VERSION_1_8.toString()

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@
55
-keepattributes LineNumberTable,SourceFile
66

77
##---------------End: proguard configuration for Compose ----------
8+
9+
##---------------Begin: proguard configuration for the kotlinx.serialization compatibility probe ----------
10+
11+
# The probe looks up this SDK-owned fixture's serializer by reflection, as the built-in mappers look
12+
# up those of apps' back stack entries. kotlinx.serialization's rules before 1.10.0 keep an object's
13+
# serializer() only while its INSTANCE field is kept, and R8 full mode removes INSTANCE here, as the
14+
# SDK only refers to the object's class. The probe would then be inconclusive for object lookups.
15+
-keepclassmembers class io.sentry.compose.navigation3.internal.backstack.kotlinx.KotlinxCompatibilityProbe$Companion$ProbeObject {
16+
public static ** INSTANCE;
17+
kotlinx.serialization.KSerializer serializer(...);
18+
}
19+
20+
##---------------End: proguard configuration for the kotlinx.serialization compatibility probe ----------

0 commit comments

Comments
 (0)