Thread
#multiplatform
    s

    Sebastien Leclerc Lavallee

    2 years ago
    Hey guys! I did setup exporting dependencies to be able to use a library inside my Swift code. It seems to have worked partially. I’m exporting RXCommon and when inside the Swift code, I see only interfaces and some abstract classes but most of the interesting classes aren’t available. Does that ring a bell to someone? Thanks!
    russhwolf

    russhwolf

    2 years ago
    Needs some extra config beyond the default, or else you only see types which your own code exposes. https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#exporting-dependencies-in-frameworks
    s

    Sebastien Leclerc Lavallee

    2 years ago
    That’s pretty much what I did. This is my shared build.gradle.kts:
    import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
    
    plugins {
        kotlin("multiplatform")
        kotlin("native.cocoapods")
        id("kotlinx-serialization")
        id("com.android.library")
        id("com.squareup.sqldelight")
        id("co.touchlab.kotlinxcodesync")
    }
    
    android {
        compileSdkVersion(28)
        defaultConfig {
            minSdkVersion(Versions.min_sdk)
            targetSdkVersion(Versions.target_sdk)
            versionCode = 1
            versionName = "1.0"
            testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        }
    }
    
    kotlin {
        android()
        //Revert to just ios() when gradle plugin can properly resolve it
        val onPhone = System.getenv("SDK_NAME")?.startsWith("iphoneos")?:false
        if(onPhone){
            iosArm64("ios")
        }else{
            iosX64("ios")
        }
    
        targets.getByName<KotlinNativeTarget>("ios").compilations["main"].kotlinOptions.freeCompilerArgs += "-Xobjc-generics"
    
        version = "1.0"
    
        sourceSets.create("macosMain").apply {
            dependencies {
                // Note: the exported dependencies must be added in the API configuration.
                api(Deps.rxcommon)
            }
        }
    
        sourceSets["commonMain"].dependencies {
            implementation(kotlin("stdlib-common", Versions.kotlin))
            implementation(Deps.SqlDelight.runtime)
            implementation(Deps.ktor.commonCore)
            implementation(Deps.ktor.commonJson)
            implementation(Deps.Coroutines.common)
            implementation(Deps.stately)
            implementation(Deps.multiplatformSettings)
            implementation(Deps.koinCore)
            implementation(Deps.ktor.commonSerialization)
            api(Deps.rxcommon)
        }
    
        sourceSets["commonTest"].dependencies {
            implementation(Deps.multiplatformSettingsTest)
            implementation(Deps.SqlDelight.runtime)
            implementation(Deps.KotlinTest.common)
            implementation(Deps.KotlinTest.annotations)
            implementation(Deps.Coroutines.jdk)
            implementation(Deps.Coroutines.common)
            implementation(Deps.Coroutines.test)
        }
    
        sourceSets["androidMain"].dependencies {
            implementation(kotlin("stdlib", Versions.kotlin))
            implementation(Deps.SqlDelight.driverAndroid)
            implementation(Deps.ktor.jvmCore)
            implementation(Deps.ktor.jvmJson)
            implementation(Deps.Coroutines.jdk)
            implementation(Deps.Coroutines.android)
            implementation(Deps.ktor.androidSerialization)
        }
    
        sourceSets["androidTest"].dependencies {
            implementation(Deps.KotlinTest.jvm)
            implementation(Deps.KotlinTest.junit)
            implementation(Deps.Coroutines.jdk)
            implementation(Deps.AndroidXTest.core)
            implementation(Deps.AndroidXTest.junit)
            implementation(Deps.AndroidXTest.runner)
            implementation(Deps.AndroidXTest.rules)
            implementation("org.robolectric:robolectric:4.0")
        }
    
        sourceSets["iosMain"].dependencies {
            implementation(Deps.SqlDelight.driverIos)
            implementation(Deps.ktor.ios, Deps.coroutinesExcludeNative)
            implementation(Deps.ktor.iosCore, Deps.coroutinesExcludeNative)
            implementation(Deps.ktor.iosJson, Deps.coroutinesExcludeNative)
            implementation(Deps.Coroutines.native)
            implementation(Deps.ktor.iosSerialization)
    
            api(Deps.rxcommon)
        }
    
        cocoapods {
            summary = "Common library for the KaMP starter kit"
            homepage = "<https://github.com/touchlab/KaMPStarter>"
        }
    
        xcodeSync {
            projectPath = "../ios/KaMPStarteriOS.xcodeproj"
            target = "KaMPStarteriOS"
        }
    }
    
    sqldelight {
        database("KampstarterDb") {
            packageName = "co.touchlab.kampstarter.db"
        }
    }
    
    val iOSTest: Task by tasks.creating {
        val device = project.findProperty("iosDevice")?.toString() ?: "iPhone 8"
        dependsOn("linkDebugTestIos")
        group = JavaBasePlugin.VERIFICATION_GROUP
        description = "Runs tests for target 'ios' on an iOS simulator"
    
        doLast {
            kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.framework {
                transitiveExport = true
                export(Deps.rxcommon)
            }
    
            val binary = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getTest("DEBUG").outputFile
            exec {
                commandLine("xcrun", "simctl", "spawn", "--standalone",device, binary.absolutePath)
            }
        }
    }