Kotlin Multiplattform Project Dependencies - kotlin

What do I do wrong, I just want to add dependencies to my multiplattform project (at the moment just windows):
I just added some example repository and dependencies.
plugins {
kotlin("multiplatform") version "1.5.0"
kotlin("plugin.serialization") version "1.5.0"
}
kotlin {
allprojects {
repositories {
mavenCentral()
google()
jcenter()
maven { setUrl("https://jitpack.io") }
}
}
sourceSets {
commonMain {
dependencies {
implementation("com.squareup.okhttp3:okhttp:4.9.1")
implementation("io.ktor:ktor-server-netty:1.5.4")
implementation("io.ktor:ktor-server-core:1.5.4")
}
}
}
}
kotlin {
mingwX64("native") {
binaries {
executable()
}
}
}
tasks.withType<Wrapper> {
gradleVersion = "7.0.1"
distributionType = Wrapper.DistributionType.ALL
}
Outcome:
Output

I would guess none of those dependencies are actually available for windows. You'll need windows compatible ktor dependencies (and I'd stick to Kotlin 1.4.32 vs 1.5 because of kotlinx.coroutines, for now).
Ktor config for windows is probably like this, but I've never done this for Windows.
sourceSets {
commonMain {
dependencies {
implementation "io.ktor:ktor-client-core:$ktor_version"
}
}
mingwX64Main {
dependencies {
implementation "io.ktor:ktor-client-curl:$ktor_version"
}
}
}

Related

Kotlin NoSuchMethodError after installing a library that enables a plugin

I want to experience a library called arrow analysis
My build.gradle.kts file looks as follows:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.10"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
implementation("io.arrow-kt:arrow-core:1.1.2")
}
buildscript {
dependencies {
classpath("io.arrow-kt.analysis.kotlin:io.arrow-kt.analysis.kotlin.gradle.plugin:2.0")
}
}
apply(plugin = "io.arrow-kt.analysis.kotlin")
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
after an attempt to build the project, I get the following error:
java.lang.NoSuchMethodError: 'org.jetbrains.kotlin.psi.KtExpression org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt.getReceiverExpression(org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)'
at arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.KotlinResolvedCall.getReceiverExpression(KotlinResolvedCall.kt:37)
at arrow.meta.plugins.analysis.phases.analysis.solver.ResolvedCallUtilsKt.allArgumentExpressions(ResolvedCallUtils.kt:75)
at arrow.meta.plugins.analysis.phases.analysis.solver.ResolvedCallUtilsKt.arg(ResolvedCallUtils.kt:119)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.controlFlowAnyFunction(Expressions.kt:438)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.checkCallExpression(Expressions.kt:397)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.fallThrough(Expressions.kt:260)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.access$fallThrough(Expressions.kt:1)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:244)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:157)
at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
I checked the source code, and everything seems to be in place. Did I something wrong as per the instructions to install this library which is denoted as follows:
buildscript {
dependencies {
classpath("io.arrow-kt.analysis.kotlin:io.arrow-kt.analysis.kotlin.gradle.plugin:2.0")
}
}
apply(plugin = "io.arrow-kt.analysis.kotlin")
You need to apply the plugin inside the plugins block and in the same fashion you're applying the Kotlin JVM plugin.
They mention to use it like this on their official docs
plugins {
kotlin("multiplatform") version "1.6.21"
// other plugins
id("io.arrow-kt.analysis.kotlin") version "2.0.2"
}
buildscript {
repositories {
mavenCentral()
}
}

How to share kotlin multiplatform targets between submodules?

After an eternity I managed to setup a kotlin multi-platform and multi-module project. As there is almost no proper guide or documentation, I have just one more issue I couldn't solve.
Can I share kotlin multiplatform targets between the submodules?
Right now every submodule has a build.gradle.kts containing:
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "15"
}
}
js(IR) {
nodejs()
browser {
testTask {
useKarma {
useChromeHeadless()
webpackConfig.cssSupport.enabled = true
}
}
}
}
}
Obviously, I want all modules to compile to the same platforms using the same versions and it would be nice to not copy this snippet for every module.
Not to mention that the test dependencies are identical as well.
sourceSets {
val commonTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-api:$kotestVersion")
implementation("io.kotest:kotest-assertions-core:$kotestVersion")
implementation("io.kotest:kotest-property:$kotestVersion")
}
}
val jvmTest by getting {
dependsOn(commonTest)
dependencies {
implementation("io.kotest:kotest-runner-junit5:$kotestVersion")
}
}
val jsTest by getting {
dependsOn(commonTest)
dependencies {
implementation("io.kotest:kotest-framework-engine:$kotestVersion")
}
}
}

