Skip to content

Commit 5fbb597

Browse files
committed
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
1 parent 3388efd commit 5fbb597

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Sources/JavaKit/JavaObjectHolder.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ import JavaRuntime
1919
/// while this instance is live.
2020
public class JavaObjectHolder {
2121
public private(set) var object: jobject?
22+
#if canImport(Android)
23+
public var environment: JNIEnvironment {
24+
try! JavaVirtualMachine.shared().environment()
25+
}
26+
#else
2227
public let environment: JNIEnvironment
28+
#endif
2329

2430
/// Take a reference to a Java object and promote it to a global reference
2531
/// so that the Java virtual machine will not garbage-collect it.
2632
public init(object: jobject, environment: JNIEnvironment) {
2733
self.object = environment.interface.NewGlobalRef(environment, object)
34+
#if !canImport(Android)
2835
self.environment = environment
36+
#endif
2937
}
3038

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

0 commit comments

Comments
 (0)