Skip to content

Support apps that depend on OkHttp 5 - #60385

Open
ide wants to merge 2 commits into
react:mainfrom
expo:feature/okhttp5-compat
Open

ide wants to merge 2 commits 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.
  • I ran a release build of RNTester (with R8) on an Android 16 emulator after adding implementation("com.squareup.okhttp3:okhttp:5.5.1"). ReactAndroid is still compiled against OkHttp 4.9.2, like the published AAR, and the app runs with okhttp-android 5.5.1, Okio 3.18.1 and okhttp-urlconnection 4.9.2. A test script checks that cookies are saved and sent back, and tries JSON and multipart requests, upload progress, Fresco image loading and a WebSocket. Cookies with non-ASCII characters in CookieManager are skipped, and the rest are still sent. Without this change, the same app crashes on its first request with NoClassDefFoundError: okhttp3/internal/Util from JavaNetCookieJar.loadForRequest.

@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.

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.

An app on OkHttp 5 needs to exclude it only if its code or a keep rule retains JavaNetCookieJar.

We probably need a page on the website explaining how to do this

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.

Added a section to the Networking page: react/react-native-website#5363

@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 2 times, most recently from 3e99c93 to ca8d6fc Compare December 3, 2026 03:35
@meta-codesync

meta-codesync Bot commented Dec 3, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D135940203.

ide added a commit to ide/react-native-website that referenced this pull request Dec 3, 2026
Why
===

react/react-native#60385 lets Android apps use OkHttp 5, starting with React Native 0.89. The Networking page doesn't say how to opt in, or what to do about the OkHttp 4 version of `okhttp-urlconnection` that `react-android` brings in.

How
===

Adds a "Using OkHttp 5 on Android" section to the Networking page, before "Configuring NSURLSession on iOS". It shows how to add OkHttp 5, when to also add `okhttp-urlconnection` at the same version, and how to exclude the OkHttp 4 `okhttp-urlconnection` from `react-android`.

Test Plan
=========

`npx prettier --check docs/network.md` and `npx eslint docs/network.md` pass.

Signed-off-by: James Ide <ide@expo.dev>
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.

Signed-off-by: James Ide <ide@expo.dev>
@ide
ide force-pushed the feature/okhttp5-compat branch from ca8d6fc to f79e78a Compare December 3, 2026 13:31
@javache

javache commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Some AI feedback :)

Suggested follow-up for React Native PR #60385

Keep the existing OkHttp 5 compatibility approach and keep CookieHandlerCookieJar internal. No new public API or dependency-version change is needed for this correction.

Cookie validation

Cookie.Builder accepts some cookie names and values that OkHttp subsequently rejects when constructing the request's Cookie header. Catching exceptions from Cookie.Builder alone does not cover that later failure.

In CookieHandlerCookieJar.decodeCookieHeader, validate both the decoded name and the unquoted, trimmed value before constructing the cookie. Permit only horizontal tab (\t) and printable ASCII (' '..'~'), matching OkHttp's request-header value validation. Keep this validation inside the existing IllegalArgumentException handling so only the bad cookie is skipped and valid cookies still load.

For example:

private fun isValidHeaderCharacter(c: Char): Boolean = c == '\t' || c in ' '..'~'
require(name.all(::isValidHeaderCharacter) && value.all(::isValidHeaderCharacter)) {
  "Invalid request header characters"
}

Use the existing url.redact() API in all three warning messages. Unlike url.resolve("/..."), it removes URL credentials as well as the private path and query. For rejected cookies, log a generic message rather than cookie contents or the builder's exception message.

Regression coverage

Extend CookieHandlerCookieJarTest to check:

  • A cookie containing Unicode can be built by Cookie.Builder, but using it in Request.Builder.header("Cookie", ...) throws IllegalArgumentException.
  • Embedded control characters other than tab, DEL (U+007F), and non-ASCII characters are skipped in both names and values. A valid cookie alongside them is preserved.
  • Spaces, embedded horizontal tabs, and printable ASCII such as ~ remain valid, and the resulting Cookie header can be constructed.
  • URL credentials and rejected cookie values do not appear in warning logs.
  • Existing comparison tests against JavaNetCookieJar still pass.

Validation to retain

The rejection regression failed against the original decoder and passed after the correction. With the logging regression included, the cookie and multipart-preview suites passed together: 31 tests, zero failures, using OkHttp 4.12.0. The public Android API snapshot is unchanged.

Run these from the GitHub repository, preserving the existing warnings-as-errors checks:

./gradlew --init-script .github/workflow-scripts/okhttp-compat.gradle \
  -PokhttpCompatVersion=3.14.9 -PenableWarningsAsErrors=true \
  :packages:react-native:ReactAndroid:verifyOkHttpCompatibility

./gradlew --init-script .github/workflow-scripts/okhttp-compat.gradle \
  -PokhttpCompatVersion=5.5.1 -PenableWarningsAsErrors=true \
  :packages:react-native:ReactAndroid:verifyOkHttpCompatibility

./gradlew -PenableWarningsAsErrors=true \
  :packages:react-native:ReactAndroid:testDebugUnitTest

yarn format

Those three Gradle configurations have not been rerun for this follow-up. The compatibility matrix verifies source compatibility by recompiling; it does not prove that a catalog-built AAR runs with OkHttp 5 and Okio 3. That mixed-version runtime smoke test also remains to be run for this follow-up. No public API snapshot update is needed because the adapter and helper remain internal.

Validate decoded cookie names and values against OkHttp's request-header character rules (tab and printable ASCII) before building the Cookie, use url.redact() in all warning logs, and omit cookie details from rejected-cookie warnings.

Signed-off-by: James Ide <ide@expo.dev>
@ide

ide commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, applied in 79eefcb:

  • Validate decoded cookie names and trimmed/unquoted values in CookieHandlerCookieJar.decodeCookieHeader to allow only \t and printable ASCII (' '..'~'), skipping invalid cookies inside the IllegalArgumentException catch so valid cookies still load.
  • Use url.redact() in all three warning logs, and log a generic message without cookie contents or the builder exception message for skipped cookies.
  • Added tests in CookieHandlerCookieJarTest covering Unicode/control/DEL rejection, spaces/tabs/printable ASCII acceptance with Request.Builder.header("Cookie", ...), and URL credential and cookie redaction in warnings.

Verified with -PenableWarningsAsErrors=true:

  • verifyOkHttpCompatibility on OkHttp 3.14.9 (Okio 2.9.0) and 5.5.1 (Okio 3.18.1)
  • :packages:react-native:ReactAndroid:testDebugUnitTest
  • yarn format

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.

3 participants