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.
Related
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.
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.
I'm getting started with Kotlin, and on the downloads page I see two different possibilities that look relevant for me:
kotlin-compiler
kotlin-native-windows
What's the difference? In what case would I need one or the other?
Most probably you won't use CLI to compile, build and run Kotlin projects a lot. Well, maybe you'll need a standalone compiler a few times compiling "Hello world" when starting with this language. BTW, you can use https://try.kotlinlang.org to compile simple programs and play with the language without local installations.
But as you proceed, it won't be enough. Most Kotlin projects today use either Gradle or Maven. Gradle scripts could be written in Kotlin themselves. I recommend you taking this extra step and use build tools instead of standalone compiler, as it will simplify a lot if things in future. IntelliJ IDEA, the most popular IDE for Kotlin made by JetBrains, the company behind Kotlin, allows you to create Gralde-based Kotlin projects in a few clicks.
Kotlin is a multi-platform language. It can be compiled to produce binaries compatible either with:
Java
JavaScript
A native platform (Windows, Linux, iOS, etc.)
Which compiler you should choose depends on your needs. If you don't need to make your code operate with a C library or a specific OS tool, I'd recommend the kotlin-compiler, so you'll get an app executable through Java, which (at least for now) produce more optimized programs, and also easily portable (install a jre on target computer and you're ready to execute your Kotlin program). Plus, you'll be able to use any Java lib you need in your kotlin program.
Note : Official documentation contains guides to get started:
Command line compiler for JVM
Command line compiler for native executables
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.
Looking at https://github.com/JetBrains/kotlin/releases/tag/v1.3.0, there is a generic multi-platform zip file (kotlin-compiler-1.3.0.zip) and some platform-specific ones (e.g. kotlin-compiler-1.3.0-release-windows-x64.zip). Looking inside the zip files, the differences seem to be that the platform-specific installations contain some native executables (e.g. .dll or .so files) and uses its own pre-built JRE, whereas the multiplatform version seems to be pure Java.
Is this the only difference, or do the platform-specific installations contain some extra features over and above the pure Java version?
Would it be safer to use the pure Java version, since my Java 1.8u192 release is later than the one Kotlin 1.3 was built with?
The platform-specific installations are AOT-compiled using Excelsior JET. That's the only difference; there are no extra platform-specific features.