簡體   English   中英

Kotlin 多平台項目

[英]Kotlin multiplatform project

我想使用Kotin 多平台編寫一個通用庫,可以在androidios上使用。 該庫將具有每個平台的依賴項,例如:在android上我想添加jsoup作為依賴項,在ios上我想添加swiftsoup

對於android添加 java 庫作為依賴項相當容易,但對於ios我找不到方法。

問題是:如何添加swift庫作為ios的項目的依賴項?

或者有人可以以一個工作項目為例嗎? 我在互聯網上找不到任何可以解決我的問題的東西。

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)

您不能在 Kotlin 中使用 Swift 依賴項,除非它們與 Objective-C 兼容。 如果您想直接與他們交談,則需要使用 cinterop 指向他們。 或者,您可以在 Kotlin 中創建接口,或者采用由 Swift 代碼實現的 lambda,並避免 cinterop。

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

我們在我們的一個示例應用程序中傳遞了很多實現: https://github.com/touchlab/DroidconKotlin/blob/master/iosApp/iosApp/app/AppDelegate.swift#L33

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM