最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Kotlin Multiplatform generated file lib to KMP sample app - Stack Overflow

programmeradmin5浏览0评论

I have a problem importing my kmp lib to my sample project.

Project

I have the following code structure :

├── app/ (KMP App module containing my samples apps)
│   ├── commonMain/
│   ├── androidMain/
│   └── jsMain/
├── myLib/ (KMP module for my library)
│   └── commonMain/
└── libs/ (myLib compiled as files)
    ├── myLib-release.aar
    ├── myLib-metadata.jar
    └── myLib-js.klib

Steps

What I try to do is building myLib to files and importing it in the commonMain folder of my samples app (to access it from commonMain, jsMain & androidMain).

  1. I build myLib. Here is my myLib/build.gradle file :
plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.android.library)
}

val sdkVersion = "1.2.0"

kotlin {
    androidTarget {
        version = sdkVersion

        @OptIn(ExperimentalKotlinGradlePluginApi::class)
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_21)
        }
    }
    js {
        browser {
            version = sdkVersion
            binaries.executable()
        }
    }
    withSourcesJar()

    sourceSets {
        commonMain {
            dependencies {
                ...
            }
        }
    }
}

android {...}
dependencies {...}

tasks.withType<Jar> {
    manifest {
        attributes["Implementation-Title"] = "myLib-${sdkVersion}"
        attributes["Implementation-Version"] = sdkVersion
    }
}

  1. I retrieve the generated files in the ./myLib/build/ folder and extract them to ./libs/

  2. I import them in my sample module. Here is the app/build.gradle :

kotlin {
    androidTarget {...}

    js {...}

    sourceSets {
        androidMain.dependencies {
            ...
            implementation(files("../libs/myLib-release.aar"))
        }
        commonMain.dependencies {
            ...
            //implementation(project(":myLib"))
            implementation(files("../libs/myLib-metadata.jar"))
        }
        jsMain.dependencies {
            ...
            implementation(files("../libs/myLib-js.klib"))
        }
    }
}

Problem

I successfully retrieve and import my lib on androidMain and jsMain. However, I can't import it in my commonMain code. My lib namespace is found, but I can't use the defined class in it.

I checked the generated .jar, and it seems everything is here. (Also, when I import it from the module project as implementation(project(":myLib")), everything works fine)

Is there a thing I'm missing here ? Anyone as an idea why I can't do that ?

I have a problem importing my kmp lib to my sample project.

Project

I have the following code structure :

├── app/ (KMP App module containing my samples apps)
│   ├── commonMain/
│   ├── androidMain/
│   └── jsMain/
├── myLib/ (KMP module for my library)
│   └── commonMain/
└── libs/ (myLib compiled as files)
    ├── myLib-release.aar
    ├── myLib-metadata.jar
    └── myLib-js.klib

Steps

What I try to do is building myLib to files and importing it in the commonMain folder of my samples app (to access it from commonMain, jsMain & androidMain).

  1. I build myLib. Here is my myLib/build.gradle file :
plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.android.library)
}

val sdkVersion = "1.2.0"

kotlin {
    androidTarget {
        version = sdkVersion

        @OptIn(ExperimentalKotlinGradlePluginApi::class)
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_21)
        }
    }
    js {
        browser {
            version = sdkVersion
            binaries.executable()
        }
    }
    withSourcesJar()

    sourceSets {
        commonMain {
            dependencies {
                ...
            }
        }
    }
}

android {...}
dependencies {...}

tasks.withType<Jar> {
    manifest {
        attributes["Implementation-Title"] = "myLib-${sdkVersion}"
        attributes["Implementation-Version"] = sdkVersion
    }
}

  1. I retrieve the generated files in the ./myLib/build/ folder and extract them to ./libs/

  2. I import them in my sample module. Here is the app/build.gradle :

kotlin {
    androidTarget {...}

    js {...}

    sourceSets {
        androidMain.dependencies {
            ...
            implementation(files("../libs/myLib-release.aar"))
        }
        commonMain.dependencies {
            ...
            //implementation(project(":myLib"))
            implementation(files("../libs/myLib-metadata.jar"))
        }
        jsMain.dependencies {
            ...
            implementation(files("../libs/myLib-js.klib"))
        }
    }
}

Problem

I successfully retrieve and import my lib on androidMain and jsMain. However, I can't import it in my commonMain code. My lib namespace is found, but I can't use the defined class in it.

I checked the generated .jar, and it seems everything is here. (Also, when I import it from the module project as implementation(project(":myLib")), everything works fine)

Is there a thing I'm missing here ? Anyone as an idea why I can't do that ?

Share Improve this question asked Mar 11 at 9:20 clemence roumyclemence roumy 313 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Your common code needs more than just the common metadata to work. There is not yet1 a common format which you can pass around in one simple file like that.

The way to pass multiplatform libraries around is as published "components", which is your compiled code artifacts together with metadata which tells the multiplatform compilation where to find the platform-specific artifacts that it needs.

To do this, you should publish your library, at first either to your Maven local repository or even just a local directory acting as a repository. Your consuming build can then obtain your publication from your repository using its coordinates, just like a regular third-party library. Kotlin have written a guide on multiplatform publishing to get you started.

Alternatively, depending on what you are doing you may consider working with the library as a subproject within your app, making it a Gradle multi-module project. This allows you to easily pass around subprojects as libraries without the configuration publishing involves.


1 See Kotlin issue KT-52666

发布评论

评论列表(0)

  1. 暂无评论