Skip to content

Commit c8bea42

Browse files
committed
feat(nav3): Add the SerialName back stack mapper
BackStackEntryMapper.SerialName names each back stack entry after its kotlinx.serialization serial name, which R8 doesn't obfuscate, and records no properties. kotlinx-serialization-core is a compileOnly dependency at 1.7.3 or later. If the app's version can't be linked, the mapper falls back to naming every entry /unknown. The mapper returns null, which is public API, for entries it can't name, so it reports why through an internal per-thread side channel that the converter reads around each mapping call. Its reasons are entry_not_annotated, generic_entry_type, serializer_not_found, serializer_access_threw, and kotlinx_serialization_version. Unit tests run against the 1.7.3 floor by default, and against the latest stable release with -Pnav3TestKotlinx=latestStable.
1 parent fb33ea0 commit c8bea42

34 files changed

Lines changed: 3329 additions & 160 deletions

‎CHANGELOG.md‎

Lines changed: 4 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 [serial names](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.descriptors/-serial-descriptor/serial-name.html) ([#6703](https://github.com/getsentry/sentry-java/pull/6703))
13+
1014
### Improvements
1115

1216
- Nav3: Record why a back stack entry is `/unknown` via an `unknown_entry_reason` next to it in the Sentry UI ([#6781](https://github.com/getsentry/sentry-java/pull/6781))
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Dependency matrices that let builds compile against a stable oldest supported version, while testing
2+
# against that version and newer, automatically-updating releases. Helps ensure our source code only uses
3+
# APIs available in the oldest supported version, while remaining compatible with subsequent releases.
4+
#
5+
# Each matrix <name> defines two `[versions]`:
6+
#
7+
# - <name>-floor: The oldest supported version. Only changed manually via a reviewed PR.
8+
# Should not be referenced by any `version.ref` so that Dependabot leaves it alone. Instead,
9+
# build files should apply it explicitly, e.g., with `version { strictly(...) }`.
10+
#
11+
# - <name>-latestStable: The latest stable release. An unused <name>-latestStable entry in `[libraries]`
12+
# should point to the <name>-latestStable key so that Dependabot can update it. Build files can
13+
# select between floor and latestStable.
14+
#
15+
# An example:
16+
#
17+
# // In dependency-matrix.versions.toml
18+
# [versions]
19+
# example-floor = "1.0.0"
20+
# example-latestStable = "1.5.0"
21+
#
22+
# [libraries]
23+
# example-core = { module = "com.example:core" }
24+
# example-latestStable = { module = "com.example:core", version.ref = "example-latestStable" }
25+
#
26+
# // In build.gradle.kts
27+
# val testVersion =
28+
# // CI, etc. can use -PtestExample=<version> to set the desired version.
29+
# when (providers.gradleProperty("testExample").getOrElse("floor")) {
30+
# "floor" -> matrixLibs.versions.example.floor.get()
31+
# "latestStable" -> matrixLibs.versions.example.latestStable.get()
32+
# else -> error("testExample must be floor or latestStable")
33+
# }
34+
#
35+
# dependencies {
36+
# testImplementation(matrixLibs.example.core) {
37+
# version { strictly(testVersion) }
38+
# }
39+
# }
40+
#
41+
# Additional rules:
42+
#
43+
# - Never give the floor key a library entry with `version.ref`, and never protect it with a
44+
# Dependabot `ignore` rule. Ignore rules match a dependency's group and name, so they'd also
45+
# prevent Dependabot from bumping <name>-latestStable.
46+
#
47+
# - A latestStable library entry mustn't share its module with any entry or build file declaration that
48+
# Dependabot also updates (e.g., in libs.versions.toml). Dependabot merges declarations of one
49+
# module and bumps them all to one version. If they would collide, track a sibling artifact
50+
# instead (e.g., `-jvm`).
51+
52+
[versions]
53+
# The oldest kotlinx.serialization version sentry-android-navigation3 supports. Before changing it, read the rules under "Version floor" in
54+
# that module's .kotlinx package README.
55+
kotlinxSerialization-floor = "1.7.3"
56+
kotlinxSerialization-latestStable = "1.13.0"
57+
58+
[libraries]
59+
kotlinxSerialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core" }
60+
kotlinxSerialization-latestStable = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinxSerialization-latestStable" }

‎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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
Use `SentryNavEffect` alongside your `NavDisplay` to record navigation transactions, breadcrumbs, screen names, and back stack context as
66
users move around your app. Configure the data you want captured via `SentryNavOptions`.
77

8+
This module requires Nav3. It doesn't declare its own dependencies on Compose, lifecycle, or kotlinx.serialization, but uses the versions
9+
your Nav3 dependency brings in.
10+
811
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`).
12+
keys and the data Sentry should display for each (see `BackStackEntryMapper` and the built-in `SerialName` mapper).
1013

1114
See the [Navigation for Android docs](https://docs.sentry.io/platforms/android/integrations/navigation/) for additional details, including
1215
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: 21 additions & 1 deletion
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,24 @@ android {
7277

7378
kotlin { explicitApi() }
7479

80+
// Unit tests run against the kotlinx.serialization floor by default. CI also runs them against the
81+
// latest stable release, as apps may ship a newer kotlinx.serialization dependency.
82+
val testKotlinxVersion =
83+
when (val selection = providers.gradleProperty("nav3TestKotlinx").getOrElse("floor")) {
84+
"floor" -> kotlinxSerializationFloor
85+
"latestStable" -> matrixLibs.versions.kotlinxSerialization.latestStable.get()
86+
else -> error("nav3TestKotlinx must be floor or latestStable, but was $selection.")
87+
}
88+
7589
dependencies {
7690
implementation(projects.sentry)
77-
compileOnly(libs.androidx.lifecycle.runtime.compose)
7891

7992
compileOnly(libs.androidx.compose.runtime)
8093
compileOnly(libs.androidx.compose.ui)
94+
compileOnly(libs.androidx.lifecycle.runtime.compose)
95+
compileOnly(matrixLibs.kotlinxSerialization.core) {
96+
version { strictly(kotlinxSerializationFloor) }
97+
}
8198

8299
testImplementation(projects.sentryAndroidCore)
83100
testImplementation(projects.sentryTestSupport)
@@ -91,6 +108,9 @@ dependencies {
91108
testImplementation(libs.mockito.core)
92109
testImplementation(libs.mockito.kotlin)
93110
testImplementation(libs.roboelectric)
111+
testImplementation(matrixLibs.kotlinxSerialization.core) {
112+
version { strictly(testKotlinxVersion) }
113+
}
94114
}
95115

96116
tasks.withType<Detekt>().configureEach {

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

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

77
##---------------End: proguard configuration for Compose ----------
8+
9+
##---------------Begin: proguard configuration for kotlinx.serialization ----------
10+
11+
# The kotlinx.serialization compatibility probe looks up this SDK-owned fixture's serializer via reflection, as the built-in mappers look
12+
# up those of apps' back stack entries. kotlinx.serialization's R8 rules before 1.8.0 keep an object's serializer() only while its INSTANCE
13+
# field is kept, and R8 full mode removes INSTANCE here, as the SDK only refers to the object's class. The probe would then be inconclusive
14+
# for object lookups.
15+
-keepclassmembers class io.sentry.compose.navigation3.internal.backstack.kotlinx.KotlinxCompatibilityProbe$ProbeObject {
16+
public static ** INSTANCE;
17+
kotlinx.serialization.KSerializer serializer(...);
18+
}
19+
20+
# Serial names survive R8 minification because Kotlin's serialization compiler plugin fixes them at compile time. Our built-in mapper
21+
# fallback for private `@Serializable` objects relies on members preserved by kotlinx.serialization's own consumer rules.
22+
23+
# We don't ship rules for `@Serializable` classes with named companion objects. kotlinx.serialization's rules cover them but only from
24+
# 1.10.0 on. A generic rule set that fixes older versions exists, but we don't apply it because it would keep `InnerClasses` and
25+
# named-companion class names in every host app.
26+
27+
# We don't include a -dontwarn rule for kotlinx.serialization even though it's a compileOnly dependency. The only apps potentially affected
28+
# are those that use sentry-android-navigation3 but not Nav3 (which brings in kotlinx-serialization-core via navigation3-runtime). Adding a
29+
# broad `-dontwarn kotlinx.serialization.**` rule could suppress legitimate – and unrelated – R8 missing-class errors for apps that use
30+
# Nav3, causing them to crash at runtime rather than fail at buildtime. Apps that need the -dontwarn rule (likely the null set) can add it
31+
# themselves.
32+
33+
##---------------End: proguard configuration for kotlinx.serialization ----------

0 commit comments

Comments
 (0)