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

Dico

03/16/2019, 12:18 PM
If I have a multiplatform child project with 2 JVM targets, how can I select one target as a dependency in gradle?
d

Dominaezzz

03/16/2019, 12:31 PM
implementation "com.dico:library-jvmFirst:1.0.0"
instead of just
implementation "com.dico:library-jvm:1.0.0"
or
implementation "com.dico:library:1.0.0"
.
I'm not sure if I've understood your question actually. Could you expand?
d

Dico

03/16/2019, 12:37 PM
So I have a multiplatform project
A child project should be used as a dependency
It has 2 JVM targets
Before I had 1, I can do
api(project(":child"))
It will find the jvm target and add it as a dependency.
I tried getting the source set from the child project and using
dependsOn
in my root source set, but it seems to assume that the source set will be from the same project
d

Dominaezzz

03/16/2019, 12:49 PM
Ah, you need to select the specific artifact but I can't find docs about it.
d

Dico

03/16/2019, 1:10 PM
Copy code
sourceSets["jvmMain"].apply {
    val targetName = (findProperty("platform")?.toString() ?: "remote") + "Jvm"
    val target = platform.kotlin.targets[targetName]
    val jarTask = platform.tasks.getByName(target.artifactsTaskName) as Jar
    val platform = jarTask.archiveFile.get().asFile

    dependencies {
        api(engine)
        api(platform)
    }
}
Here:
Copy code
dependencies {
    api(project(":platform", "myTargetJvmApiElements")
}
d

Dominaezzz

03/16/2019, 2:41 PM
Did that work?
d

Dico

03/16/2019, 2:46 PM
Both of them work.
And with both of them, IntelliJ doesn't recognize the dependencies, so everything is red.
But the compile task works fine.
l

louiscad

03/16/2019, 2:48 PM
Did you report it on kotl.in/issue?
d

Dico

03/16/2019, 2:48 PM
Actually, the
compileKotlinMetadata
doesn't work.
compileKotlinJvm
is fine
There's no "commonMainApiElements"
Does an api configuration extend an implementation configuration? @louiscad I think you might know this
Or is it the other way around? wait...
l

louiscad

03/16/2019, 2:56 PM
api is implementation + api
d

Dico

03/16/2019, 2:57 PM
Copy code
val platform = project(":platform")
val configurationName = (findProperty("platform")?.toString() ?: "remote") + "JvmApiElements"

sourceSets["commonMain"].dependencies {
    api(engine)
    api(platform)
}

sourceSets["jvmMain"].dependencies {
    api(engine)
    api(project(":platform", configurationName))
}
works!
l

louiscad

03/16/2019, 2:59 PM
@Dico Isn't
api(engine)
a duplicate in
jvmMain
?
d

Dico

03/16/2019, 3:00 PM
Is it? I always do it like this
l

louiscad

03/16/2019, 3:00 PM
I do it only for per platform artifacts
d

Dico

03/16/2019, 3:01 PM
Ah it figures that part out if I put it in common. Awesome!
l

louiscad

03/16/2019, 3:02 PM
If you're using gradle metadata, yes
If the library is also compatible with gradle metadata
d

Dico

03/16/2019, 3:02 PM
I am, I thought it just figures out which target to use from the project
but it does the same then for the other target sif its in common