跳到主要内容

多 Target / 白标项目管理

问题

同一套代码需要打包多个不同品牌的 App(白标),如何管理?

答案

方案对比

方案适用场景复杂度
Build Configuration简单的环境区分
Target2-3 个差异较大的 App
xcconfig + 脚本批量白标(10+ App)

xcconfig 方案

Config/BrandA.xcconfig
PRODUCT_NAME = BrandA
BUNDLE_IDENTIFIER = com.company.brandA
APP_ICON = AppIcon-A
PRIMARY_COLOR = #FF5733
API_BASE_URL = https://api.branda.com
Config/BrandB.xcconfig
PRODUCT_NAME = BrandB
BUNDLE_IDENTIFIER = com.company.brandB
APP_ICON = AppIcon-B
PRIMARY_COLOR = #3357FF
API_BASE_URL = https://api.brandb.com

代码中读取配置

// Info.plist 中引用 xcconfig 变量
// API_BASE_URL = $(API_BASE_URL)

enum AppConfig {
static let apiBaseURL: String = {
Bundle.main.infoDictionary?["API_BASE_URL"] as? String ?? ""
}()

static let primaryColor: UIColor = {
let hex = Bundle.main.infoDictionary?["PRIMARY_COLOR"] as? String ?? "#000000"
return UIColor(hex: hex)
}()
}

资源管理

Assets/
├── BrandA/
│ ├── AppIcon.appiconset/
│ └── LaunchScreen.storyboard
├── BrandB/
│ ├── AppIcon.appiconset/
│ └── LaunchScreen.storyboard
└── Shared/
└── common assets...

常见面试问题

Q1: 多 Target 编译慢怎么优化?

答案:多 Target 共享同一份源码,避免重复编译。差异部分通过 #if 编译条件或运行时配置区分。将共享代码抽成 Framework/SPM Package,每个 Target 只编译差异部分。

相关链接