1

I've built a lib in Kotlin, which I would like to use in Kotlin backend, Android app & in swift ios app. Lib is written in plain Kotlin and it consists of 3 modules, dependent from each other. To create multiplatform/ios version of it, I've created separated module with build.gradle.kts:

plugins {
    kotlin("multiplatform")
}
kotlin {
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
                implementation(project(":myJvmModule"))
            }
        }
    }
    iosArm64 { binaries.framework("NativeFramework") }
    iosX64 { binaries.framework("NativeFramework") }

    tasks {
      ...
    }
}

tasks.withType<Wrapper> {
    gradleVersion = "5.3.1"
    distributionType = Wrapper.DistributionType.ALL
}

Besides that, myJvmModule has build.gradle.kts:

val fasterXmlVersion: String by project

plugins {
    kotlin("jvm")
}

dependencies {
    // Modules
    implementation(project(":myAnotherModule"))
    implementation(project(":myAnotherModule2"))

    // Jackson
    implementation(group = "com.fasterxml.jackson.module", name = "jackson-module-kotlin", version = fasterXmlVersion)
}

No matter how I try to use myJvmModule as dependency, I always receive error: Could not resolve company:myJvmModule:0.0.1 How can I import JVM module to multiplatform module?

1 Answer 1

4

I'm not sure I completely understand your question here. If your intention is to use a JVM module transitively from a native module, it's simply not possible.

Multiplatform modules have different source sets: common, JVM, JS, and native ones like iOS.

In a multiplatform module, you can only depend on JVM modules from the JVM source set. But note that this dependency will only be available to the JVM "flavour" of the multiplatform library, not to the iOS one.

If you want to use a multiplatform library from a platform specific module like iOS, that "consumer" module will only have access to the common code and the iOS-specific code of the multiplatform library. Since it will be running on iOS, there is no way that module can transitively use a JVM one (it won't have a JVM runtime).

I guess your best option here (if you control the JVM module) is to turn your JVM module into a multiplatform one as well. Any JVM library used there will need to be replaced by a multiplatform equivalent, or a native equivalent will need to be added for the native target. If you choose the latter, you'll likely have to define a common API in common code, and use expect/actual declarations to map common code to platform-specific one.

You can read more about multiplatform dependency management in the official doc.

Sign up to request clarification or add additional context in comments.

7 Comments

What I want is to compile my code to .h files, which I'll be able to use in existing iOS Swift project. However, lib which I want to compile to .h files, need to be dependent on another module - myJvmModule
Kotlin multiplatform alone cannot do that, it is not how it's designed to be used. The way multiplatform can help is by giving a possibility to write common code and share that between platforms. But it doesn't allow you to use platform-specific code in another platform. You cannot use a JVM module (myJvmModule) in a native project.
Is it possible to use any maven repository lib inside multiplatform module? i.e. "com.fasterxml.jackson.module:jackson-module-kotlin:2.12.3"
Some maven libs are multiplatform, some aren't. Jackson is a purely JVM library, it cannot be used in common code, nor on any other platform than JVM. jackson-module-kotlin is also JVM-only, and is only meant to add capabilities to Jackson to handle Kotlin-specific subtleties (it is only meant for Kotlin/JVM). If you need multiplatform JSON serialization/deserialization, you should instead have a look at kotlinx-serialization
Also, if you're using JSON only for the payload of network requests, you may want to look at multiplatform HTTP clients altogether, like Ktor. This way you can reuse the whole client across different platforms
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.