无法为 Kotlin 多平台通用导入依赖项

Can't import dependencies for Kotlin multi platform common

我正在试用 Kotlin 多平台并尝试为其设置所有依赖项。从 commonMain

开始

我正在尝试将 KoinKtor 依赖项添加到公共部分,但我似乎无法使用它们中的任何一个。

这是我的Gradle脚本

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.61'
}
repositories {
    google()
    jcenter()
    mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId 'org.jetbrains.kotlin.mpp_app_android'
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName '1.0'
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'org.koin:koin-android:2.0.1'
    implementation 'org.koin:koin-androidx-viewmodel:2.0.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel:2.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

kotlin {
    android("android")
    // This is for iPhone emulator
    // Switch here to iosArm64 (or iosArm32) to build library for iPhone device
    iosX64("ios") {
        binaries {
            framework()
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation 'org.koin:koin-ktor:2.0.1'
                implementation 'org.koin:koin-core:2.0.1'

                // HTTP
                implementation "io.ktor:ktor-client-core:1.2.6"
                implementation "io.ktor:ktor-client-json:1.2.6"
                implementation "io.ktor:ktor-client-serialization:1.2.6"

                // Coroutines
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.2.1"
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        androidMain {
            dependencies {
                implementation kotlin('stdlib')
            }
        }
        androidTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        iosMain {
        }
        iosTest {
        }
    }
}

// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework {
    def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
    def target = project.findProperty('kotlin.target') ?: 'ios'
    dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask

    doLast {
        def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
        def targetDir = getProperty('configuration.build.dir')
        copy {
            from srcFile.parent
            into targetDir
            include 'app.framework/**'
            include 'app.framework.dSYM'
        }
    }
}

在我创建项目时生成的 Sample.kt 文件中,我尝试在 main 方法中设置 Koin

fun main() {
    startKoin {
        // declare modules
        modules(myModule)
    }
    println(hello())
}

但是startKoin无法解析。

我 cleaned/rebuilt 这个项目,进行了 gradle 同步,但仍然无法导入 koin 或任何其他依赖项,所以我缺少什么

Koin 不支持多平台。我们有 [multiplatform forks][1],但需要讨论使用哪个以及哪个版本。我们仍在讨论多平台策略。

Ktor 的依赖比这更复杂。见下文。为了清楚起见,我删除了您的其他依赖项,但您显然应该保留它们:)

sourceSets {
        commonMain {
            dependencies {
                implementation "io.ktor:ktor-client-core:1.2.6"
                implementation "io.ktor:ktor-client-json:1.2.6"
                implementation "io.ktor:ktor-client-serialization:1.2.6"
            }
        }
        androidMain {
            dependencies {
                implementation "io.ktor:ktor-client-core-jvm:1.2.6"
                implementation "io.ktor:ktor-client-json-jvm:1.2.6"
                implementation "io.ktor:ktor-client-serialization-jvm:1.2.6"
            }
        }
        iosMain {
            dependencies {
                implementation "io.ktor:ktor-client-ios:1.2.6"
                implementation "io.ktor:ktor-client-core-native:1.2.6"
                implementation "io.ktor:ktor-client-json-native:1.2.6"
                implementation "io.ktor:ktor-client-serialization-native:1.2.6"
            }
        }
}


  [1]: https://github.com/kpgalligan/koin/tree/kpg/khan