Search code examples
kotlinktorkotlin-multiplatformkoin

Can't import dependencies for Kotlin multi platform common


I am trying out Kotlin multi-platform and trying to setup all my dependencies for it. Starting with commonMain

I am trying to add Koin and Ktor dependencies to the common portion but I cant seem to be able to use any of them.

This is my Gradle script

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'
        }
    }
}

In the Sample.kt file that was generated when I created the project I tried to setup Koin in the main method

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

but startKoin cannot be resolved.

I cleaned/rebuilt the project, did a gradle sync and still cant import koin or any other dependency so what am I missing


Solution

  • Koin is not multiplatform capable. We have [multiplatform forks][1], but would need to chat about which to use and what version. We're still discussing multiplatform strategy.

    Ktor's dependencies are more complex than that. See below. I removed your other dependencies for clarity, but you should leave them obviously :)

    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