diff --git a/Sources/JavaKit/JavaObjectHolder.swift b/Sources/JavaKit/JavaObjectHolder.swift index 173991c9..75adbfed 100644 --- a/Sources/JavaKit/JavaObjectHolder.swift +++ b/Sources/JavaKit/JavaObjectHolder.swift @@ -19,13 +19,21 @@ import JavaRuntime /// while this instance is live. public class JavaObjectHolder { public private(set) var object: jobject? +#if canImport(Android) + public var environment: JNIEnvironment { + try! JavaVirtualMachine.shared().environment() + } +#else public let environment: JNIEnvironment +#endif /// Take a reference to a Java object and promote it to a global reference /// 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 = environment +#endif } /// Forget this Java object, meaning that it is no longer used from anywhere