使用Swift Package Manager怎样区分debug和release打包环境
SPM自身并不支持根据打包环境自适应是否依赖某些库。
为了解决这个问题可引入一个私有库,打包前修改该私有库即可实现release下不引用某些库的目的。
import PackageDescription
// 标记是否为测试环境,isDebug为false则不依赖FLEX和LookinServer两个库,为true则依赖这两个库
// 将该私有库放入工程目录下,build前通过脚本修改`isDebug`的值即可自适应依赖
let isDebug = false
var debugDependencies: [Package.Dependency] = []
var debugTargetDep: [PackageDescription.Target.Dependency] = []
if isDebug {
debugDependencies = [
.package(url: "https://github.com/FLEXTool/FLEX.git", from: "5.22.10"),
.package(url: "https://github.com/QMUI/LookinServer.git", from: "1.2.8"),
]
debugTargetDep = [
.product(name: "FLEX", package: "FLEX"),
.product(name: "LookinServer", package: "LookinServer"),
]
}
let package = Package(
name: "MyLibrary",
platforms: [
.iOS(.v15)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "MyLibrary",
targets: ["MyLibrary"]),
],
dependencies: [] + debugDependencies,
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "MyLibrary",
dependencies: [] + debugTargetDep
)
]
)