GithubHelp home page GithubHelp logo

Comments (6)

zhuowei avatar zhuowei commented on August 12, 2024

Currently @ephemer is working on https://github.com/SwiftAndroid/swift-jni, which allows access to Java JNI. Then, one can write wrapper functions that convert JNI arguments to/from native Swift datatypes, and use them as native methods from Java. A very simple proof of concept can be found at https://github.com/SwiftAndroid/swift-android-samples/blob/master/swifthello/src/main/swift/Sources/main.swift

I would suggest that you also look at other projects for Swift on Android if you don't need full compatibility with the reference toolchain, such as the (more mature) RemObjects Silver.

from swift.

avaidyam avatar avaidyam commented on August 12, 2024

The problem is that I do need compatibility with the reference toolchain. I'll take a look at the other projects; thanks for the info!

from swift.

ephemer avatar ephemer commented on August 12, 2024

What do you mean exactly by compatibility?

@zhuowei has made a gradle plugin that will compile your swift code alongside other native code. Personally I've just used swiftc-android to build shared libraries which can be loaded / linked against by any other native come you compile (regardless of toolchain)

Either way you'll very likely have to deal with the JNI/NDK, unless you use RemObjects Silver or the like (which may or may not have its own issues- I don't know how it works under the hood, I did read some question marks re: Arrays being passed by reference rather than by value, but I suspect the actual issues there will be more along the lines of interfacing with other native / Java code down the line)

from swift.

avaidyam avatar avaidyam commented on August 12, 2024

@ephemer @zhuowei So if I have a pure Swift project, understandably I would end up dealing with the JNI or NDK (which is C++, so how is Swift doing that?). Is there a way for me to expose all the library functions/classes/structs to the Java side of things and call them directly?

from swift.

ephemer avatar ephemer commented on August 12, 2024

@avaidyam, you expose certain top-level (non-class) functions to Java, currently using some undocumented (but very easy to use) compiler features. In a nutshell, that is what the JNI does.

Maybe I'm missing something but from what I can see the JNI and NDK have C (in addition to C++) APIs, which is no problem for Swift. What I've done is wrap those C APIs to make them look closer to their C++ equivalents and more idiomatically "Swift-like" in their usage.

from swift.

avaidyam avatar avaidyam commented on August 12, 2024

@ephemer Got it, thanks! 👍

from swift.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.