ios Swift包解析失败,因为多个目标命名为同一框架

idv4meu8  于 2022-12-20  发布在  iOS
关注(0)|答案(3)|浏览(202)

我有2个本地Swift包裹:LibALibB。LibA和LibB都依赖于同一个框架(AmazonIVSPlayer)。我想将这两个都添加到我的项目中,但出现以下错误:
LibALibB中多个名为“AmazonIVSPlayer”的目标
两个库的Package.swift如下所示:

import PackageDescription

let package = Package(
    name: "LibA",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "LibA",
            targets: ["LibA","AmazonIVSPlayern"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "LibA",
            dependencies: []),
        .binaryTarget(
            name: "AmazonIVSPlayer",
            url: "https://player.live-video.net/1.8.1/AmazonIVSPlayer.xcframework.zip",
            checksum: "8256f9f580fdb09b156afad43cd17dd120091c794e848b27aad83c1a098ecc7f")
    ]
)

我读了

  1. Swift Package Manager: "multiple targets named..."
  2. Swift Package Manager (SPM) and Cocoapod Dependency Conflict
  3. Swift packages and conflicting dependencies
  4. https://forums.swift.org/t/multiple-target-issue-with-spm/16696
  5. https://www.reddit.com/r/swift/comments/d4wwbk/question_about_dependency_conflicts_in_swift
    由于他们都没有提供任何解决方案,所有的职位都是旧的,我想知道有什么新的方法来解决这个问题?
ubof19bj

ubof19bj1#

我也在寻找这个问题的解决方案,我发现解决这个问题的唯一方法是创建一个git仓库,在那里放置xcframework(在你的例子中是AmazonIVSPlayer)和一个Package.swift。

let package = Package(
    name: "AmazonIVSPlayer",
    products: [
        .library(
            name: "AmazonIVSPlayer",
            targets: ["AmazonIVSPlayer"]
        )
    ],
    targets: [
        .binaryTarget(name: "AmazonIVSPlayer", path: "AmazonIVSPlayer.xcframework")
    ]
)

然后从LibA和LibB中删除binaryTarget,并将以下内容添加到依赖项中:

.package(url: "https://github.com/foo/bar.git", from: "1.8.1")

不要忘记为版本号创建一个标记。
然后将以下内容添加到目标依赖项:

.product(name: "AmazonIVSPlayer", package: "bar"),

这样你就可以在LibA和LibB中使用AmazonIVSPlayer,并且可以将两个库添加到同一个项目中而不会出现错误。这不是最简单的解决方案,但却是唯一对我有效的解决方案。

cnh2zyt3

cnh2zyt32#

您收到的错误指示您正在使用的两个产品的模块与另一个产品的模块名称冲突。您可以尝试以下解决方案:
1.如果引起名称冲突的模块是相同的(包含相同的代码和文件),则可以为该模块创建一个单独的包,在这种情况下,可以为AmazonIVSPlayer创建一个单独的包:

import PackageDescription

let package = Package(
    name: "AmazonIVSPlayer",
    products: [
        .library(
            name: "AmazonIVSPlayer",
            targets: ["AmazonIVSPlayern"]),
    ],
    dependencies: [],
    targets: [
        .binaryTarget(
            name: "AmazonIVSPlayer",
            url: "https://player.live-video.net/1.8.1/AmazonIVSPlayer.xcframework.zip",
            checksum: "8256f9f580fdb09b156afad43cd17dd120091c794e848b27aad83c1a098ecc7f")
    ]
)

1.或者,您可以将LibALibB的软件包清单组合到单个软件包中,并将它们作为多个产品公开:

import PackageDescription

let package = Package(
    name: "LibAB",
    products: [
        .library(
            name: "LibA",
            targets: ["LibA","AmazonIVSPlayern"]),
        .library(
            name: "LibB",
            targets: ["LibB","AmazonIVSPlayern"]),
    ],
    dependencies: [],
    targets: [
        .target(
            name: "LibA",
            dependencies: []),
        .target(
            name: "LibB",
            dependencies: []),
        .binaryTarget(
            name: "AmazonIVSPlayer",
            url: "https://player.live-video.net/1.8.1/AmazonIVSPlayer.xcframework.zip",
            checksum: "8256f9f580fdb09b156afad43cd17dd120091c794e848b27aad83c1a098ecc7f")
    ]
)

1.现在,对于两个模块实际上不同的场景,在swift 5.7中,您将能够通过模块别名来区分它们。因此,使用当前的软件包描述时:

targets: [
  .executableTarget(
    name: "App",
    dependencies: [
      .product(name: "LibA", package: "LibA"),
      .product(name: "LibB", package: "LibB", moduleAliases: ["AmazonIVSPlayer": "AmazonIVSPlayerFromLibB"]), 
    ])
]

导入模块时,必须提供已指定的别名:

import AmazonIVSPlayer // imports from LibA
import AmazonIVSPlayerFromLibB // imports from LibB
7vhp5slm

7vhp5slm3#

我刚刚编辑了project. pbxproj,找到了在packageProductDependencieslist中添加了SPM框架的目标,并将该行复制到了缺少它的目标中。
例如:

name = "Target 1";
        38D300792549B0C600FDEFA8 /* UIKit Extensions */,

在其他目标中复制了框架的该行

name = "Target 2";
        38D300792549B0C600FDEFA8 /* UIKit Extensions */,

相关问题