如何使用 Kotlin/native 生成依赖于另一个的 .framework?

How to generate a .framework depending from another with Kotlin/native?

我对 KMP 和 iOS 框架有一些依赖性问题。

这里是一些上下文: 我有 2 个 KMP 模块:

我还有 2 个 android 个项目和 2 个 iOS 个项目:

在 Android,我有以下内容:

// KMP API project
public interface Foo

// KMP libA project
public class Bar {
    fun doSomething(foo: Foo)
}

// ANDROID: libB project
import API
public class FooImpl: Foo { }

// ANDROID app
import libA
import libB
var foo = FooImpl()
var bar = Bar()
bar.doSomething(foo) // <----- Everything is fine here

但是在 iOS,我有这个:

// iOS app
import libA
import libB
var foo = FooImpl()
var bar = Bar()
bar.doSomething(foo) // <----- Error here : FooImpl is of type APIFoo but here LibAAPIFoo is excpected 

确实,当我查看生成的 headers 时,我有以下内容:

// KMP API.h
@protocol APIFoo
@end;

// KMP libA.h
@protocol LibAKAFoo // <----- here we have a redefinition of the protocol.
@end;

@interface Bar
- (void)doSomething:(KMPKAFoo)foo;  
@ends;

我期待有更多类似的东西:

// KMP API.h
@protocol APIFoo
@end;

// KMP libA.h
#include <API/API.h> // <----- import the API

@interface Bar
- (void)doSomething:(APIFoo)foo; // <----- use it
@ends;

我的 build.gradle 中是否缺少特殊配置? 我尝试在依赖项定义中使用 compileOnly,但它没有任何效果,并且具有与 implementation

相同的行为
val commonMain by getting {
dependencies {
    compileOnly("com.poc.sample:KMPAPI:0.0.1")
}

您不能创建多个 Kotlin iOS 框架并在同一个项目中交替使用它们。当 Kotlin 编译器创建一个 iOS 框架时,它就是它的“自己的世界”,因为它包含您需要的一切(依赖项等)。这是一个大二进制文件。

总结一下,你想要的配置是不可能的。您可以在同一项目中使用多个 Kotlin iOS 框架,但它们需要完全独立。他们将无法相互通信。