使用Carthage在Xcode项目中添加依赖项的最快方法是什么。
如何后期添加或编辑依赖项。
使用Carthage在Xcode项目中添加依赖项的最快方法是什么。
如何后期添加或编辑依赖项。
安装 Carthage
打开终端
终端: cd ~/路径/到/包含/项目的文件夹
创建 Carthage 文件:
终端: touch Cartfile
从项目文件夹中打开 Cartfile 文件并添加所需的依赖项
示例 Cartfile 文件
github "Alamofire/Alamofire" == 4.5
github "Alamofire/AlamofireImage"
编辑 Cartfile 文件后保存。
从终端运行以下命令
终端: carthage update --platform iOS
xCode > Build phases
/usr/local/bin/carthage copy-frameworks
$(SRCROOT)/Carthage/Build/iOS/DependencyName.framework
完成
使用Carthage流程
简述:
Download dependency -> build fat binary -> it should be imported -> slice for release
长版:
Installing Carthage
//Homebrew
brew install carthage
Create Cartfile
file at the project(.xcodeproj
)/workspace(.xcworkspace
) directory
Modify Cartfile
. Add necessary dependency == repo
github "<owner>/<repo>" == <version>
Run carthage update
under Cartfile
location. High level logic:
`carthage update [dependency]` {
- reads `Cartfile`, resolves dependency graph and generates `Cartfile.resolved` to store a list of versions that will be actually built
//--no-use-binaries - this flag force a Carthage to not use a `Prebuilt framework`
//--no-build - this flag skip a building step - `carthage build`
`carthage bootstrap [dependency]` {
- reads `Cartfile.resolved`
`carthage checkout [dependency]` {
`carthage fetch <path>` {
- fetch dependency from `Cartfile.resolved` into `~/Library/Caches/org.carthage.CarthageKit/` folder
}
- checkout/move a dependency from `~/Library/Caches/org.carthage.CarthageKit/` to generated `Carthage/Checkouts` folder
}
`carthage build [dependency]` {
- builds all `shared frameworks schemes` from `Carthage/Checkouts` into generated `Carthage/Build` folder
//--no-skip-current - (+current consumer)this flag also builds all `shared frameworks schemes` of a consumer workspace/project(in addition to `Carthage/Checkouts` folder)
}
}
}
Drag and drop builded frameworks to General -> Frameworks and Libraries
//framework location
<cartfile_path>/Carthage/Build/
Run the next script. This step is important because carthage build a fat
binary(arm64... + x86_64) using lipo
[About]. Apple reject application which uses it. That is why you should add this extra step to cut architecture for simulator(x86_64)
Build Phases -> + -> New Run Script phase ->
// /usr/local/bin/carthage - path to Carthage, copy-frameworks - command which sets a necessary architecture and copies framework to an application bundle
Shell -> /usr/local/bin/carthage copy-frameworks
//path to a generated Carthage/Build
Input files -> + -> $(SRCROOT)/Carthage/Build/<platform>/<name>.framework
任何carthage
命令都应该从Cartfile
文件夹中调用。
如果你遇到了一些错误
This usually indicates that project itself failed to compile. Please check the xcodebuild log for more details: /var/folders/2q/jhrcxkmx49g21lydqfrf26ph0000gn/T/carthage-xcodebuild.AySUH3.log
//you can use open command to review the log
open /var/folders/2q/jhrcxkmx49g21lydqfrf26ph0000gn/T/carthage-xcodebuild.AySUH3.log
创建 Carthage 缓存
rm -rf ~/Library/Caches/org.carthage.CarthageKit
卡在更新上
carthage update --platform ios --verbose --new-resolver
//e.g.
//The dependency graph contained a cycle
Use XCFramework[About] --use-xcframeworks
//error
Building universal frameworks with common architectures is not possible. The device and simulator slices for "<schema_name>" both build for: arm64
Rebuild with --use-xcframeworks to create an xcframework bundle instead.
//solution
carthage update --platform ios --verbose --new-resolver --use-xcframeworks
这个选项告诉Carthage不要使用预编译二进制文件,而是构建所有依赖项。这将需要更长的时间来完成,但可以解决一些潜在的问题。
使用该选项可以启用构建缓存和增量构建,从而加快构建速度。这对于具有大量依赖项的项目特别有用。
使用此选项可以将Carthage依赖项存储在本地文件夹中,以避免在每次构建时下载它们。这对于在没有网络连接的情况下工作的开发人员特别有用。brew install carthage
Cartfile
,例如 vim Cartfile
并粘贴以下内容:
github "AFNetworking/AFNetworking" ~> 4.0
carthage update --platform iOS
carthage update
命令的说明。您可能需要运行例如 carthage update --use-xcframeworks --platform iOS --no-use-binaries
。请查看它们的自述文件。$project_dir/Carthage/Build
。您将找到一个 FrameworkName.framework
或 FrameworkName.xcframework
。将 .framework/.xcframework 拖入您的 Xcode 项目(在项目导航器中)。