Swift-Java Integration

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In traditional JNI development, you’d write complex bridge code to connect Java and native libraries. swift-java eliminates this tedious work by automatically generating Java wrappers and JNI bridges from your Swift code.

What is swift-java?

swift-java is an official Swift project that provides automatic Java interoperability. It consists of:

Mejilumod Zahup QufcZelaxagef.luqa (bfondeh) SoxxGunubuxuw+LwiblNufo.ypafz (SJA kvecfi) DEccfamwGjidkVdixes (jpozy & cumoceduf) (aobawisel) Yeor Qbizs Haza BiscHazalojil.dxogz (seo qvema)
Gzibk-Gofo kagu sakowiyuuf slup

Installing Java Development Kits

swift-java requires specific Java versions for different purposes:

Install sdkman

sdkman is a Java version manager that makes it easy to install and switch between JDK versions.

Install Both JDK Versions

Configuring Package.swift for swift-java

Now you’ll update your Package.swift to integrate swift-java. This involves adding dependencies, configuring JNI headers, and enabling JExtractSwiftPlugin.

Configuring JExtractSwiftPlugin

JExtractSwiftPlugin needs to know what Java package to use for generated classes. In the Project pane of Android Studio navigate to taskmanager-lib/Sources/TaskManagerKit. Now create swift-java.config with this content:

How JExtractSwiftPlugin Works

When you build your Swift code, here’s the complete flow:

Ccig 3: Qexevocas qafex XerrRuyofunav.ruli (byodnow) FejcLodajoloq+NvapcVage.nkakv (LSO) Rkaj 2: Pveqix xfanx sigrog cuwwzuetp/fkudlos roydm: xonegogoWedri Tfot 9: Boo pzeru Znafh PimsRiforicap.klugh kaceceniHupwu(Bnmupv)
DUklkuhpKmiplXcaxox qworukj

Writing Swift Code

Now you’ll write the Swift validation logic. JExtractSwiftPlugin will automatically make it callable from Kotlin.

Creating the Task Data Model

Now you’ll create the Task data model in Swift. This demonstrates one of Swift’s key advantages: Value types with strong type safety.

Why Create Models in Swift?

You might wonder why you should define Task and Priority in Swift when you already have them in Kotlin. Here’s why:

Create Task.swift

Create a new file taskmanager-lib/Sources/TaskManagerKit/Task.swift:

Creating TaskManager

Now you’ll create a manager class that handles task operations and uses your validator.

Building and Viewing Generated Code

Now build your Swift package to trigger JExtractSwiftPlugin:

Check Generated Java Code

After the build, JExtractSwiftPlugin generates Java wrapper classes in this directory:

TaskValidator.java

Open TaskValidator.java in Android Studio. The generated file contains JNI wrapper code - here are the key parts:

Task.java and Priority.java

Open Task.java in Android Studio. The file is quite long, but here are the key parts showing how Swift structs map to Java classes:

TaskManager.java

Open TaskManager.java in Android Studio. Here’s a simplified view of the key methods:

Check Generated Swift JNI Bridge

JExtractSwiftPlugin also generated Swift code that handles the JNI calls. These are located in:

Understanding the Generated Code Flow

When Kotlin calls TaskValidator.validateTitle("Hi"), here’s what happens:

8. Xgimw pozxduap CafzZokeweluk.ddufm fuwisavoVanda(_:) cijomdp Viim 9. CZE hraske + SwiynBule.bduqg yiwczoqt Xshuzk yosorym jbaixood 8. Qumo kefeco xopper TavgCudomugiq.tuni $toxixaheNehsu(nebuzi) 7. Pipwes wakph YarsPutewijuvp.wp gaqofekuQirro(‘Ge’)
Fokdgeqo ledi cgax cpos Vahjev pe Mbetx

What You’ve Accomplished

In this segment, you’ve:

See forum comments
Download course materials from Github
Previous: Environment & Swift Package Setup Next: Kotlin Integration & Gradle Build Automation