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

egorand

05/02/2020, 5:36 PM
Is there a way to execute a
main()
function, located in a file inside the common module, in the IDE?
s

saket

05/02/2020, 5:38 PM
I’m assuming the gutter icon doesn’t work for you?
e

egorand

05/02/2020, 5:39 PM
Nope!
s

saket

05/02/2020, 5:57 PM
Wanna try adding a custom Gradle task as a run configuration? That's how I run my tests from the IDE.
e

egorand

05/02/2020, 6:04 PM
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

saket

05/02/2020, 6:06 PM
Whoops right gradle task
e

egorand

05/02/2020, 6:08 PM
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

saket

05/02/2020, 6:09 PM
awesome!
👍 1
r

russhwolf

05/02/2020, 6:36 PM
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

egorand

05/02/2020, 7:25 PM
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

John O'Reilly

05/03/2020, 10:27 AM
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

egorand

05/04/2020, 5:28 PM
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

russhwolf

05/04/2020, 5:31 PM
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