Kotlin-dsl gradle, multi-platform project, minimal example

  1. I am aware of kotlin-dsl-samples/samples at master · gradle/kotlin-dsl-samples · GitHub and have read through the .kts files in the four multiplatform- folders .

  2. I have gradle + multiplatform kotlin working. My standard setup is:
    foobar-common/build.gradle
    foobar-jvm/build.gradle (expectedBy(“:foobar-common”))
    foobar-js/build.grade (expectedBy(“:foobar-common”)

  3. The gradle/kotlin-dsl/samples/multiplatform-* examples confuse me, as I don’t see the X-common, X-js, X-jvm directory structure.

  4. I have tried converting my existing build.gradle to build.gradle.kts … but it’s not clear to me how various lines should be convertedc.

Question: Is there a minimal gradle kotlin-dsl example which follows the

X-common/
X-jvm/
X-js/

directory structure?

Thanks!

1 Like

They do? There aren’t any. You confused them with multi-project. As far as I can tell none of those examples have a multi-platform setup. This should not matter however.
You can follow those examples on how to setup the project structure. I guess you have to replace the plugin from kotlin("jvm") to kotlin("common"). I don’t know what exactly it is. You might need to experiment with the name a bit until you get it right. Then in the dependencies I guess you do exactly the same as you would using Groovy except with a slightly different syntax expectedBy(project(":-common"))

I am trying to get a minimal example running as well, has anybody already done this? If not, can anyone point my existing project in the right direction? You can find it here GitHub - SeanShubin/kotlin-prototype: Sample of how to create a multi module project in kotlin

I am putting together a presentation to promote Kotlin, and want to use Kotlin’s common module as the main selling point unique to Kotlin. Unfortunately, being new to both gradle and multiplatform projects, I am having a really difficult time putting a sample together. I have already tried looking at KotlinAcademyApp for reference, but that one is way more complex than I need and is in fact so complex I have been unable to even get it to work with IntelliJ IDEA. kotlin-fullstack-sample is not workable either because it does not demonstrate testing.

You can find my (not working yet) sample project here:

Any help getting this up and running with the minimal possible configuration would be appreciated, I think it would benefit the Kotlin community to have a starting point like this to demonstrate the usefulness of common modules.

Sean

Your missing the expectedBy part in the dependencies of js and jvm.
Take another look at the docs: https://kotlinlang.org/docs/reference/multiplatform.html

Also testing should be pretty simple. It works the same way it does with any kotlin gradle project. There is nothing special to do, except adding the right dependencies.

Your suggestion to take another look at https://kotlinlang.org/docs/reference/multiplatform.html helped me get the common module to build. I have updated the project and added the expectBy clauses as you suggest. Now I am trying to figure out how to invoke a common module from a regular module.

I am not sure if expectedBy applies in my case. I am not specifying that my JS and JVM modules provide the “actual” implementations of the “expect” declarations in common code. I am trying to get both JS and JVM regular modules to depend on code in the common module.

What I am envisioning is a large amount of common modules for my domain and libraries, depended on a few regular modules that handle launching the application and platform specific details. For this I expect I will not need platform modules at all, only regular modules and common modules.

Perhaps this is not the original intent, but I think it would be really useful and a great selling point for Kotlin.

Just need to get the JavaScript tests to actually run.

I do indeed need the expectedBy. I am really close now. The JVM tests are working fine, properly delegating to common code. I did the same thing with the javascript module but it seems the test simply do not run when I type “gradle test”. Anyone know what I am missing? It is probably something really simple that I am just not seeing.

You can mute this topic by clicking on the “Tracking” button at the bottom, or by clicking on the blue icon below the right-hand side scroll bar.

Sure, I thought my question was a duplicate (minimal, multi-platform project) so did not want to spam with a new topic, but am happy to post as a separate thread as well.

The current (Dec 2018) multiplatform setup states:

Gradle Kotlin DSL support has not been implemented yet for multiplatform projects, it will be added in the future updates. Please use the Groovy DSL in the build scripts.

So I assume no minimal example of Kotlin-dsl gradle multi-platform would exist at this time.

Assuming this is still true, where should people be checking to see when kotlin-dsl support for multi-platform will be (again?) available?

It will be available (with the updated documentation) in the upcoming 1.3.20 release: https://youtrack.jetbrains.com/issue/KT-26389 . You can try it right now in the 1.3.20 EAP release (“1.3.20-eap-25” version of the plugin).

It would be much easier to try, and then give feedback if that is desired, if there was an example or something enabling discovering the syntax (I have tried guessing, but without success)

https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-native-binaries/kotlin-dsl/build.gradle.kts

1 Like

In an earlier example, the targets entry was created through code like this:

targets.withGroovyBuilder {
"fromPreset"(presets["jvm"], "jvm")
"fromPreset"(presets["js"], "js")
}

If that approach is used then sourceSets["jvmMain"].apply can be used in place of sourceSets.create() , but I see that the fromPresent can be omitted using the sourceSets.create() function. Is any thing lost by not using the fromPreset() approach, that cannot be added in other ways, and if so any idea how to add what is missed?

From preset does also set other data in the target which is not set by the create operation, but possibly can be manually applied in the apply block. Without ‘from preset’ , i cannot get the data for uploading the library to maven central. (this also requires a ‘pom’ block and this post may help with the pom ).

So more data about other settings for targets would be appreciated to allow not just building a multi-platform library, but also to allow publishing the multi-platform library.

Almost working sample here:
https://github.com/msink/kotlin-libui/blob/kotlin-1.3.20/libui/build.gradle.kts
Discussion in issue Publishing · Issue #2372 · JetBrains/kotlin-native · GitHub

“Almost working” I mean - working, but not entirely in Kotlin DSL.