Skip to content

Dynamically load libjvm rather than requiring it at build time#8

Merged
ktoso merged 8 commits intoswiftlang:mainfrom
swift-android-sdk:main
Mar 11, 2026
Merged

Dynamically load libjvm rather than requiring it at build time#8
ktoso merged 8 commits intoswiftlang:mainfrom
swift-android-sdk:main

Conversation

@marcprux
Copy link
Contributor

This PR removes the build/link time requirement for libjvm and moves it into dlload/dlsym calls from JavaVirtualMachine.shared(). This greatly simplifies the Package.swift build by eliminating all the tool calls and path searching, and enables swift-java-jni-core to be a build dependency in environments that do not have any JVM installed. For example, it can be a transitive dependency for iOS libraries, where it won't actually work if you tried to use it, but it will build.

Since <jni.h> is no longer needed, we declare a minimal subset of the function table and types described in the JNI specification right into CSwiftJavaJNI.h. The only symbols we need to dynamically load and invoke are JNI_GetCreatedJavaVMs and JNI_CreateJavaVM; everything else falls out of the function tables.

Tested on macOS (with swift test) and an Android 28 emulator (with skip android test).

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

That's an interesting idea, I think this might work? Thanks for looking into it Marc

@ktoso
Copy link
Collaborator

ktoso commented Mar 11, 2026

Refs #3

@ktoso
Copy link
Collaborator

ktoso commented Mar 11, 2026

I think we're happy with this, as this is derived from the spec and we don't copy the actual jni.h anywhere this makes sense we think. Thanks for pouring in the work and idea @marcprux !

@ktoso ktoso merged commit 9ba3255 into swiftlang:main Mar 11, 2026
51 checks passed
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