Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2025 Swift

WWDC25 · 21 min · Swift

Explore Swift and Java interoperability

Learn how you can mix Swift and Java in a single codebase. We’ll introduce the swift-java interoperability project, which allows you to use Swift in Java programs or vice versa. We’ll show you how to use the tools and libraries offered by swift-java to write safe and performant code that interoperates between these two runtimes.

Watch at developer.apple.com ↗

Transcript all transcripts

Chapters

Code shown on screen · 5 snippets

Implement JNI native methods in Swift swift · at 9:05 ↗
import JavaKit
import JavaRuntime

import Crypto

@JavaImplementation("com.example.JNIExample")
extension JNIExample: JNIExampleNativeMethods {
 
  @JavaMethod
  func compute(_ a: JavaInteger?, _ b: JavaInteger?) -> [UInt8] {
    guard let a else { fatalError("Expected non-null parameter 'a'") }
    guard let a else { fatalError("Expected non-null parameter 'b'") }
    
    let digest = SHA256Digest([a.intValue(), b.intValue()]) // convenience init defined elsewhere
    return digest.toArray()
  }
}
Resolve Java dependencies with swift-java swift · at 12:30 ↗
swift-java resolve --module-name JavaApacheCommonsCSV
Use a Java library from Swift swift · at 13:05 ↗
import JavaKit
import JavaKitIO
import JavaApacheCommonsCSV

let jvm = try JavaVirtualMachine.shared()

let reader = FileReader("sample.csv") // java.io.StringReader

for record in try JavaClass<CSVFormat>().RFC4180.parse(reader)!.getRecords()! {
  for field in record.toList()! {      // Field: hello
    print("Field: \(field)")           // Field: example
  }                                    // Field: csv
}

print("Done.")
Wrap Swift types for Java swift · at 16:22 ↗
swift-java --input-swift Sources/SwiftyBusiness \ 
           --java-package com.example.business \
           --output-swift .build/.../outputs/SwiftyBusiness \
           --output-java .build/.../outputs/Java ...
Create Swift objects from Java csharp · at 18:55 ↗
try (var arena = SwiftArena.ofConfined()) {
  var business = new SwiftyBusiness(..., arena);
}

Resources