fix: avoid kotlin.time.Duration in CacheControl to fix Kotlin 2.3 compat#9397
fix: avoid kotlin.time.Duration in CacheControl to fix Kotlin 2.3 compat#9397faraz152 wants to merge 1 commit intosquare:masterfrom
Conversation
Replace `Int.MAX_VALUE.seconds` with `maxStale(Int.MAX_VALUE, TimeUnit.SECONDS)` in `commonForceCache()`. The `.seconds` extension goes through internal stdlib methods (`Duration.Companion.fromRawValue`) whose mangled names changed in Kotlin 2.3, causing NoSuchMethodError at runtime when OkHttp (compiled with Kotlin 2.2.x) runs against Kotlin 2.3 stdlib. Using the TimeUnit-based overload avoids the kotlin.time.Duration code path entirely, making OkHttp binary-compatible across Kotlin 2.2.x and 2.3.x. Fixes square#9347
|
Is there a YouTrack issue which details the breaking change? Something published should still be stable. |
|
I couldn't find an existing YouTrack issue for this specific breakage (
But neither covers the 2.2→2.3 |
|
@faraz152 you filing an issue and linking here would be good. |
|
@yschimke Will do — I'm preparing a YouTrack issue now for the |
|
Filed the YouTrack issue: KT-85558 — Binary incompatibility in kotlin.time.Duration between stdlib 2.2.x and 2.3.x |
Summary
Fixes #9347 —
NoSuchMethodError: Duration.Companion.fromRawValue-UwyO8pcwhen OkHttp 5.x (compiled with Kotlin 2.2.x) runs against Kotlin 2.3 stdlib.Root cause
commonForceCache()in-CacheControlCommon.ktusesInt.MAX_VALUE.seconds, which callskotlin.time.Duration.Companion.seconds→toDuration→durationOfNanos→Duration.Companion.fromRawValue. ThefromRawValuemethod is@PublishedApi internalin stdlib, and its mangled name changed between Kotlin 2.2.x and 2.3.x. When Gradle resolves a single stdlib (2.3.x wins), the OkHttp bytecode referencing the old mangled name crashes.Fix
Replace:
With:
This avoids the
kotlin.time.Durationcode path entirely. TheTimeUnit-basedmaxStaleoverload is OkHttp's own public API — no stdlib internals involved.Changes
-CacheControlCommon.kt: Replacekotlin.time.Duration.Companion.secondsimport withjava.util.concurrent.TimeUnit, changemaxStalecall to useTimeUnitoverloadTesting
CacheControlTestandCacheControlJvmTestcoverFORCE_CACHEbehavior — the value ofmaxStaleSecondsis unchanged (Int.MAX_VALUE)