Skip URLSession on Android to drop libFoundationNetworking from DT_NEEDED#438
Merged
Merged
Conversation
…EDED Wraps the outer `#if Foundation` in `URLSession.swift` with `&& !os(Android)`, skipping the file entirely when cross-compiling for the Swift Android SDK. Why this file specifically: the `UnimplementedURLProtocol: URLProtocol` subclass near the bottom registers `URLProtocol` class metadata at module init. On Android, `URLProtocol` lives in `libFoundationNetworking.so`, so the metadata reference forces the linker to add that lib to the consumer binary's `DT_NEEDED` list — ~16 MB of dead weight for projects that never reach for `\.urlSession` and would otherwise drop the lib entirely. No source change outside the `#if` guard. No behavior change on Apple, Linux, WASM, or any other target — `canImport(Android)` is false everywhere else. The `\.urlSession` dependency value remains available on every other platform that has the Foundation trait enabled. Consumers that *do* need `\.urlSession` on Android can re-introduce it downstream behind their own gate, or pull `URLSession` from `FoundationNetworking` directly.
Member
|
Hi @mansbernhardt, thanks for investigating this. I think a better change would be to introduce a dedicated |
Contributor
Author
|
Sounds good, I have pushed an update adding a FoundationNetworking trait. |
mbrandonw
reviewed
May 12, 2026
mbrandonw
reviewed
May 12, 2026
Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com>
mbrandonw
reviewed
May 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Wraps the outer
#if FoundationinURLSession.swiftwith&& !os(Android), skipping the file entirely when cross-compiling for the Swift Android SDK. Why this file specifically: theUnimplementedURLProtocol: URLProtocolsubclass near the bottom registersURLProtocolclass metadata at module init. On Android,URLProtocollives inlibFoundationNetworking.so, so the metadata reference forces the linker to add that lib to the consumer binary'sDT_NEEDEDlist — ~16 MB of dead weight for projects that never reach for\.urlSessionand would otherwise drop the lib entirely. No source change outside the#ifguard. No behavior change on Apple, Linux, WASM, or any other target —canImport(Android)is false everywhere else. The\.urlSessiondependency value remains available on every other platform that has the Foundation trait enabled.Consumers that do need
\.urlSessionon Android can re-introduce it downstream behind their own gate, or pullURLSessionfromFoundationNetworkingdirectly.