so this works fine ```plugins { kotlin("jvm") ...
# getting-started
r
so this works fine
Copy code
plugins {
    kotlin("jvm") version "1.9.23"
    application
}
repositories {
    mavenCentral()
}
application {
    mainClass.set("MainKt")
}
sourceSets {
    main {
        kotlin.srcDirs("src/")
    }
}
But then when I try changing jvm to multiplatform
Copy code
plugins {
    kotlin("multiplatform") version "1.9.23"
    application
I get the following error
Copy code
FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* Where:
Build file '/workspaces/testkotlin/build.gradle.kts' line: 13

* What went wrong:
Script compilation error:

  Line 13:         kotlin.srcDirs("src/")
                          ^ Unresolved reference: srcDirs
Why does it differ here? it's as if the syntax for the gradle build file changes based on the plugin.
r
It does. The plugins block is compiled first, and then the rest of the file is compiled in the context of the applied plugins, so the methods available will change based on which plugins you are using.
r
how do I go about this then?
its hard that there is zero intellisense support to tell me if I am typing a correct configuration field
the docs say sourceSets is an option
if I have the goal of "i want to set the source code directory to src/main.kt", it seems not easy to figure out what the way to do that is in gradle
r
I don't know, I've never used multiplatform. Unfortunately Gradle DSLs tend to be pretty bad at exploration via the compiler, you need either a deep understanding of the Gradle model and / or the particular plugins model, documentation, or examples when you go off piste. Generally easier not to go off piste unless it's very necessary.
r
so there is a default gradle language I can use
as opposed to kts?
r
No, kts is the default now. By going off piste I meant changing the project structure in the way you are trying. There won't be many examples of people doing that, so you'll have to find documentation on the multiplatform plugin or be prepared to build up a decent mental model of how it interacts with gradle to work it out yourself, which may be a bit slow and frustrating.
v
To get the IntelliSense you are after, you have to successfully sync the project. So comment out the things that refuse to compile in the build script, then sync. After that you have full IntelliSense power, which is one of the great advantages of the Kotlin DSL opposed to the old Groovy DSL.
What you need with KMP should be something like
Copy code
kotlin {
    jvm()
    sourceSets {
        jvmMain {
            kotlin.srcDir("src")
        }
    }
}
r
@Vampire so this is my config now
Copy code
plugins {
    kotlin("multiplatform") version "1.9.23"
    application
}
repositories {
    mavenCentral()
}
application {
    mainClass.set("MainKt")
}
kotlin {
    jvm()
    sourceSets {
        jvmMain {
            kotlin.srcDir("src")
        }
    }
}
but I am still getting an error
Copy code
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> No main class specified and classpath is not an executable jar.
my dir structure is
Copy code
├── README.md
├── build.gradle.kts
├── settings.gradle.kts
└── src
    └── main.kt
and I have a
main.kt
Copy code
fun main() {
    println("Hello")
}
v
Afair the
application
plugin and KMP plugin do not work together by default without further configuration
r
@Vampire I tried removing it before, but then I get
Copy code
FAILURE: Build failed with an exception.

* What went wrong:
Task 'run' not found in root project 'testkotlin' and its subprojects.
Copy code
plugins {
    kotlin("multiplatform") version "1.9.23"
}
repositories {
    mavenCentral()
}
kotlin {
    jvm()
    sourceSets {
        jvmMain {
            kotlin.srcDir("src")
        }
    }
}
v
Yeah, of coure, the
application
plugin adds the
run
task. If you remvoe the, plugin, you also remove the task
r
oh gotcha
@Vampire Now I'm getting
Copy code
./gradlew jsRun

FAILURE: Build failed with an exception.

* What went wrong:
Task 'jsRun' not found in root project 'testkotlin' and its subprojects. Some candidates are: 'jvmRun'.
config
Copy code
plugins {
    kotlin("multiplatform") version "1.9.23"
}
repositories {
    mavenCentral()
}
kotlin {
    js {
        browser()
    }
    jvm()
    js()
    sourceSets {
        jsMain {
            kotlin.srcDir("src")
        }
    }
}
v
Well, obviously the plugin does not add a
jsRun
task
./gradlew tasks
r
yes i did that
jsRun is not there
but the docs I am following don't say anything about adding an entry
it should just work
The Kotlin/JS plugin provides a
jsRun
task that lets you run pure Kotlin/JS projects without additional configuration.
v
But you are not using the Kotlin/JS plugin
You are using the Kotlin Multiplatform plugin
r
image.png
v
Like you in the beginning of this thread use the Kotlin/JVM plugin
r
the kotlin docs for js say to do multiplatform...?
v
And then changed to the Kotlin Multiplatform plugin and wondered the config options changed
Yes, the Kotlin/JS plugin is discontinued and you should use the KMP plugin with JS target
r
this same page says to do jsRun
image.png
v
Maybe it is outdated, the fate of any documentation. 🙂
r
last updated feb 12th 2024
v
Doesn't mean the content is not outdated
r
in any case, do you know how to run it then?
v
Nope, sorry, never used it
But the docs tell you
image.png
a
Try adding an execution environment (browser or node) https://kotlinlang.org/docs/js-project-setup.html#execution-environments
v
No executable JS file => nothing to execute
If you add that, you get various jsBrowswer.... tasks
r
thanks a ton
👌 1