Skip to content

Skip URLSession on Android to drop libFoundationNetworking from DT_NEEDED#438

Merged
mbrandonw merged 4 commits into
pointfreeco:mainfrom
mansbernhardt:android-support
May 12, 2026
Merged

Skip URLSession on Android to drop libFoundationNetworking from DT_NEEDED#438
mbrandonw merged 4 commits into
pointfreeco:mainfrom
mansbernhardt:android-support

Conversation

@mansbernhardt
Copy link
Copy Markdown
Contributor

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.

…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.
@mbrandonw
Copy link
Copy Markdown
Member

Hi @mansbernhardt, thanks for investigating this. I think a better change would be to introduce a dedicated FoundationNetworking trait for this instead of including URLSession with just the Foundation trait. In order to make it backwards compatible you could include URLSession when FoundationNetworking is enabled or when on Apple platforms. Can you give that a shot?

@mansbernhardt
Copy link
Copy Markdown
Contributor Author

Sounds good, I have pushed an update adding a FoundationNetworking trait.

Comment thread Sources/Dependencies/DependencyValues/URLSession.swift Outdated
Comment thread Package.swift Outdated
Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com>
Copy link
Copy Markdown
Member

@mbrandonw mbrandonw left a comment

Choose a reason for hiding this comment

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

Thanks!

Comment thread Sources/Dependencies/DependencyValues/URLSession.swift Outdated
Copy link
Copy Markdown
Member

@mbrandonw mbrandonw left a comment

Choose a reason for hiding this comment

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

Thanks!

@mbrandonw mbrandonw merged commit 9a6017a into pointfreeco:main May 12, 2026
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.

2 participants