【发布时间】:2021-03-04 08:21:44
【问题描述】:
我正在尝试制作一个可在 Android、JavaScript 和 iOS 中使用的 Kotlin 多平台库。该库由多个模块组成,因此可以轻松扩展。 我现在的问题只在于 Kotlin 本机。
这就是项目的设置方式
- 一个模块
:common
object MySingleton {
fun doSomethingWithMyInterface(value: MyInterface) {
// ...
}
}
interface MyInterface {
fun doSomething()
}
- 其他模块实现
MyInterface
class MyInterfaceExample : MyInterface {
override fun doSomething() {
// ...
}
}
我已经为:common 设置了build.gradle.kts 文件,如下所示:
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("maven-publish")
}
kotlin {
targets {
jvm()
js().browser()
ios("ios") {
binaries {
framework("CommonModule") {
baseName = "common"
}
}
}
}
cocoapods {
frameworkName = "CommonModule"
summary = "My common module"
homepage = "-"
license = "-"
ios.deploymentTarget = "10.0"
}
sourceSets {
val iosX64Main by getting
val iosArm64Main by getting
val iosMain by getting {
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
}
}
}
我的其他模块如下:
plugins {
id("com.android.library")
id("maven-publish")
kotlin("multiplatform")
kotlin("native.cocoapods")
}
android {
compileSdkVersion(30)
}
kotlin {
targets {
js().browser()
ios("ios") {
binaries {
framework("ExampleImplementation", listOf(DEBUG)) {
baseName = "example"
freeCompilerArgs += "-Xobjc-generics"
export(project(":common")) {
isStatic = true
}
}
}
}
android("android") {
publishLibraryVariants("release")
}
}
cocoapods {
frameworkName = "ExampleImplementation"
summary = "Example implementation of MyInterface"
homepage = "-"
license = "-"
useLibraries()
ios.deploymentTarget = "10.0"
pod("MyDependency")
}
sourceSets {
val commonMain by getting {
dependencies {
api(project(":common"))
}
}
val iosX64Main by getting
val iosArm64Main by getting
val iosMain by getting {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
}
val androidMain by getting {
dependsOn(commonMain)
dependencies {
implementation("group:artifactId:1.0.0")
}
}
val jsMain by getting {
dependencies {
implementation(npm("my_dependency", "1.0.0"))
}
}
}
}
:common 和 :example 模块都会生成一个 podspec 文件,其中添加了一个由 cocoapods 管理器执行的脚本:
<<-SCRIPT
set -ev
REPO_ROOT="$PODS_TARGET_SRCROOT"
"$REPO_ROOT/../gradlew" -p "$REPO_ROOT" :example:syncFramework \
-Pkotlin.native.cocoapods.target=$KOTLIN_TARGET \
-Pkotlin.native.cocoapods.configuration=$CONFIGURATION \
-Pkotlin.native.cocoapods.cflags="$OTHER_CFLAGS" \
-Pkotlin.native.cocoapods.paths.headers="$HEADER_SEARCH_PATHS" \
-Pkotlin.native.cocoapods.paths.frameworks="$FRAMEWORK_SEARCH_PATHS"
SCRIPT
当我像这样在 iOS 项目中添加这些模块时:
pod 'common', :path => '~/Project/MyLibrary/common/'
pod 'example', :path => '~/Project/MyLibrary/example/'
在我执行pod install 之后,我可以看到两个框架都被添加了。
问题
为example 生成的框架不依赖于为common 生成的框架。然而,:example 会生成一个名为ExampleCommonMyInterface 的自己的接口,它与:common 中的MyInterface 具有相同的签名。
此外,它们都包含一些 KotlinBase 类,当我遇到重复类的问题时,它们会阻止我构建。
我试图只包含 :example 但它缺少我的 MySingleton 类(这不是 Swift 中的单例,但这是另一个问题)和 :example 不直接依赖的其他类。
此外,在某些时候,我希望拥有多个模块,这取决于 :common,因此仅包括 :example 只会暂时起作用。
我这几天尝试了很多东西,但都没有奏效。此外,文档分散在 kotlin multiplatform 和 kotlin native 的文档之间,这让我很难找到有关我想要实现的目标的相关信息。
我知道 Kotlin 多平台仍然不稳定,我想要实现的目标目前可能无法实现。不过,如果有人能说明问题是什么以及如何解决它,我将不胜感激。
【问题讨论】:
标签: kotlin-multiplatform kotlin-native