Call Java library from Kotlin Native

161 views Asked by At

I am using Kotlin Native to create code that will run on two separate operating systems (MacOS and Android). Both machines will have JVMs installed.

I have a Java library that cannot be rewritten in Kotlin, as it relies on an open-souce third party Java dependency -- specifically WireMock but I don't think that's relevant to answer the broader question.

Are there options for Java interop beyond invoking a standalone JAR from the Native shell/terminal? Is there something that can be achieved via the JNI to allow for programatic invocation of the JAR from Kotlin Native?

1

There are 1 answers

0
Evgeny K On

The answer is yes, you can. But I would never recommend this. For MacOS and Android targets it will be better use JVM target for Kotlin instead of Kotlin/Native. Performance and developer experience will be much better this way.

If you really want to use Kotlin/Native you need first compile your java library using Java AOT for every native target, and then use Kotlin/Native cinterop.

JNI is completely opposite technology, it allows to use native libraries in JVM, not other way around.