Skip to content

Commit ba56de0

Browse files
committed
JavaKit: support for Android NDK JVM
1 parent 99fb077 commit ba56de0

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ let package = Package(
245245
.when(platforms: [.windows])),
246246
.linkedLibrary(
247247
"jvm",
248-
.when(platforms: [.linux, .macOS, .windows])
249-
),
248+
.when(platforms: [.iOS, .macOS, .tvOS, .watchOS, .macCatalyst, .linux, .openbsd, .wasi, .windows])),
250249
]
251250
),
252251
.target(

Samples/JavaKitSampleApp/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func findJavaHome() -> String {
3030
}
3131
let javaHome = findJavaHome()
3232

33-
let javaIncludePath = "\(javaHome)/include"
33+
let javaIncludePath = ProcessInfo.processInfo.environment["JAVA_INCLUDE_PATH"] ?? "\(javaHome)/include"
3434
#if os(Linux)
3535
let javaPlatformIncludePath = "\(javaIncludePath)/linux"
3636
#elseif os(macOS)

Samples/JavaKitSampleApp/Sources/JavaKitExample/JavaKitExample.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ extension HelloSwift: HelloSwiftNativeMethods {
2727
let answer = self.sayHelloBack(i + j)
2828
print("Swift got back \(answer) from Java")
2929

30+
#if !canImport(Android)
31+
// no class variables in Kotlin
3032
print("We expect the above value to be the initial value, \(self.javaClass.initialValue)")
33+
#endif
3134

3235
print("Updating Java field value to something different")
3336
self.value = 2.71828

Sources/ExampleSwiftLibrary/MySwiftLibrary.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import Glibc
2323
import CRT
2424
#elseif canImport(Darwin)
2525
import Darwin.C
26+
#elseif canImport(Android)
27+
import Android
2628
#endif
2729

2830
public func helloWorld() {

Sources/JavaKit/JavaEnvironment.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
import JavaRuntime
1616

17+
#if canImport(Android)
18+
typealias JNINativeInterface_ = JNINativeInterface
19+
#endif
20+
1721
extension UnsafeMutablePointer<JNIEnv?> {
1822
var interface: JNINativeInterface_ { self.pointee!.pointee }
1923
}

Sources/JavaKit/JavaKitVM/JavaVirtualMachine.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import Foundation
1919
#endif
2020

2121
public typealias JavaVMPointer = UnsafeMutablePointer<JavaVM?>
22+
#if canImport(Android)
23+
typealias JNIEnvPointer = UnsafeMutablePointer<JNIEnv?>
24+
#else
25+
typealias JNIEnvPointer = UnsafeMutableRawPointer
26+
#endif
2227

2328
public final class JavaVirtualMachine: @unchecked Sendable {
2429
/// The JNI version that we depend on.
@@ -61,7 +66,7 @@ public final class JavaVirtualMachine: @unchecked Sendable {
6166
) throws {
6267
self.classpath = classpath
6368
var jvm: JavaVMPointer? = nil
64-
var environment: UnsafeMutableRawPointer? = nil
69+
var environment: JNIEnvPointer? = nil
6570
var vmArgs = JavaVMInitArgs()
6671
vmArgs.version = JavaVirtualMachine.jniVersion
6772
vmArgs.ignoreUnrecognized = jboolean(ignoreUnrecognized ? JNI_TRUE : JNI_FALSE)
@@ -161,12 +166,18 @@ extension JavaVirtualMachine {
161166
return environment.assumingMemoryBound(to: JNIEnv?.self)
162167
}
163168

169+
#if canImport(Android)
170+
var jniEnv = environment?.assumingMemoryBound(to: JNIEnv?.self)
171+
#else
172+
var jniEnv = environment
173+
#endif
174+
164175
// Attach the current thread to the JVM.
165176
let attachResult: jint
166177
if asDaemon {
167-
attachResult = jvm.pointee!.pointee.AttachCurrentThreadAsDaemon(jvm, &environment, nil)
178+
attachResult = jvm.pointee!.pointee.AttachCurrentThreadAsDaemon(jvm, &jniEnv, nil)
168179
} else {
169-
attachResult = jvm.pointee!.pointee.AttachCurrentThread(jvm, &environment, nil)
180+
attachResult = jvm.pointee!.pointee.AttachCurrentThread(jvm, &jniEnv, nil)
170181
}
171182

172183
// If we failed to attach, report that.
@@ -175,9 +186,13 @@ extension JavaVirtualMachine {
175186
throw attachError
176187
}
177188

178-
JavaVirtualMachine.destroyTLS.set(environment!)
189+
JavaVirtualMachine.destroyTLS.set(jniEnv!)
179190

180-
return environment!.assumingMemoryBound(to: JNIEnv?.self)
191+
#if canImport(Android)
192+
return jniEnv!
193+
#else
194+
return jniEnv!.assumingMemoryBound(to: JNIEnv?.self)
195+
#endif
181196
}
182197

183198
/// Detach the current thread from the Java Virtual Machine. All Java

0 commit comments

Comments
 (0)