Can I use Spock test in Koltin Multiplatform project? If so how to set in gradle.build.kts?

I try to use Spock test framework in my Kotlin multiplatform project, but with fail, the gradle script:
plugins {
val kotlinVersion = "1.4.30"
kotlin("multiplatform") version kotlinVersion
application
kotlin("plugin.serialization") version kotlinVersion
groovy // for spock
}
repositories {
// ...
maven("https://jitpack.io") // for spock
}
kotlin {
jvm {withJava()}
js {/*...*/}
sourceSets {
val commonMain by getting {
dependencies {/*...*/}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jvmMain by getting {
dependencies {/*...*/}
}
val jvmTest by getting {
dependencies {
dependsOn(commonTest)
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
// implementation(kotlin("test-junit5"))
// for spock
implementation("org.spockframework.spock:spock-core:spock-1.3")
implementation("org.codehaus.groovy:groovy-all:3.0.7")
}
}
val jsMain by getting {
dependencies {/*...*/}
}
}
}
I tried the spock-1.3 and spock-2.0-M4 with groovy-2.5 and Junit5, all failed with dependencies problems, even thought the test can run, it tells: Test events were not received, and more important:
I cannot create groovy source fold for test only.(The directory appreas like normal folder, not source or test folder)
How to configure the dependencies or gradle.build.kts file? Any help is highly appreciated!
I can now make Spock work with existed Ktor project with the code below added to my gradle.build.kts :
plugins {
groovy
}
repositories {
maven("https://jitpack.io")
}
dependencies {
testImplementation("org.codehaus.groovy:groovy-all:2.4.15")
testImplementation("org.spockframework.spock:spock-core:spock-1.3")
}
sourceSets {
main {
withConvention(GroovySourceSet::class) {
groovy {
setSrcDirs(listOf("src"))
}
}
}
test {
withConvention(GroovySourceSet::class) {
groovy {
setSrcDirs(listOf("test"))
}
}
}
}
tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}
However, I cannot figure out the Koltin Multiplatform project with Spock test, I will post the script when I make it.

Can't import dependencies for Kotlin multi platform common

