Swift Package with dependencies giving error in console.

huangapple go评论64阅读模式
英文:

Swift Package with dependencies giving error in console

问题

我正在尝试从我为客户构建的自定义xcframework创建一个Swift Package。该框架依赖于一些第三方框架。我已经了解到,尽管二进制框架不直接支持依赖关系,但可以通过'wrapper'目标来实现这一点,因此这是我为Package.swift提出的解决方案:

let package = Package(
    name: "SBFramework",
    platforms: [
            .iOS(.v16)
        ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "SBFramework",
            targets: ["SBFramework-Target"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/apple/swift-algorithms.git", from: "1.0.0"),
        .package(url: "https://github.com/fakeurl/NumbersKit.git", from: "1.0.0")
    ],
    targets: [
        .target(name: "SBFramework-Target",
                    dependencies: [
                        .target(name: "SBFramework", condition: .when(platforms: [.iOS])),
                        .product(name: "Algorithms", package: "swift-algorithms"),
                        .product(name: "NumbersKit", package: "NumbersKit")
                    ]
                ),
        .binaryTarget(name: "SBFramework", path: "SBFramework.xcframework")
    ]
)

这个解决方案有效,我可以将该包添加到我的测试项目中,导入框架并且它也链接到了依赖项,功能上一切都正确。

问题是,每次运行应用程序时,Xcode控制台也会显示以下消息:

> objc[845]: Class
> _TtC14NumbersKitP33_0FE53357E470A64027C8F0CAF7B114C812BundleFinder is implemented in both
> /private/var/containers/Bundle/Application/EEE0C0A6-4FF5-44BC-B81A-F95401219D32/TestSBFrameworkImport.app/Frameworks/SBFramework.framework/SBFramework
> (0x100f4aaf0) and
> /private/var/containers/Bundle/Application/EEE0C0A6-4FF5-44BC-B81A-F95401219D32/TestSBFrameworkImport.app/TestSBFrameworkImport
> (0x10069b778). One of the two will be used. Which one is undefined.

对于不同的类,会有多行显示"Class X is implemented in both [.../MyApp.app/Frameworks/MyFramework.framework/MyFramework/] and [.../MyApp.app/MyApp]"。我不确定如何避免这个问题,以及是否会在后续引发问题。这个Swift Package的基础框架链接到了这两个依赖项,因为没有它们我将无法构建框架。但它们还需要添加到应用程序目标(至少需要这样做,如果不这样做,当使用NumbersKit初始化程序时,会导致运行时崩溃)。

有没有解决这个问题的好方法?我担心当客户将其集成到其应用程序中并部署到数千台设备时,这可能会成为问题。

英文:

I am trying to create a Swift Package from a custom xcframework that I'm building for a client. The framework has a dependency on a couple of 3rd party frameworks. I've read about how even though binary frameworks don't support dependencies directly, there is way to do this with a 'wrapper' target, so this is what I came up with for Package.swift:

let package = Package(
    name: "SBFramework",
    platforms: [
            .iOS(.v16)
        ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "SBFramework",
            targets: ["SBFramework-Target"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/apple/swift-algorithms.git", from: "1.0.0"),
        .package(url: "https://github.com/fakeurl/NumbersKit.git", from: "1.0.0")
    ],
    targets: [
        .target(name: "SBFramework-Target",
                    dependencies: [
                        .target(name: "SBFramework", condition: .when(platforms: [.iOS])),
                        .product(name: "Algorithms", package: "swift-algorithms"),
                        .product(name: "NumbersKit", package: "NumbersKit")
                    ]
                ),
        .binaryTarget(name: "SBFramework", path: "SBFramework.xcframework")
    ]
)

This works, and I can add the package to my test project and import the framework and it links against the dependancies as well, and functionally it all works correctly.

The problem is that every time I run the app, it also shows these messages in the Xcode console:

> objc[845]: Class
> _TtC14NumbersKitP33_0FE53357E470A64027C8F0CAF7B114C812BundleFinder is implemented in both
> /private/var/containers/Bundle/Application/EEE0C0A6-4FF5-44BC-B81A-F95401219D32/TestSBFrameworkImport.app/Frameworks/SBFramework.framework/SBFramework
> (0x100f4aaf0) and
> /private/var/containers/Bundle/Application/EEE0C0A6-4FF5-44BC-B81A-F95401219D32/TestSBFrameworkImport.app/TestSBFrameworkImport
> (0x10069b778). One of the two will be used. Which one is undefined.

There's multiple lines for different classes that show the "Class X is implemented in both [.../MyApp.app/Frameworks/MyFramework.framework/MyFramework/] and [.../MyApp.app/MyApp]". I'm not sure how to avoid this problem, and whether this could cause a problem down the line. The framework that is the basis for this Swift Package is linked against the two dependencies, because I wouldn't be able to build the framework without them. But they also need to be added to the app target (at least, and if I don't, I get a run-time crash when using the NumbersKit initializer.

Is there a good way to resolve this issue? I'm worried this will could be a problem for the client when they integrate it into their app, and the app is deployed to 1000s of devices.

答案1

得分: 2

这确实是一个真正的问题,你应该修复它。如果实现冲突,它可能会导致非常微妙的错误。

我预计你在你的框架和你的应用程序中都链接了 NumbersKit。你不应该这样做。NumbersKit 必须链接一次。这可能是因为混合使用 SPM 和直接链接,而不是让 SPM 完成所有工作。如果你的主应用程序也需要 NumbersKit,请确保将其添加为一个包,就像你的框架一样,这样 SPM 就可以解析并链接单个副本。

英文:

Yes, this is a real problem, and you should fix it. It can create very subtle errors if the implementation conflict.

I expect that you're linking NumbersKit both in your framework and also in your app. You must not do this. NumbersKit must be linked exactly one time. This is likely due to mixing SPM with direct linking rather than letting SPM do all the work. If your main app also needs NumbersKit, make sure to add it as a package, just like your framework is doing, so SPM can resolve a single copy to link.

huangapple
  • 本文由 发表于 2023年6月1日 07:06:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定