@@ -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+
1821android {
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
7378kotlin { 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+
7586dependencies {
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+
96249tasks.withType<Detekt >().configureEach {
97250 // Target version of the generated JVM bytecode. It is used for type resolution.
98251 jvmTarget = JavaVersion .VERSION_1_8 .toString()
0 commit comments