Skip to content

Commit

Permalink
JavaKit: always use ambient javaEnvironment on Android
Browse files Browse the repository at this point in the history
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: swiftlang#157
  • Loading branch information
lhoward committed Nov 8, 2024
1 parent bcd90a3 commit 5b514d7
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/JavaKit/JavaObjectHolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public class JavaObjectHolder {
/// so that the Java virtual machine will not garbage-collect it.
public init(object: jobject, environment: JNIEnvironment) {
self.object = environment.interface.NewGlobalRef(environment, object)
#if canImport(Android)
self.environment = try! JavaVirtualMachine.shared().environment()
#else
self.environment = environment
#endif
}

/// Forget this Java object, meaning that it is no longer used from anywhere
Expand Down

0 comments on commit 5b514d7

Please sign in to comment.