Reputation: 1877
I am currently working on a project that implements the following steps:
The step number 1 works correctly.
For the step number 2, I use the assemble
gradle task. This task generates the following binaries in the build/libs folder:
For the step 3, I use Android Studio and the "Kotlin Multiplatform Mobile" plugin from JetBrains and I select the "Kotlin Multiplatform Library" template.
Currently, I do not deploy the generated binaries on a maven repository. So for the step number 4, I copy/past the jar files into a "libs" folder and I reference them into the build.gradle.kts file:
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.3")
implementation("io.ktor:ktor-client-core:2.1.3")
implementation("io.ktor:ktor-client-serialization:2.1.3")
implementation("io.ktor:ktor-client-content-negotiation:2.1.3")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.1.3")
implementation(files("libs/kotlin-client-metadata-1.0.0.jar"))
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val androidMain by getting {
dependencies {
implementation("io.ktor:ktor-client-cio-jvm:2.1.3")
implementation(files("libs/kotlin-client-jvm-1.0.0.jar"))
}
}
val androidUnitTest by getting
val iosX64Main by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:2.1.3")
implementation(files("libs/kotlin-client-iosx64-1.0.0-metadata.jar"))
}
}
val iosArm64Main by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:2.1.3")
implementation(files("libs/kotlin-client-iosarm64-1.0.0-metadata.jar"))
}
}
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
}
}
Using this configuration, it seems to work only for the android part of my KMM library project.
In fact, in my KMM library project, I can use classes from my KMP project into classes of the "androidMain" folder. But when I am trying to use the same classes into classes of the "commonMain" or the "iosMain" folders, it does not work. Classes cannot be find (Unresolved reference).
What is wrong with my project? How can I package my KMP library in order to use it in a KMM library?
Upvotes: 2
Views: 1488
Reputation: 3137
The main reason that you can't use your library in iOS and common code, because files kotlin-client-*-metadata.jar
are just metadata. For multiplatform and native code Kotlin use special format of libraries *.klib
. Please take a look at article that explains this.
If you don't want to publish your library and want to use local setup, no need to build the library, this is not a Gradle way. Place your library near your main code and take advantage of multi project builds and add dependency to your lib project:
val commonMain by getting {
dependencies {
//...
implementation(project(":kotlin-client"))
}
}
// no need to add to another source sets, if your library already supports all targets
Or publish it in local Maven repository.
Upvotes: 1