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

dwursteisen

03/03/2020, 3:14 PM
Hello all! I’m trying to implement a lib which run on the JVM, JS and Android but with different dependencies for each platform. I got a lot of issues to define specific android dependencies without having any error from Gradle.  When I’m trying to add a dependency to Android, I got an error saying my compilation with the name «release » (I tried with « main » and « test » or name given by android(“android”).compilations) wasn’t found. 
Copy code
android("android").compilations["release"].defaultSourceSet { ... }
I end up with this solution: iterate over all compilations and if the name match the one I expect, change dependencies. Is anyone already encounter this kind of issues? How did you fix it? ``````
My solution:
Copy code
plugins {
  id("com.android.application")
  kotlin("multiplatform") version "1.3.61"
}

kotlin {
  // I need to put a name to the target otherwise
  // I got a conflict with the KotlinOptions extension already declared.
  android("android") {}
  jvm { }
}

sourceSets {
 // …
jvm().compilations["test"].defaultSourceSet {
  dependencies {
   // specific JVM dependencies
  }
}

android("android").compilations.all {
  if (this.name.endsWith("Test")) {
    this.defaultSourceSet.dependencies {
        // specify Android Dependencies
    }
  }
 }
}
k

Kris Wong

03/03/2020, 3:17 PM
you're in the
sourceSets
scope, but then you're calling the target functions and having to drill all the way back in
use
named("androidMain")
for instance
d

dwursteisen

03/03/2020, 3:34 PM
😱 So simple. I though that
jvm()
… was part of the
sourceSets
scope. 👍 thanks
k

Kris Wong

03/03/2020, 3:37 PM
use code navigation to explore the plugin source and learn it's structure
☝️ 1
2 Views