With Kotlin/Multiplatform 1.3.11 i had the following build.gradle
:
fromPreset(presets.jvm, 'jvm')
fromPreset(presets.android, 'jvm') // reusing jvm sources for android platform impl
So basically i wanted to reuse all code for jvm
target for android
target.
With just-released Kotlin/Multiplatform 1.3.20 now i'm getting an error:
The target 'jvm' already exists, but it was not created with the 'android' preset. To configure it, access it by name in
kotlin.targets
or use the preset function 'jvm' Open File
I've tried to migrate to new syntax:
jvm()
android() {
sourceSets.add(kotlin.targets.jvm.compilations.main.defaultSourceSet)
}
but it does not reuse jvm defaultSourceSet for Android target:
Expected class 'URL' has no actual declaration in module
so it does not use default jvm sourceset actually and not throwing Groovy syntax error.
What's wrong?
Actually, the best solution is to have some common sourceset and one for each platform:
commonJvmMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
}
jvmMain {
dependsOn commonJvmMain
}
androidMain {
dependsOn commonJvmMain
}
Put shared code in commonJvmMain
sourceset directory.