SwiftJava / SwiftJava

Swift to Java Bridge
Other
363 stars 26 forks source link

Using Swift code in a Java project #11

Open DanielZanchi opened 2 weeks ago

DanielZanchi commented 2 weeks ago

Is it possible to do the opposite? Using Swift code (with libraries like StoreKit) in a Java application?

I tried creating a Swift Package but after creating the .dylib it only works when running my code from eclipse. Not running the .jar that gets created.

johnno1962 commented 1 week ago

Hi, you can call Swift from Java through "proxy classes" where the Swift has been linked into the example app as it was for the android examples otherwise you'd need to be doing dynamic loading of the Swift Code then looking up the proxy classes. You realise this isn't the Apple Swift-Java initiative? This code hasn't been looked at in 8 years.

DanielZanchi commented 1 week ago

Yes i realize. I would need to call some functions that are available in Swift from a java application that runs on desktop. I need to use StoreKit framework that is available in Swift so I was wondering if checking one of these projects could help.

johnno1962 commented 1 week ago

The components are all there but this isn't a configuration I've investigated. When you say it's a Java application that runs on the desktop it's still an executable you could link your Swift code with that you could call from your Java right? That going to be easier than dynamic loading the Swift code in. Java objects can be passed through and are represented by interface proxies. I'll be honest I'm a bit hazy on the code now but these seem to be the most relevant files https://github.com/SwiftJava/swift-android-samples/blob/master/swifthello/src/main/java/net/zhuoweizhang/swifthello/SwiftHello.java which is the java client and https://github.com/SwiftJava/swift-android-samples/blob/master/swifthello/src/main/swift/Sources/swifthello.swift which provides the native implementation using an \@_silgen_name. It dynamic loaded extensions which implement the native method declared according to Java's conventions on the symbols name. In this code, "binding" is where the Swift "listener" is passed a Java "responder" as if the boundary between the two is a network connection.