https://kotlinlang.org logo
#getting-started
Title
# getting-started
r

Ray Rahke

03/15/2024, 10:29 AM
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

Rob Elliot

03/15/2024, 10:31 AM
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

Ray Rahke

03/15/2024, 10:32 AM
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

Rob Elliot

03/15/2024, 10:37 AM
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

Ray Rahke

03/15/2024, 10:37 AM
so there is a default gradle language I can use
as opposed to kts?
r

Rob Elliot

03/15/2024, 10:41 AM
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

Vampire

03/15/2024, 10:45 AM
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

Ray Rahke

03/15/2024, 10:57 AM
@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

Vampire

03/15/2024, 10:58 AM
Afair the
application
plugin and KMP plugin do not work together by default without further configuration
r

Ray Rahke

03/15/2024, 11:00 AM
@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

Vampire

03/15/2024, 11:01 AM
Yeah, of coure, the
application
plugin adds the
run
task. If you remvoe the, plugin, you also remove the task
r

Ray Rahke

03/15/2024, 11:01 AM
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

Vampire

03/15/2024, 11:06 AM
Well, obviously the plugin does not add a
jsRun
task
./gradlew tasks
r

Ray Rahke

03/15/2024, 11:07 AM
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

Vampire

03/15/2024, 11:09 AM
But you are not using the Kotlin/JS plugin
You are using the Kotlin Multiplatform plugin
r

Ray Rahke

03/15/2024, 11:09 AM
image.png
v

Vampire

03/15/2024, 11:09 AM
Like you in the beginning of this thread use the Kotlin/JVM plugin
r

Ray Rahke

03/15/2024, 11:10 AM
the kotlin docs for js say to do multiplatform...?
v

Vampire

03/15/2024, 11:10 AM
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

Ray Rahke

03/15/2024, 11:10 AM
this same page says to do jsRun
image.png
v

Vampire

03/15/2024, 11:12 AM
Maybe it is outdated, the fate of any documentation. 🙂
r

Ray Rahke

03/15/2024, 11:12 AM
last updated feb 12th 2024
v

Vampire

03/15/2024, 11:13 AM
Doesn't mean the content is not outdated
r

Ray Rahke

03/15/2024, 11:13 AM
in any case, do you know how to run it then?
v

Vampire

03/15/2024, 11:13 AM
Nope, sorry, never used it
But the docs tell you
image.png
a

Adam S

03/15/2024, 11:14 AM
Try adding an execution environment (browser or node) https://kotlinlang.org/docs/js-project-setup.html#execution-environments
v

Vampire

03/15/2024, 11:15 AM
No executable JS file => nothing to execute
If you add that, you get various jsBrowswer.... tasks
r

Ray Rahke

03/15/2024, 11:19 AM
thanks a ton
👌 1
6 Views