How to exclude dependency for Catalyst in Swift Package Manager? - ios

There is an ios project with Catalyst included. The project has a YandexMobileMetrica dependency that cannot compile for mac architecture. How can I add an iOS-only dependency using Swift Package Manager?
I tried to do two targets. In the first spm-test, I disabled the mac flag. And in the second, spm-test-mac left. My Package.swift looks like this:
import PackageDescription
let package = Package(
name: "spm-test",
products: [
.library(
name: "spm-test",
targets: ["spm-test"]),
],
dependencies: [
.package(name: "YandexMobileMetrica", url: "https://github.com/yandexmobile/metrica-sdk-ios", from: "3.14.1"),
],
targets: [
.target(
name: "spm-test",
dependencies: ["YandexMobileMetrica"]),
.target(
name: "spm-test-mac",
dependencies: [])
]
)
Error:
AppDelegate.swift:11:12: No such module 'YandexMobileMetrica'
My AppDelegate.swift
// AppDelegate.swift
//
import UIKit
#if !targetEnvironment(macCatalyst)
import YandexMobileMetrica // 🛑 Error here
#endif

Related

Is it possible to have conditional dependencies in a Package.swift target depending on the platform?

I am trying to build a Swift package, where one of the targets must depend on a different version of a library depending on whether it is being built for iOS or MacOS.
For iOS, the dependency is packaged as an .xcframework, whereas for MacOS it is packaged as a system library.
So my dependencies package looks like this:
// swift-tools-version:5.6
import PackageDescription
let package = Package(
name: "LibFoo",
products: [
.library(name: "LibFooFramework", targets: ["LibFooFramework"]),
.library(name: "ClibFoo", targets: ["ClibFoo"])
],
targets: [
.binaryTarget(
name: "LibFooFramework",
path: "path/to/LibFooFramework.xcframework"
),
.systemLibrary(
name: "ClibFoo",
pkgConfig: "libFoo"
)
]
)
So then in my client package, I want to use one of these dependencies depending on the target platform.
I have tried this:
// swift-tools-version:5.6
import PackageDescription
#if os(iOS)
let conditionalTarget: Target = .target(
name: "MyLib",
dependencies: [
.product(name: "LibFooFramework", package: "LibFooFramework")
]
)
#else
let CVulkanUtilsTarget: Target = .target(
name: "MyLib",
dependencies: [
.product(name: "ClibFoo", package: "LibFoo")
]
)
#endif
let package = Package(
name: "MyLib",
products: [
.library(name: "MyLib", targets: ["MyLib"]),
],
dependencies: [
.package(url: "https://github.com/spencerkohan/LibFoo", branch:"master")
],
targets: [
.library(
name: "LibFooFramework",
path: "path/to/LibFooFramework.xcframework"
),
]
)
But it seems that the package manager always takes the else branch, even if the package is being built for iOS. I guess this is because the os target of the Package.swift will always be the build environment and not the target.
But is there any way to achieve this type of conditional dependency?

Swift Package Resolution failed because multiple targets named a same framework

I have 2 local Swift Packages: LibA and LibB.
Both LibA and LibB depends on a same framework(AmazonIVSPlayer).
I want to add both of the into my Project but I got the below error:
Multiple targets named 'AmazonIVSPlayer' in LibA and LibB
the Package.swift of both libraries are like below:
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")
]
)
I read
Swift Package Manager: "multiple targets named..."
Swift Package Manager (SPM) and Cocoapod Dependency Conflict
Swift packages and conflicting dependencies
https://forums.swift.org/t/multiple-target-issue-with-spm/16696
https://www.reddit.com/r/swift/comments/d4wwbk/question_about_dependency_conflicts_in_swift
Since none of them offer any solution and all posts are old, I am wondering is there any new way to solve this issue?
I am also searching for a solution for this problem. The only way I found to work around this, is to create a git repository, put the xcframework (in your case AmazonIVSPlayer) there and a Package.swift. The Package.swift in the newly created repo would look like that:
let package = Package(
name: "AmazonIVSPlayer",
products: [
.library(
name: "AmazonIVSPlayer",
targets: ["AmazonIVSPlayer"]
)
],
targets: [
.binaryTarget(name: "AmazonIVSPlayer", path: "AmazonIVSPlayer.xcframework")
]
)
Then remove the binaryTarget from LibA and LibB and add the following to the dependencies:
.package(url: "https://github.com/foo/bar.git", from: "1.8.1")
Don't forget to create a tag for the version number.
Then add the following to the target dependencies:
.product(name: "AmazonIVSPlayer", package: "bar"),
That way you can use AmazonIVSPlayer in LibA and LibB and can add both Libs to the same project without getting an error. Not the easiest solution, but the only one which works for me.
The error you are getting indicating that you are using two products that have module that conflicts with another products module name. The solution you could try:
If the module causing the name conflict are same (contain the same code and files), then you can create a separate package for the module, in this case a separate package for AmazonIVSPlayer could be created:
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")
]
)
Alternatively, you can combine the package manifest of LibA and LibB into a single package and expose both of them as multiple products:
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")
]
)
Now for the scenarios where both modules are actually different, with swift 5.7 you will be able to differentiate them with module aliases. So with your current package description, when consuming:
targets: [
.executableTarget(
name: "App",
dependencies: [
.product(name: "LibA", package: "LibA"),
.product(name: "LibB", package: "LibB", moduleAliases: ["AmazonIVSPlayer": "AmazonIVSPlayerFromLibB"]),
])
]
When importing module you have to provide the alias name that you have specified:
import AmazonIVSPlayer // imports from LibA
import AmazonIVSPlayerFromLibB // imports from LibB
I've just edited project.pbxproj, and find the target that has the framework added with SPM inside packageProductDependencies list, and I've copied that line to the target that's missing that.
Something like:
name = "Target 1";
38D300792549B0C600FDEFA8 /* UIKit Extensions */,
Copied that line of the framework in the other target
name = "Target 2";
38D300792549B0C600FDEFA8 /* UIKit Extensions */,

