【问题标题】:Kotlin multiplatform projectKotlin 多平台项目
【发布时间】:2020-09-05 14:35:10
【问题描述】:

我想使用Kotin multiplatform 编写一个通用库,可以在androidios 上使用。 该库将具有每个平台的依赖项,例如:在android 我想添加jsoup 作为依赖项和在ios 我想添加swiftsoup

对于android,添加 java 库作为依赖项相当容易,但对于 ios,我找不到方法。

问题是:如何为ios 添加一个swift 库作为此项目的依赖项?

或者有人可以以我的工作项目为例吗?我在互联网上找不到任何可以解决我的问题的东西。

build.gradle.kts :

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

    val serializationVersion = "0.20.0"
    val kotlinVersion = "1.3.72"

    plugins {
        kotlin("multiplatform") version kotlinVersion
        kotlin("plugin.serialization") version kotlinVersion
    }

    buildscript {
        dependencies {
            classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion")
        }
    }

    repositories {
        jcenter()
        mavenCentral()
    }

    kotlin {
        //select iOS target platform depending on the Xcode environment variables
        val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
                if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
                    ::iosArm64
                else
                    ::iosX64

        iOSTarget("ios") {
            binaries {
                framework {
                    baseName = "SharedCode"
                }
            }
        }

        jvm("android")

        sourceSets["commonMain"].dependencies {
            implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion")
        }

        sourceSets["androidMain"].dependencies {
            implementation("org.jetbrains.kotlin:kotlin-stdlib")
            implementation("org.jsoup:jsoup:1.13.1")
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion")
        }

        sourceSets["iosMain"].dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serializationVersion")
        }

    }

    val packForXcode by tasks.creating(Sync::class) {
        group = "build"

        //selecting the right configuration for the iOS framework depending on the Xcode environment variables
        val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
        val framework = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)

        inputs.property("mode", mode)
        dependsOn(framework.linkTask)

        val targetDir = File(buildDir, "xcode-frameworks")
        from({ framework.outputDirectory })
        into(targetDir)

        doLast {
            val gradlew = File(targetDir, "gradlew")
            gradlew.writeText("#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n")
            gradlew.setExecutable(true)
        }
    }

    tasks.getByName("build").dependsOn(packForXcode)

【问题讨论】:

    标签: android ios swift kotlin kotlin-multiplatform


    【解决方案1】:

    您不能在 Kotlin 中使用 Swift 依赖项,除非它们与 Objective-C 兼容。如果您想直接与他们交谈,则需要使用 cinterop 指向他们。或者,您可以在 Kotlin 中创建接口,或者采用 lambdas,由 Swift 代码实现,并避免 cinterop。

    https://kotlinlang.org/docs/reference/native/objc_interop.html

    我们在我们的一个示例应用程序中传递了很多实现:https://github.com/touchlab/DroidconKotlin/blob/master/iosApp/iosApp/app/AppDelegate.swift#L33

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多