Is there a way to execute a `main()` function, loc...
# multiplatform
e
Is there a way to execute a
main()
function, located in a file inside the common module, in the IDE?
s
I’m assuming the gutter icon doesn’t work for you?
e
Nope!
s
Wanna try adding a custom Gradle task as a run configuration? That's how I run my tests from the IDE.
e
I'm actually not sure if there's a Gradle task that gets generated if I add a main method to a Kotlin file
Kinda wonder how it works on JVM, feels like an IDE-specific thing
s
Whoops right gradle task
e
Looks like creating a run configuration manually worked. Need to reference the main class as it's declared in the common module, but pick
jvmMain
in
Use classpath of module
s
awesome!
👍 1
r
I’m actually not sure if there’s a Gradle task that gets generated if I add a main method to a Kotlin file
There isn’t one automatically, but you can configure one if you want. On JVM you can use the
application
plugin like you would for non-MPP. On JS the
browser()
and
nodejs()
configurations add run tasks. On native you can do
binaries { executable { ... } }
and point it at your main function.
e
Nice, good to know! I was basically looking for a way to run common code in the simplest way possible on any platform, would be great if there was a more seamless way to create a run configuration in the IDE.
j
It should be possible to do that in JVM target code in multiplatform module (without
application
plugin). I've done that for example in https://github.com/joreilly/PeopleInSpace/blob/master/common/src/jvmMain/kotlin/com/surrus/Main.kt . With that I see run icon to left of
main
and am able to just run that then in IDE.
(I'm using AS 4.1 fwiw)
e
Right, that works! It's just that I only had common code and that would've required me to create a file in
jvmMain
for the sole reason of executing the common code with the Run button.
As I mentioned though, you can invoke a
main()
method inside the common module using the gutter icon, you just need to manually tweak the run configuration.
r
I've had mixed results with the gutter icon but good that you figured something out
On the bright side though it's working super well for tests now in IDEA on everything except Android
👍 1