Skip to content

Don't link jvm on Android #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
Merged

Conversation

colemancda
Copy link
Contributor

Android doesn't have an equivalent jvm so this library should not be linked for that platform.

Package.swift Outdated
@@ -209,8 +209,7 @@ let package = Package(
.when(platforms: [.windows])),
.linkedLibrary(
"jvm",
.when(platforms: [.linux, .macOS, .windows])
),
.when(platforms: [.iOS, .macOS, .tvOS, .watchOS, .macCatalyst, .linux, .openbsd, .wasi, .windows])),
Copy link
Collaborator

Choose a reason for hiding this comment

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

We’re not really promising compatibility with a bunch of those yet

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd suggest removing: wasi, wasm, catalyst, ios, tvos, watchos -- it's not platforms we support right now

Suggested change
.when(platforms: [.iOS, .macOS, .tvOS, .watchOS, .macCatalyst, .linux, .openbsd, .wasi, .windows])),
.when(platforms: [.macOS, .linux,])),

wdyt?

@@ -30,7 +30,7 @@ func findJavaHome() -> String {
}
let javaHome = findJavaHome()

let javaIncludePath = "\(javaHome)/include"
let javaIncludePath = ProcessInfo.processInfo.environment["JAVA_INCLUDE_PATH"] ?? "\(javaHome)/include"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Worth calling out in the readme? What do the paths look like for android?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lhoward I cherry picked your commit, can you comment please?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a while since I looked at this. JAVA_INCLUDE_PATH is something I used in swift-build-android.sh, I'm not sure if it's a standard environment variable or not.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably not standard but my question was more about "why do we need this" / "why are the paths different and how do they look like" and just adding a note to the README about how to use this

Copy link
Contributor

Choose a reason for hiding this comment

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

We need to pick up the host JVM when building Java2Swift and JavaCompilerPlugin, vs the Android JVM when building JavaKitExample. JAVA_HOME and JAVA_INCLUDE_PATH serve to disambiguate between the two.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good, can we plop such a note in to the readme somewhere along with this pr please?

@@ -27,7 +27,10 @@ extension HelloSwift: HelloSwiftNativeMethods {
let answer = self.sayHelloBack(i + j)
print("Swift got back \(answer) from Java")

#if !canImport(Android)
// no class variables in Kotlin
Copy link
Collaborator

Choose a reason for hiding this comment

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

But Android != Kotlin, there's Java there too right? the comment strikes me as bit weird, is this really necessary?

Copy link
Contributor

Choose a reason for hiding this comment

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

The sample Android app is written in Kotlin. I do not have the cycles to rewrite it in Java.

Copy link
Collaborator

@ktoso ktoso Jun 9, 2025

Choose a reason for hiding this comment

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

Maybe I'm confused, this is the JavaKitSampleApp, why does what the android sample app matter to this example project?

public let environment: JNIEnvironment
#endif

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about such a semantic change based on platform... can would this actually do the right thing or lead to unexpected issues sometimes?

Copy link
Contributor

Choose a reason for hiding this comment

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

Please see the comment in commit a2ba0e16d6255abb62598910151056f54c92340e.

Copy link
Contributor

Choose a reason for hiding this comment

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

    JavaKit: always use ambient javaEnvironment on Android
    
    This is not a real fix, I suspect the real fix is to remove javaEnvironment as
    an instance variable on all platforms and always use the ambient environment
    (as the JNI specification clearly states the environment cannot be shared
    between threads).
    
    Works around: #157

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok got it, thanks!

@ktoso
Copy link
Collaborator

ktoso commented Jun 9, 2025

Added some more questions, with the C++ file I'm ok with I guess.

We should think about Android CI eventually...

return (*JavaRuntime_GetCreatedJavaVMs)(vmBuf, bufLen, nVMs);
}

#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

This code needs to be removed and replaced with targeting a NDK version which exports the JNI.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be fairly trivial to do with a current Swift Android SDK, I just haven't had time to do it.

@ktoso
Copy link
Collaborator

ktoso commented Jun 9, 2025

@colemancda can we step back a little and clarify the PR a bit? It says we "don't link jvm on android" which is fine, and I'd be happy to merge that, but then there's also a bunch of picked fly-by changes that don't seem fully fleshed out.

How about we make this PR the minimum change to not link the jvm and the rest let's do in separate follow ups?

@ktoso ktoso added the android label Jun 9, 2025
@lhoward
Copy link
Contributor

lhoward commented Jun 9, 2025

@colemancda can we step back a little and clarify the PR a bit? It says we "don't link jvm on android" which is fine, and I'd be happy to merge that, but then there's also a bunch of picked fly-by changes that don't seem fully fleshed out.

How about we make this PR the minimum change to not link the jvm and the rest let's do in separate follow ups?

I'm confused how these relate to the Android PRs I filed, if at all?

@ktoso
Copy link
Collaborator

ktoso commented Jun 9, 2025

Bottom line is: I don't know what to do about this PR specifically because it picks a bunch of stuff I'm not entirely clear about. It doesn't seem like something we should accept given the bunch of comments on the commits other than the first one.

Should we just make this PR the first commit and the others later on?

@colemancda
Copy link
Contributor Author

@ktoso I force pushed so the PR is only 1 commit. I will clean up the other work and open a separate PR.

@ktoso
Copy link
Collaborator

ktoso commented Jun 9, 2025

Sounds good, thank you! We'll also work towards getting Android CI in the near future but it depends on Swift getting official android support on swift.org AFAICS. Thanks for your work!

More than happy to keep accepting Android PRs but I may be missing some context since I've not looked at it much yet, thank you in advance!

@ktoso ktoso merged commit 6e9d7da into swiftlang:main Jun 9, 2025
24 checks passed
@colemancda colemancda deleted the feature/android branch June 9, 2025 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants