Hi everyone, In our latest 1.3.20 EAP, we have add...
# multiplatform
h
Hi everyone, In our latest 1.3.20 EAP, we have added Gradle Kotlin DSL support to the
kotlin-multiplatform
plugin, along with some DSL improvements, such as simpler targets declaration. You're welcome to try it and share your feedback! See the details in the thread.
❤️ 10
👍 3
To try this build, you need to add the Kotlin EAP repository to the
pluginManagement { ... }
section of the
settings.gradle
file:
Copy code
pluginManagement {
    repositories {
        maven { setUrl("<https://kotlin.bintray.com/kotlin-eap>") }
        gradlePluginPortal() 
	}
}
Add this repository to the build script as well and apply the plugin within the
plugins { ... }
block as you normally would, but specify the EAP version:
Copy code
plugins {
    kotlin("multiplatform") version "1.3.20-eap-25"
}
repositories {
    maven { setUrl("<https://kotlin.bintray.com/kotlin-eap>") }
}
The DSL improvements in the EAP that are available both in Groovy and Kotlin DSL are: * simpler target declaration: instead of
fromPreset(presets.<...>, '...')
, you can now use functions: for example,
jvm { ... }
or
jvm('jvm6') { ... }
or even
jvm()
, and similarly for the other platforms. This works in the whole
kotlin { ... }
scope, not only in the
targets { ... }
block. If such a target already exists, these functions return it, which can be used to access or configure an existing target:
jvm().compilations.<...>
. * an equivalent for
targets { fromPreset(...) }
in case you need to dynamically create multiple targets from several presets: `targetFromPreset(...)`; *
defaultSourceSet
and
defaultSourceSet { ... }
as a less error-prone way to configure a default source set of a compilation: `jvm().compilations["main"].defaultSourceSet { ... }`; * statically typed
kotlinOptions
,
kotlinOptions { ... }
, and
compileKotlinTask
for a `KotlinCompilation`; * shorthands for Kotlin dependencies, like
kotlin('stdlib')
instead of
'org.jetbrains.kotlin:kotlin-stdlib'
.
a
this is awesome!
t
@h0tk3y could you please update https://github.com/h0tk3y/k-new-mpp-samples, too?
s
Love the new MPP Kotlin DSL changes! Only thing I'm really missing is a function with the following signature:
Copy code
fun KotlinDependencyHandler.implementation(group: String, name: String, version: String)
h
@thevery Ok, I've added some superficial update to that repo, maybe will add Kotlin DSL samples later.
👍 1
m
What Gradle version will be used in Jetbrains libs - .coroutines, .serialization etc, 5.0? Now they use 4.7.
g
Side note,
Copy code
maven { setUrl("<https://kotlin.bintray.com/kotlin-eap>") }
Can be replaced with just
Copy code
maven("<https://kotlin.bintray.com/kotlin-eap>")
@h0tk3y What do you think about main/test static accessors, or set of type safe extensions, like:
Copy code
fun KotlinOnlyTarget<*>.dependenciesMain(block: KotlinDependencyHandler.() -> Unit) {
    compilations["main"].defaultSourceSet {
        dependencies {
            block()
        }
    }
}
Because
compilations["main"].defaultSourceSet {
is pretty wordy and you need a lot of such code
h
@gildor Yes that makes sense. Though Android doesn't have a
main
compilation, so this kind of extension won't be available for Android targets. I've created an issue for this: https://youtrack.jetbrains.com/issue/KT-28866
👍 1
p
I am attempting to convert …/k-new-mpp-samples/lib-and-app/sample-lib to use the Kotlin DSL, plus make a few updates. I get an error as shown: Screen Shot 2018-12-27 at 5.42.38 PM.png
Any clues on how to fix the remaining errors?
Script compilation errors: Line 35: nativeMain ^ Unresolved reference: nativeMain Line 37: configure([linuxMain, windowsMain, macMain]) { ^ Type mismatch: inferred type is Array<TypeVariable(T)> but (Mutable)Iterable<Any!> was expected
s
well there’s no nativeMain property, you have to do
get(“nativeMain”)
and configure in that lambda
p
| “and configure in that lambda”
@serebit Can you elaborate just a tad more, thanks.
s
Sure. Before the newest EAP, the way to access any source set was
get(“${targetName}Main”)
or
get(“${targetName}Test”
, like
windowsMain
and
linuxMain
. I wasn’t clear in my other message, sorry.
If you wanted to configure them all, I’m not sure if this would work, but try replacing
[linuxMain, windowsMain, macMain]
with
mutableListOf(get(“linuxMain”), get(“macMain”), get(“windowsMain”))
.
I don’t have my laptop with me to give it a shot myself, but I can take a look later.
p
Appreciate the attempt. Fwiw, the following shows the best that I can conjure up which at least gets rid of the script errors but still has a build error:
Screen Shot 2018-12-28 at 12.24.24 AM.png
g
@pajatopmr But what is nativeMain? There is no target with name “native” in you config
p
I’m not sure how to answer that question, especially since the code is an attempt to transform the original Groovy script into Kotlin. Perhaps @h0tk3y can answer.
g
So I believe this is expected error, because you cannot configure non-existing target source set
p
But the Groovy script works fine. I’m still learning but if I have it right, the three statements on lines 20-22 define the targets and the
getByName("nativeMain")...
is defining the source set, associating it with the three native targets. Feel free to correct my understanding.
g
Just print all available source sets:
Copy code
println(asMap.values)
p
This is what I get:
Copy code
Values: [source set commonMain, source set commonTest, source set jvmMain, source set jvmTest, source set linuxMain, source set linuxTest, source set macMain, source set macTest, source set windowsMain, source set windowsTest]!
g
so no “nativeMain”
actually you need
asMap.keys
, sorry
Did you try this config on Groovy with the same MPP plugin version? Maybe something changes
p
It’s pretty clear that the intent in the
sourceSets
block is to associate the native source sets with the directory
src/nativeMain
but how that association is supposed to be made is not clear to me.
This script works fine with the Groovy code. It is my inept translation to Kotlin code that is the issue. And I plead guilty to barely knowing what I am doing here.
It might have been missed that this code is from @h0tk3y’s k-new-mpp-samples repo on GitHub. I am trying to learn how the new model works by transforming his Groovy code to Kotlin, fwiw.
btw, I do not expect the native code to work as I’m using Gradle 5.0 (the 4.7 constraint) but I do expect
k-new-mpp-samples/lib-and-app/sample-lib
to build and report errors (link errors at that). That will be confirmation that I have the Groovy -> Kotlin transformation correct.
m
Copy code
kotlin {
    sourceSets["commonMain"].apply {
        dependencies {
            ....
        }
    }

    sourceSets.create("nativeMain").apply {
        dependencies {
            .....
        }
    }
.....
h
Yeah, in Groovy, just accessing a named item as a property is equivalent to
getOrCreate("...")
. In the Kotlin DSL, you should either call those functions (
get("...")
for an existing item,
create("...")
for a new one, or
getOrCreate("...")
if in doubt) or use delegation (https://docs.gradle.org/5.0/userguide/kotlin_dsl.html#using_kotlin_delegated_properties).
So, in Kotlin DSL, that would be something like this:
Copy code
kotlin {
     sourceSets {
          // ...

          val nativeMain by creating { 
               // ...
          }

          configure(listOf(mingw, linux, mac)) {
               compilations["main"].defaultSourceSet.dependsOn(nativeMain)
          }
     }
}
p
Awesome! Extremely helpful and appreciated.