Creating multiplatform library for usage in other multiplatform projects

Hello! Currently I developing library using kotlin/multiplatform. I want to make this library usable in other multi-platform projects. Let me explain what I mean. Currently I have modules:

lib/
sample_app/

Module “lib” contains shared library, which I want to export.
build.gradle file of “lib” module:

plugins {
    id "org.jetbrains.kotlin.multiplatform"
}

kotlin {
    sourceSets {
        commonMain {
            kotlin.srcDir('src/main')
            dependencies {
                implementation kotlin('stdlib-common')
                implementation kotlin('reflect')
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
    }

    jvm().compilations.main.defaultSourceSet {
        kotlin.srcDir('src/jvm')
        dependencies {
            implementation kotlin('stdlib-jdk8')
        }
    }
    jvm().compilations.test.defaultSourceSet {
        kotlin.srcDir('src/test')
        dependencies {
            implementation kotlin('test-junit')
        }
    }

}

build.gradle file of “sample_app” module:

plugins {
    id "org.jetbrains.kotlin.multiplatform"
}

kotlin {
    sourceSets {
        commonMain {
            kotlin.srcDir('src/main')
                dependencies {
                    implementation kotlin('stdlib-common')
                    implementation kotlin('reflect')
                    implementation project(':lib')
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
    }
    jvm().compilations.test.defaultSourceSet {
        kotlin.srcDir('src/test')
        dependencies {
            implementation kotlin('test-junit')
        }
    }
}

I need to build module “lib” to multiplatform library, which can be used as dependency in “commonMain”:
In sample_app/build.gradle I need to replace line

implementation project(':lib')

by

implementation file('my_library_file')

It needed to use this library as dependency in SHARED (!!!) code (“commonMain”), not separate for jvm and iOS, but exactly in shared code. Idea is to use this library in different projects to write shared code using it and publish this library later so others can use it in multiplatform projects.

I can’t find way how to build multiplatform library, only can build separately .jar and Apple Framework library, which cannot be used in shared code as dependency. Can anybody help with it?

You need to publish lib as a multiplatform library to a Maven repository. Then you will be able to declare a dependency on it in your common sourcesets as usual, see https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#adding-dependencies

BTW a great place to ask questions like this is the multiplatform channel in Kotlin Slack, please join us there.

1 Like

Thanks you for your answer. After all I decided to publish to maven using local repository. I don;t know why it is not possible to simply make build of library file but it’s solved main problem.

Asked to add me to slack but still no answer after one day :frowning:

Please be patient, the Slack requests are processed manually for legal reasons.