https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

Shubham Singh

03/05/2023, 8:11 AM
Hey everyone, how can we build native WIndows Apps with the help of Kotlin Multiplatform? I know we generally use Compose Desktop for the UI but that is still a JVM application, right? Isn't it different from building native Windows apps? And if it is possible, is there a UI framework for that? I'm expecting to use frameworks like WinUI3 but afaik, it's written in C#
j

Johann Pardanaud

03/05/2023, 10:05 AM
I don’t know anything about calling C# APIs from Kotlin, however you can perfectly write your business code in Kotlin, build it as a DLL, build your UI using C# with the framework you want, then use the DLL from the C# code.
v

vbsteven

03/05/2023, 10:54 AM
If you want Kotlin without the JVM you will likely end up using Kotlin Native. It has some tools like
cinterop
that can be used to call native API's from Kotlin. Right now it only supports native libraries that expose C headers so your options for available platform frameworks are limited.
Me and some others are working on making GTK work for Kotlin Native, while that isn't exactly a "windows native" GUI toolkit, I am very hopeful for the project and I think we will have a good Kotlin Native GUI solution for Linux/Windows/MacOS in the near future.
K 1
s

Shubham Singh

03/05/2023, 2:09 PM
That would be awesome to see! Thank you for working on it.. very hopeful for it 🤞K
s

SrSouza

03/05/2023, 5:09 PM
What do you want to achieve here? Do you want to use Windows only APIs in your application? Compose Desktop can generate pretty good installer for Windows, Mac and Linux. The JVM is built in this installer, the user don’t even know that the API is JVM based. This means that for example, the user downloading your application does not need to have JVM installed in the machine… For windows only APIs, there is Open Source libraries, JNI, JNA.
v

vbsteven

03/05/2023, 5:42 PM
I'm not OP but I can see use cases for Kotlin/Native applications that don't require a JVM to run. Why would I bring in a full (or stripped down) JVM if most of the application code is Kotlin/Native and all the JVM does is provide a bridging layer to other native platform API's through JNI/JNA?
s

Shubham Singh

03/05/2023, 5:45 PM
To be fair, this wasn't a practical question from my end. I was just curious to know that since KMP theoretically supports so many platforms on a native level, do we also have any arrangements to write their Native UI as well? I agree that JVM can produce very good output, but from what I understand, KMP's base idea was to NOT share the UI layer but everything else besides it (and we're essentially doing that only by using Compose for everything, this is exactly what Flutter is doing as well). And in order to get the native look and feel, it's generally the best idea to use the native framework only. That's why I was wondering if we could somehow achieve interop between Kotlin and C# Windows Application code?
v

vbsteven

03/05/2023, 6:05 PM
It looks like there are some solutions to export methods from a C# codebase as C functions. So in theory you could then use cinterop to call this C function from KN. https://stackoverflow.com/a/6795403
so if the native API's you want are available in C#, you could write a small C# shim that exports to C. It will probably be a ton of work if you have a big API surface to cover.
s

Shubham Singh

03/05/2023, 6:25 PM
That seems fair for now, will have a look at it. Thanks to everyone who commented on this thread and gave their inputs 🙌
440 Views