问题
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
For android
adding java libraries as dependencies is rather easy, but for ios
I could not find a way.
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)
回答1:
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
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
来源:https://stackoverflow.com/questions/61892569/kotlin-multiplatform-project