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

时间:2020-01-14 02:29:50

标签: kotlin ktor kotlin-multiplatform koin

我正在尝试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文件中,我尝试使用Koin方法设置main

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

但是startKoin无法解决。

我清理/重建了项目,进行了gradle同步,仍然无法导入koin或任何其他依赖项,所以我想念什么

1 个答案:

答案 0 :(得分:2)

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