Load Kotlin/Native shared library in Kotlin/JVM - kotlin

Is there any way relative simple to load Kotlin/Native dynamic library into Kotlin/JVM. Some details of my case: I have some Kotlin/JVM interface for example IGenerator:
interface IGenerator {
fun generate(): ByteArray
}
and I want to implement this interface using native code due to performance purpose. Without Kotlin/Native I would wrote a C/C++ code then compile it into shared library and build a JVM wrapper using for example SWIG (http://www.swig.org/). This tool generate JNI wrapper for my shared library and it'll be good to load into my Kotlin/JVM app.
So the question: is there a convenient way to load Kotlin/Native shared library into Kotlin/JVM application? I'd like to use any sort of Kotlin tooling without creation of separated wrappers or any other voodoo magic.

If you're using gradle, it's simple enough (:shared is Kotlin/Native Multiplatform project):
implementation project(':shared')
You can then use any K/N method written in :shared in your Kotlin/JVM project.

Related

What does "common" mean in kotlin documentation?

In kotlin standard library documentation i can see the following filters:
As far as i can gather, JVM means that internals of a package can be compiled into byte code, JS into javaScript and Native into binaries. But what does "Common" mean ?
Common means available for all platforms. It is an API that you can use directly in platform independent code in Kotlin Multiplatform projects.
Kotlin Multiplatform allows you to write common Kotlin code for multiple different platforms. For example, you could have a project with an Android app (using Kotlin/JVM for native Android applications) and a web interface (using Kotlin/JS). Then, you could share code between both subprojects.
As you have guessed correctly, JVM means it is available using Kotlin/JVM (compiling to Java bytecode).
If it is marked with JS, it is available for Kotlin/JS which transpiles the Kotlin code to JavaScript.
Finally, Native means it is available when using Kotlin/Native. This is different from compiling with native-image, which compiles JVM bytecode to native executables. For that, you would still use Kotlin/JVM.
If you write code in "common", then it can be compiled into any of the other targets, so you can share Kotlin code amongst the different platforms you are targeting. You can read more in the documentation.

Can I build a windows executable from kotlin source code?

As far as I know, kotlin native allows you to compile kotlin source code to platform specific nativ code, that runs without any virtual machine.
But I don't seem to find any example on how to build a windows executable (.exe) from kotlin source code.
Why is that?
Can you do it or not?
https://kotlinlang.org/docs/native-get-started.html shows you how to set up a basic Kotlin Native project, and compile it. The only note is that kotlin native will generate a .kexe, which is just a renamed .exe.

kotlin-test vs kotlin-test-junit

I have a kotlin project that is compiled to java.
My test library is junit.
Im using maven as my dependency management and Intellij IDEA.
recently I got that strange warning in my pom.
Inspection info: If you have kotlin-test and junit dependency then
most likely you better to include kotlin-test-junit instead of just
kotlin-test
What is the difference between kotlin-test and kotlin-test-junit?
from what I read it seems that kotlin-test is not deprecated, so Why did intellij recommend kotlin-test-junit instead of kotlin-test?
So I found the answer here (I don't understand how I missed it in my first search)
In short kotlin-test library provides a set of annotations and utility functions independently of the test framework.
Where kotlin-test-junit library provides an implementation on top of Junit Asserter and the annotations and utility functions kotlin-test provides.

Can we develop desktop applications in kotlin?

I want to develop Desktop and Android applications. And I want to learn kotlin for this. Kotlin is mostly used for developing Android applications but I want to know that can we develop desktop applications in kotlin?
Yes, you can. Technically any program you can write in Java can also be written in Kotlin*. Of course it's even better when using frameworks that were specially tailored for Kotlin.
There are frameworks like TornadoFX built on top of JavaFX to build desktop UIs.
More recently, Compose has been generalized for Desktop apps (initially JetPack Compose for Android).
*More specifically, Kotlin/JVM compiles to Java bytecode, so you can use Kotlin to target any environment running a JVM that interprets this bytecode (provided the target bytecode version is at most that of the JVM, like in Java).
As pointed out by #Tenfour04, GWT is not in this category because it directly works on the Java source code, not bytecode.

Loading Kotlin generated classes at runtime from Java

I have 2 projects...
My main java project and a kotlin sub project that I moved some java classes out of the main project into and converted into kotlin.
I've configured the kotlin project to have a dependency on the main java project, it works quite well since IJ is constantly recompiling java classes in the main project.
But now that i've extracted classes from the Java project, it won't compile anymore of course - it needs to access the kotlin project.
However, I don't know how to do that.. can someone explain? thanks
my kotlin out folder is empty whenever I look at it
Kotlin generates perfectly ordinary Java class files, so you can use Class.forName() and Class<?>.getConstructor().getNewInstance() with them just as well as with classes compiled from Java source code.