android - Kotlin 多平台项目

标签 android ios swift kotlin kotlin-multiplatform

我想用 Kotin multiplatform 写一个通用库可用于 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)

最佳答案

除非它们与 Objective-C 兼容,否则不能在 Kotlin 中使用 Swift 依赖项。如果您想直接与他们交谈,则需要使用 cinterop 指向他们。或者,您可以在 Kotlin 中创建接口(interface),或者采用 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

关于android - Kotlin 多平台项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61892569/

相关文章:

ios - 在不移除标签栏的情况下显示额外的 View Controller

iphone - 调用方法时 UIView 无法正确显示

ios - 如何在运行时调整 XIB/NIB 内 subview 的大小

ios - Swift:单击后退按钮后错过标签栏

android - 向图层列表添加渐变

java - Android 异步任务中的 Thread.Sleep()

ios - iOS10 是否删除了从 bundle 中读取 SQLite 数据库的功能?

ios - 如何提高将 UIImage 转换为 CVPixelBuffer 的性能?

java - android中如何显示已安装的应用程序?

java - 在android上,获取xml rsa公钥并用它加密一个字符串