I am trying out Kotlin multi-platform and trying to setup all my dependencies for it. Starting with commonMain
I am trying to add Koin and Ktor dependencies to the common portion but I cant seem to be able to use any of them.
This is my Gradle script
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.61'
}
repositories {
google()
jcenter()
mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
defaultConfig {
applicationId 'org.jetbrains.kotlin.mpp_app_android'
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName '1.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.koin:koin-android:2.0.1'
implementation 'org.koin:koin-androidx-viewmodel:2.0.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
kotlin {
android("android")
// This is for iPhone emulator
// Switch here to iosArm64 (or iosArm32) to build library for iPhone device
iosX64("ios") {
binaries {
framework()
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
implementation 'org.koin:koin-ktor:2.0.1'
implementation 'org.koin:koin-core:2.0.1'
// HTTP
implementation "io.ktor:ktor-client-core:1.2.6"
implementation "io.ktor:ktor-client-json:1.2.6"
implementation "io.ktor:ktor-client-serialization:1.2.6"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.2.1"
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
androidMain {
dependencies {
implementation kotlin('stdlib')
}
}
androidTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
iosMain {
}
iosTest {
}
}
}
// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework {
def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
def target = project.findProperty('kotlin.target') ?: 'ios'
dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask
doLast {
def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
def targetDir = getProperty('configuration.build.dir')
copy {
from srcFile.parent
into targetDir
include 'app.framework/**'
include 'app.framework.dSYM'
}
}
}
In the Sample.kt file that was generated when I created the project I tried to setup Koin in the main method
fun main() {
startKoin {
// declare modules
modules(myModule)
}
println(hello())
}
but startKoin cannot be resolved.
I cleaned/rebuilt the project, did a gradle sync and still cant import koin or any other dependency so what am I missing
Koin is not multiplatform capable. We have [multiplatform forks][1], but would need to chat about which to use and what version. We're still discussing multiplatform strategy.
Ktor's dependencies are more complex than that. See below. I removed your other dependencies for clarity, but you should leave them obviously :)
sourceSets {
commonMain {
dependencies {
implementation "io.ktor:ktor-client-core:1.2.6"
implementation "io.ktor:ktor-client-json:1.2.6"
implementation "io.ktor:ktor-client-serialization:1.2.6"
}
}
androidMain {
dependencies {
implementation "io.ktor:ktor-client-core-jvm:1.2.6"
implementation "io.ktor:ktor-client-json-jvm:1.2.6"
implementation "io.ktor:ktor-client-serialization-jvm:1.2.6"
}
}
iosMain {
dependencies {
implementation "io.ktor:ktor-client-ios:1.2.6"
implementation "io.ktor:ktor-client-core-native:1.2.6"
implementation "io.ktor:ktor-client-json-native:1.2.6"
implementation "io.ktor:ktor-client-serialization-native:1.2.6"
}
}
}
[1]: https://github.com/kpgalligan/koin/tree/kpg/khan

Cannot update kotlin to v1.3.50 for multiplatform project

I'm using kotlin 1.3.41 for my multiplatform project (included ios, jvm, js) and everything is ok. Now I'm trying to update to the latest version 1.3.50 but get this error:
Could not determine the dependencies of task ':kotlinNpmInstall'.
> org.jetbrains.kotlin.gradle.targets.js.npm.KotlinNpmResolutionManager$ResolutionState$Installed cannot be cast to org.jetbrains.kotlin.gradle.targets.js.npm.KotlinNpmResolutionManager$ResolutionState$Configuring
I have tried to search everywhere (github, kotlin issue tracker) but got nothing. Does anyone know what causes this error? Thanks.
Part of my configuration:
Root project configuration
buildscript {
apply from: "buildsystem/dependencies.gradle"
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/kotlinx/" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath deps.gradlePlugins.android
classpath deps.gradlePlugins.androidNavigation
classpath deps.gradlePlugins.node
classpath deps.gradlePlugins.kotlin
classpath deps.gradlePlugins.kotlinSerialization
classpath deps.gradlePlugins.kotlinAtomicfun
classpath deps.gradlePlugins.dokka
}
}
Multiplatform module configuration
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'kotlinx-serialization'
kotlin {
targets {
jvm("jvm")
js("js") {
nodejs {}
}
iosArm64("ios64")
iosX64("iosSim")
configure([ios64, iosSim]) {
binaries.framework {
baseName = "LIB"
}
}
}
sourceSets {
def commonDependencies = rootProject.ext.deps.common
commonMain {
dependencies {
implementation commonDependencies.kotlin
implementation commonDependencies.kotlinCoroutines
}
}
commonTest {
dependencies {
implementation commonDependencies.kotlinTest
implementation commonDependencies.kotlinTestAnnotations
implementation commonDependencies.mockk
}
}
def jvmDependencies = rootProject.ext.deps.jvm
jvmMain {
dependencies {
implementation jvmDependencies.kotlin
implementation jvmDependencies.kotlinCoroutines
}
}
jvmTest {
dependencies {
implementation jvmDependencies.kotlinTest
implementation jvmDependencies.kotlinTestJunit
implementation jvmDependencies.kotlinCoroutinesTest
implementation jvmDependencies.mockk
}
}
def jsDependencies = rootProject.ext.deps.js
jsMain {
dependencies {
implementation jsDependencies.kotlin
implementation jsDependencies.kotlinCoroutines
// npm
implementation npm("uuid", "^3.3.2")
}
}
jsTest {
dependsOn jsMain
dependsOn commonMain
dependencies {
implementation jsDependencies.kotlinTest
}
}
def iosDependencies = rootProject.ext.deps.ios
iosMain {
dependencies {
implementation iosDependencies.kotlinCoroutines
}
}
ios64Main {
dependsOn iosMain
}
iosSimMain {
dependsOn iosMain
}
}
}
// other configuration
I can confirm that the issue is in Android Studio.
When I run the official KotlinJS react app tutorial on IntelliJ, the app works. But on AS, I get the above error when executing any Gradle task dependent on npm.
Until the issue is resolved, I recommend using the Community version of IntelliJ. You can download it here