Is it possible to use common kotlin library (not multiplatform) in kotlin multiplatform commonMain/JVM?

I built Kotlin library and already used it on Android but it doesn’t has any android dependencies in it. It’s just plain Kotlin (helpers, utils and extensions) and some annotations. Then, I try to include my Kotlin library into another project built with kotlin multiplatform. Is it possible?

I tried:

kotlin {
    /* Targets configuration omitted. 
    *  To find out how to configure the targets, please follow the link:
    *  https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#setting-up-targets */

    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation "com.my.libs:utils:0.0.1"
                // kapt "com.my.libs:utils-processor:0.0.1"
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
    }
}

But, I can’t use my library on commonMain as it can’t find the library. I saw the libs is successfully installed and listed in External Libraries folder. And by the way, I also have my annotation processor library along with the utils library. Is it possible to use it on Kotlin Multiplatform?

When you publish a library on maven, the kotlin plugin compiles it. In your case if you published it in pure JVM, a .jar has been uploaded. If you published as Android library, an .aar has been uploaded.

A Common target has it’s particular type of publication file structure, as such if you did not built you library with the K/MPP plugin, you cannot use in a K/MPP project.

2 Likes

Currently it is not possible to publish a common-only library. You have to declare targets from the beginning. Sadly it is a limitation of current compilation model. It will be fixed in future, when IR and klib format will be available.

4 Likes

What what?! Kotlin 1.4 will allow to implement the missing targets of a published K/MPP library?!

Thank you for the information. Can’t wait to see possible awesome future kotlin world!

@lamba92 Not 1.4 (it has quite limited public usage of IR) and not for jar-based libraries. But in ultimate future klib format will allow to do that.

2 Likes

I am beginner. I created boilerplate for JVM/Ios/native(mac) using MPP commonMain/nativeMain/iosMain/AndriodMain. nativeMain uses CInterop and working in native. I want to transfer Cinterop import in shareMain, so that all platform uses this. But i a stuck how to configure dependencies/sourceset. Please help to proceed further. Thanks

With Kotlin 1.4-m2 and 1.4-m3 the IDE should be able to infer the common cinterops created in a native only source set.

Have a look here: Kotlin 1.4-M2 Released | The Kotlin Blog

Thanks for your reply. Idea ID-community version does not contain experimental MPP wizard shown in 1.4-m3. I am rewriting my question, as i feel, i am fundamentally wrong some where. i included kt jar along with java jar in javac. created klib using cinterop. But now stuck to klib linking with javac/dx/d8.
My question is basic. I have lot of simple “c/C” language functions . I want to re-use these for aneroid app. I want to use kotlin interface and Cinterop. Please help. How to use sharecode+cinterop for android app. Or how to use klib in android. Or how to convert klib to Jar. I know these are silly questions, but I am not clear about fundamentals. Please guide, what is possible and what is impossible and what is the correct path. I want to use command prompt and want to avoid JNI. I am coming from iOS objc background.

regards

manchanda