Firebase [Swift Package Manager]: no such module FirebaseRemoteConfig

After I read the instructions listed here for how to add Firebase as a dependency to a Swift package, I couldn't get it to work, here's my Package.swift manifest:
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MyPackage",
targets: ["MyPackage"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "Firebase",
url: "https://github.com/firebase/firebase-ios-sdk.git",
.upToNextMajor(from: "8.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: "MyPackage",
dependencies: [.product(name: "FirebaseRemoteConfig", package: "Firebase")]),
.testTarget(
name: "MyPackageTests",
dependencies: ["MyPackage"]),
]
)
the package graph resolves without problems, but when I try to add my own code in Sources/MyPackage.swift starting with import FirebaseRemoteConfig the compiler complains with:
No such model 'FirebaseRemoteConfig'.
what's went wrong with my setup ?
I figured out the solution, but unfortunately it's not documented on Firebase Docs, I have to add the .platforms array in the Package.swift manifest specifying a version that supports FirebaseRemoteConfig, for example:
platforms: [
.iOS(.v13)
]

How do I declare the Siesta Swift package as a dependency of another Swift package?

I'm trying to use the Swift package Siesta as a dependency for the package I'm building and reference it in my package code. I've identified how to import the package into my project in my Package.swift file which is simple enough:
dependencies: [
.package(url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
],
This causes the package to be copied into my package just fine. The problem I'm having is actually linking it up to my package so I can import it and reference it in code. I know I need to actually link it up to my target
I've read some other package files and because the package name for Siesta is like this
let package = Package(
name: "Siesta",
And the products it declares are like this
products: [
.library(name: "Siesta", targets: ["Siesta"]),
.library(name: "SiestaUI", targets: ["SiestaUI"]),
.library(name: "Siesta_Alamofire", targets: ["Siesta_Alamofire"]),
],
I should be able to just do this in my package file's target to use it
.target(
name: "MyTarget",
dependencies: [.product(name: "Siesta", package: "Siesta")]),
But when I try to build my package, I get an error:
/Users/blahblah/Desktop/MyPackage/Package.swift: unknown package 'Siesta' in dependencies of target 'MyTarget'
And not only that, all the targets for my single run scheme on my package go missing and I can't build again without discarding all my local version control changes. What's going on here?
With Swift tools version 5.2 you have to provide a name argument when declaring your package dependency.
.package(name: "Siesta", url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
A working example of a Package.swift file:
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
.library(
name: "MyLibrary",
targets: ["MyTarget"]),
],
dependencies: [
// make sure to provide a `name` argument here
.package(name: "Siesta", url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
],
targets: [
.target(
name: "MyTarget",
dependencies: [
.product(name: "Siesta", package: "Siesta")
]),
]
)
Source: https://forums.swift.org/t/package-names-in-swift-5-2/34886/6

No such module 'PackageDescription' in Xcode 11

I have an existing projects of a framework and library and would like them to use Swift Package Manager.
I have a Xcode 11 and have already already read Creating a Swift Package with Xcode.
After adding Package.swift to the project and an application target
import PackageDescription
let package = Package(
name: "MyLibrary",
platforms: [
.macOS(.v10_13),
],
products: [
.library(name: "MyLibrary", targets: ["MyLibrary"]),
],
dependencies: [
.package(url: "https://url/of/another/package/named/Utility", from: "1.0.0"),
],
targets: [
.target(name: "MyLibrary", dependencies: ["Utility"]),
.testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]),
]
)
I get an error
No such module 'PackageDescription'
How can I import PackageDescription module?
I just added line below at the very first line of the Package.swift file and it worked for me.
// swift-tools-version:4.0
Note: Please do not write any thing even comment before this line
After that do not forget to run swift build command

Resources