Skip to content

Support apps that depend on OkHttp 5 - #60385

Open
ide wants to merge 1 commit into
react:mainfrom
expo:feature/okhttp5-compat
Open

ide wants to merge 1 commit into
react:mainfrom
expo:feature/okhttp5-compat

Conversation

@ide

@ide ide commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

This makes OkHttp 5 usable in React Native apps on Android. Today an app that depends on OkHttp 5 crashes when React Native loads cookies (#52945): okhttp-urlconnection stays at 4.9.2, and its JavaNetCookieJar calls an OkHttp internal that 5 removed.

With this change, an app opts in by depending on OkHttp 5:

// android/app/build.gradle
dependencies {
  implementation("com.squareup.okhttp3:okhttp:5.5.1")
}

Apps whose dependencies don't select OkHttp 5 keep OkHttp 4.9.2, and React Native stays source-compatible with OkHttp 3.14.9.

  • CookieHandlerCookieJar replaces JavaNetCookieJar and uses only API that OkHttp 3, 4 and 5 share. React Native no longer uses okhttp-urlconnection, but ReactAndroid still declares it, so code that uses JavaNetCookieJar or JavaNetAuthenticator through React Native keeps compiling. An app on OkHttp 5 needs no change unless its code or a keep rule retains JavaNetCookieJar: R8's shrinking removes the unused 4.9.2 classes, so a minified build doesn't fail on the OkHttp 4 internals they call. An app that uses JavaNetCookieJar on OkHttp 5 declares okhttp-urlconnection at its OkHttp version. An app that wants the dependency gone can exclude it from its react-android dependency, and from any other dependency that brings it in:

    implementation("com.facebook.react:react-android") {
      exclude group: "com.squareup.okhttp3", module: "okhttp-urlconnection"
    }
  • DevTools previews multipart uploads part by part on OkHttp 5 too, whose MultipartBody reports itself as one-shot when any part is.

  • Response.body() null checks no longer warn on OkHttp 5, where the body is non-null.

  • The test_okhttp CI job in test-android.yml, from Add OkHttp 3 compatibility CI for ReactAndroid #60342, runs against OkHttp 3.14.9 and 5.5.1.

Changelog:

[ANDROID] [FIXED] - Fix a crash loading cookies in apps that depend on OkHttp 5

Test Plan:

  • ./gradlew --init-script .github/workflow-scripts/okhttp-compat.gradle -PokhttpCompatVersion=5.5.1 -PenableWarningsAsErrors=true :packages:react-native:ReactAndroid:verifyOkHttpCompatibility passes, and so does the same command with 3.14.9. ./gradlew -PenableWarningsAsErrors=true :packages:react-native:ReactAndroid:testDebugUnitTest passes on the catalog's 4.9.2.
  • CookieHandlerCookieJarTest checks the port against JavaNetCookieJar from the OkHttp version under test.
  • With okhttp 5.5.1 and okhttp-urlconnection 4.9.2, JavaNetCookieJar.loadForRequest throws NoClassDefFoundError: okhttp3/internal/Util, and the port works.
  • ReactAndroid compiled against OkHttp 4.9.2, as the published AAR is, links against OkHttp 5.5.1 and Okio 3.18.1.
  • R8 9.3.16 minifies a small app on okhttp-android 5.5.1 and Okio 3.18.1 with okhttp-urlconnection 4.9.2 among its inputs. When the app keeps JavaNetCookieJar, R8 fails with Missing class okhttp3.internal.Util.

Not run: an app on a device with OkHttp 5, and Meta's internal build with OkHttp 3.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 29, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 29, 2026
@ide
ide marked this pull request as ready for review November 29, 2026 10:48
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 29, 2026
@ide
ide force-pushed the feature/okhttp5-compat branch from 0948886 to 4538fa6 Compare December 1, 2026 00:13

@cortinico cortinico left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sending this @ide - we should be able to make it non-breaking though

api(libs.yoga.proguard.annotations)

api(libs.jsr305)
api(libs.okhttp3.urlconnection)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be made non-breaking as apps can declare in their .gradle files that they don't intend to consume this api dependency from react-native.

@ide ide Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactAndroid now keeps okhttp-urlconnection as an api dependency, so the change is no longer breaking, and the [BREAKING] changelog entry is gone.
An app on OkHttp 5 needs to exclude it only if its code or a keep rule retains JavaNetCookieJar.

@ide
ide force-pushed the feature/okhttp5-compat branch 2 times, most recently from f378dfa to 13c1611 Compare December 2, 2026 15:40
@ide
ide force-pushed the feature/okhttp5-compat branch from 13c1611 to 3e99c93 Compare December 3, 2026 03:07
An app that depends on OkHttp 5 crashes when React Native loads cookies (react#52945). Gradle resolves okhttp to the app's 5.x, but okhttp-urlconnection stays at the 4.9.2 that ReactAndroid declares, and its JavaNetCookieJar calls okhttp3.internal.Util, which OkHttp 5 removed. No okhttp-urlconnection release works with OkHttp 3, 4 and 5, because each calls internals of its own major version.

Add CookieHandlerCookieJar, a port of OkHttp 5's JavaNetCookieJar onto API that OkHttp 3, 4 and 5 share, and use it in NetworkingModule and FrescoModule. React Native no longer uses okhttp-urlconnection. ReactAndroid still declares it as an api dependency, so code that uses JavaNetCookieJar or JavaNetAuthenticator through React Native keeps compiling, and the tests use it to compare the port with JavaNetCookieJar from the OkHttp version under test. An app on OkHttp 5 needs no change unless its code or a keep rule retains JavaNetCookieJar: R8's shrinking removes the unused 4.9.2 classes, so a minified build doesn't fail on the OkHttp 4 internals they call. An app that uses JavaNetCookieJar on OkHttp 5 declares okhttp-urlconnection at its OkHttp version, and an app that wants the dependency gone can exclude it from react-android and any other dependency that brings it in.

Like OkHttp 5.5.1's, the port accepts a value that is a lone quote and a value with whitespace just inside its quotes; earlier versions throw on both. Unlike JavaNetCookieJar, it logs and skips a cookie that Cookie.Builder rejects, such as one whose name starts with a vertical tab. Uncaught, that exception fails the request, and OkHttp rethrows it on its dispatcher thread, which crashes the app.

Two other things differ on OkHttp 5:

- Response.body() is non-null, so seven null checks that OkHttp 3 and 4 need become warnings, which -PenableWarningsAsErrors=true makes errors. Each check now reads the body into an explicitly nullable value first.
- MultipartBody reports itself as one-shot when any part is, so getRequestBodyPreview showed a form upload with a file as only "[Binary data, N bytes]" in DevTools. It now checks the parts first.

Run the OkHttp compatibility job as a matrix over OkHttp 3.14.9 and 5.5.1, with warnings as errors. The init script takes the version as -PokhttpCompatVersion and logs the Okio version it resolves. Add the OkHttp 5 rules to AGENTS.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ide
ide force-pushed the feature/okhttp5-compat branch from 3e99c93 to ca8d6fc Compare December 3, 2026 03:35

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants