Kotlin多平台项目 [英] Kotlin multiplatform project

查看:130
本文介绍了Kotlin多平台项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Kotin多平台编写一个公共库可以在androidios上使用. 该库将具有每个平台的依赖关系,例如:在android上,我要添加 jsoup 作为依赖关系,并且在ios上,我想添加 swiftsoup

I want to write a common library using Kotin multiplatform that can be used on android and on ios. This library will have dependencies for each platform, for eg: on android I want to add jsoup as a dependency and on ios I want to add swiftsoup

对于android,将Java库添加为依赖项相当容易,但是对于ios,我找不到方法.

For android adding java libraries as dependencies is rather easy, but for ios I could not find a way.

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

The question is: how can I add a swift library as a dependency to this project for ios?

还是可以有人将我指向一个正在运行的项目作为示例?我在互联网上找不到能解决我问题的任何东西.

or can somebody point me to a working project as an example? I could not find anything on the internet that could solve my issue.

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中创建接口,也可以采用由Swift代码实现的lambda,并避免使用cinterop.

You can't use Swift dependencies in Kotlin unless they are compatible with Objective-C. If you want to talk to them directly, you'll need to point to them with cinterop. Alternatively, you can create interfaces in Kotlin, or take lambdas, that are implemented by Swift code, and avoid cinterop.

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

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

We pass in a lot of implementations in one of our example apps: https://github.com/touchlab/DroidconKotlin/blob/master/iosApp/iosApp/app/AppDelegate.swift#L33

这篇关于Kotlin多平台项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