Skip to content

fix: avoid kotlin.time.Duration in CacheControl to fix Kotlin 2.3 compat#9397

Open
faraz152 wants to merge 1 commit intosquare:masterfrom
faraz152:fix/9347-duration-kotlin-2.3-compat
Open

fix: avoid kotlin.time.Duration in CacheControl to fix Kotlin 2.3 compat#9397
faraz152 wants to merge 1 commit intosquare:masterfrom
faraz152:fix/9347-duration-kotlin-2.3-compat

Conversation

@faraz152
Copy link
Copy Markdown

Summary

Fixes #9347NoSuchMethodError: Duration.Companion.fromRawValue-UwyO8pc when OkHttp 5.x (compiled with Kotlin 2.2.x) runs against Kotlin 2.3 stdlib.

Root cause

commonForceCache() in -CacheControlCommon.kt uses Int.MAX_VALUE.seconds, which calls kotlin.time.Duration.Companion.secondstoDurationdurationOfNanosDuration.Companion.fromRawValue. The fromRawValue method is @PublishedApi internal in 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:

.maxStale(Int.MAX_VALUE.seconds)          // uses kotlin.time.Duration

With:

.maxStale(Int.MAX_VALUE, TimeUnit.SECONDS)  // uses OkHttp's own TimeUnit overload

This avoids the kotlin.time.Duration code path entirely. The TimeUnit-based maxStale overload is OkHttp's own public API — no stdlib internals involved.

Changes

  • -CacheControlCommon.kt: Replace kotlin.time.Duration.Companion.seconds import with java.util.concurrent.TimeUnit, change maxStale call to use TimeUnit overload

Testing

  • Existing CacheControlTest and CacheControlJvmTest cover FORCE_CACHE behavior — the value of maxStaleSeconds is unchanged (Int.MAX_VALUE)
  • The fix is a 2-line change with identical runtime semantics

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
@JakeWharton
Copy link
Copy Markdown
Collaborator

Is there a YouTrack issue which details the breaking change? Something published should still be stable.

@faraz152
Copy link
Copy Markdown
Author

I couldn't find an existing YouTrack issue for this specific breakage (Duration.Companion.fromRawValue-UwyO8pc removed in 2.3). There are older related issues:

  • KT-46108NoSuchMethodError when using kotlin.time.Duration as parameter (Kotlin 1.5 era)
  • KT-41546Duration$Companion.getZERO NoSuchMethodError with coroutines

But neither covers the 2.2→2.3 fromRawValue change. Agreed that @PublishedApi internal should be binary stable — this looks like it warrants a new YouTrack issue. Want me to file one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NoSuchMethodError: Duration.Companion.fromRawValue-UwyO8pc with Kotlin 2.3

2 participants