首页 > 解决方案 > 在另一个 kotlin 多平台库中使用 kotlin 多平台库

问题描述

我在 gradle 中使用 intellij 创建了一个多平台 kotlin 库(L1),它在 JS 和 JVM 中构建。

我想在另一个多平台 kotlin lib L2 中重用这个库我该怎么做?

我在 L2 的公共目标中导入 L1 的“公共”部分有问题,(错误很多Unresolved reference),基本上 L2 的公共目标找不到 L1 中包含的实现。

我只有这个共同目标的问题,js和jvm工作正常。

L1的gradle,要重用的lib

kotlin {
    jvm {
        compilations.all {
            kotlinOptions.jvmTarget = "1.8"
        }
        testRuns["test"].executionTask.configure {
            useJUnit()
        }
    }
    js(LEGACY) {
        browser {
            testTask {
                useKarma {
                    useChromeHeadless()
                    webpackConfig.cssSupport.enabled = true
                }
            }
        }
    }
    
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val jvmMain by getting
        val jvmTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
            }
        }
        val jsMain by getting
        val jsTest by getting {
            dependencies {
                implementation(kotlin("test-js"))
            }
        }
    }
}

L2的gradle,重用L1的lib:

sourceSets {
    val commonMain by getting {
        dependencies {
         
            implementation("groupId:L1:1.0-SNAPSHOT")
        }
    }
    val commonTest by getting {
            dependencies {

            implementation(kotlin("test-common"))
            implementation(kotlin("test-annotations-common"))
        }
    }
    val jvmMain by getting {
        dependencies {

           
            implementation("groupId:L1-jvm:1.0-SNAPSHOT")
        }
    }
    val jvmTest by getting {
        dependencies {

            implementation(kotlin("test-junit"))
        }
    }
    val jsMain by getting {
        dependencies {
            implementation("groupId:L1-js:1.0-SNAPSHOT")
        }
    }
    val jsTest by getting {
        dependencies {

            implementation(kotlin("test-js"))
        }
    }
}

}

标签: kotlinkotlin-multiplatform

解决方案


推荐阅读