Carthage

版权声明:知识版权是属于全人类的! 欢迎评论与转载!!! https://blog.csdn.net/zhuge1127/article/details/82078097

Carthage基本流程

  1. 创建一个Cartfile文件,里面包含你将要在你项目中使用的frameworks.
  2. 运行Carthage,这将获取和编译你列出来的framework.
  3. 将编译的.framework二进制文件拖进你的项目工程.
  4. Create a Cartfile that lists the frameworks you’d like to use in your project.
  5. Run Carthage, which fetches and builds each framework you’ve listed.
  6. Drag the built .framework binaries into your application’s Xcode project.

Carthage为你构建了依赖并且提供给你二进制的framework,但是并不修改project的配置信息, 你需要自己去修改配置信息.
Carthage builds your dependencies and provides you with binary frameworks,
but you retain full control over your project structure and setup.
Carthage does not automatically modify your project files or your build settings.

Carthage和CocoaPods区别

默认情况下,CocoaPods会为你的项目和依赖自动创建和更新工作区间(workspace)
automatically creates and updates an Xcode workspace for your application and all dependencies.

Carthage使用xcodebuild编译framework的二进制文件,将他们的集成留给用户来操作
Carthage builds framework binaries using xcodebuild, but leaves the responsibility of integrating them up to the user.

对于使用者来说,CocoaPods更为简单,而Carthage更为灵活并且对你的项目更少的入侵
CocoaPods’ approach is easier to use, while Carthage’s is flexible and unintrusive.

CocoaPods项目还必须具有podspec文件,它包含有关项目的元数据及明确项目的编译方法。
Carthage使用xcodebuild去编译依赖,而不是将依赖集成到一个单一的工作区间,但你的依赖必须包括它们自己的Xcode工程文件来描述是如何编译它们的项目。
创建了Carthage是因为我们想要最简单的工具:一个依赖管理器,完成任务而不取代Xcode的部分功能,并且不为framework作者增加额外的工作。
CocoaPods提供的那些神奇的特性Carthage永远不会有。因为我们不会用复杂度去换取那些特性。
CocoaPods projects must also have what’s known as a podspec file, which includes metadata about the project and specifies how it should be built.
Carthage uses xcodebuild to build dependencies, instead of integrating them into a single workspace, it doesn’t have a similar specification file but your dependencies must include their own Xcode project that describes how to build their products.

安装carthage

  1. 直接下载安装包: 这种方式是最好的,因为你可能无法指定Homebrew的版本号
  2. Homebrew: brew install carthage 如果你之前有安装carthage,你需要删除/Library/Frameworks/CarthageKit.framework).

给应用添加frameworks

注: carthage只支持动态库
Note that Carthage only supports dynamic frameworks, which are only available on iOS 8 or later (or any version of OS X).

  1. 创建一个cartfile文件,并且列出所需要的frameworks, 关于如何写cartfile需要参见<创建cartfile>
    Create a Cartfile that lists the frameworks you’d like to use in your project.

  2. 执行carthage update:
    先将代码fetch到Carthage/Checkouts文件夹下.build 所有的framework,或者下载一些预编译的framework.
    Run carthage update. This will fetch dependencies into a Carthage/Checkouts folder, then build each one or download a pre-compiled framework.

  3. 在Linked Frameworks and Libraries中从Carthage/Build文件夹下找到并且添加需要使用的frameworks
    On your application targets’ General settings tab, in the “Linked Frameworks and Libraries” section, drag and drop each framework you want to use from the Carthage/Build folder on disk.

  4. 在Build Phases中添加一个新 的run script选项, 创建一个运行脚本
    On your application targets’ Build Phases settings tab, click the + icon and choose New Run Script Phase. Create a Run Script in which you specify your shell (ex: /bin/sh), add the following contents to the script area below the shell:

脚本内容如下:
/usr/local/bin/carthage copy-frameworks

  1. 添加input files
    Add the paths to the frameworks you want to use under “Input Files”. For example:
$(SRCROOT)/Carthage/Build/iOS/Result.framework
$(SRCROOT)/Carthage/Build/iOS/ReactiveSwift.framework
$(SRCROOT)/Carthage/Build/iOS/ReactiveCocoa.framework
  1. 添加output files
    Add the paths to the copied frameworks to the “Output Files”. For example:
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Result.framework
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/ReactiveSwift.framework
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/ReactiveCocoa.framework

有了与输入文件一起指定的输出文件,Xcode只需要在输入文件发生更改或丢失输出文件时运行脚本, 这样的话如果你没有运行update命令, 那么就不会更新本地的库文件.
With output files specified alongside the input files, Xcode only needs to run the script when the input files have changed or the output files are missing. This means dirty builds will be faster when you haven’t rebuilt frameworks with Carthage.

此脚本处理由通用二进制文件触发的App Store提交错误,并确保在归档时复制必需的bitcode-related文件和dSYM。
This script works around an App Store submission bug triggered by universal binaries and ensures that necessary bitcode-related files and dSYMs are copied when archiving.

通过将调试信息复制到已经编译的工程的目录中,只要在断点处停止,Xcode就能够对堆栈跟踪进行符号化。它也使你在调试器中通过第三方代码。
With the debug information copied into the built products directory, Xcode will be able to symbolicate the stack trace whenever you stop at a breakpoint. This will also enable you to step through third-party code in the debugger.

当打包程序提交到App Store或TestFlight时,Xcode还会将这些文件复制到应用程序的.xcarchive包的dSYMs子目录中。
When archiving your application for submission to the App Store or TestFlight, Xcode will also copy these files into the dSYMs subdirectory of your application’s .xcarchive bundle.

Artifacts

在整个过程中,Carthage将会创建一些artifacts。
其中最重要的是Cartfile.resolved文件,它列出了为每个framework编译的实际版本。确保提交你的Cartfile.resolved,因为任何使用该项目的人将通过该文件来编译相同的framework版本。

使用Carthage去运行项目

当完成上述的步骤之后,需要拉取并执行carthage bootstrap

为framework添加frameworks

对任何任意target的依赖性使用Carthage非常类似于前面提到的给应用添加frameworks。 主要的区别在于frameworks如何在Xcode中设置和链接。
The main difference lies in how the frameworks are actually set up and linked in Xcode.

因为单元测试target在其“General”设置选项卡中缺少“Linked Frameworks and Libraries”部分,所以必须将构建的frameworks拖动到“Link Binaries With Libraries”构建阶段。

在“Build Settings”选项卡下的测试目标中,将@ loader_path/Frameworks添加到“Runpath Search Paths”(如果尚未存在)。
在极少数情况下,你可能想将每个依赖复制到你构建的产品中(例如,在外部框架中嵌入依赖项,或确保测试包中存在依赖性)。 为此,使用“Framework”目标创建一个新的“Copy Files”构建阶段,然后在那里添加框架引用。

升级frameworks

如果你已经修改了你的Cartfile,或者你想去更新每个框架到最新版本(在你指定要求在下进行),只需要简单的运行carthage update命令。
如果你只想更新一个或特定的依赖项,只需要将它们作为以空格分隔的列表传递给update命令。例如carthage update Box 或者 carthage update Box Result

嵌套依赖关系

如果你想添加到项目中的框架的依赖已经出现在Cartfile中了,Carthage将会为你自动检索它们。然后,你必须从Carthage/Build 文件夹将它们自己拖动到你的项目中。
如果项目中的嵌入框架具有对其他框架的依赖性,则必须将它们链接到应用程序目标(即使应用程序目标对该框架没有依赖性,也从不使用它们)。
If the framework you want to add to your project has dependencies explicitly listed in a Cartfile, Carthage will automatically retrieve them for you. You will then have to drag them yourself into your project from the Carthage/Build folder.

If the embedded framework in your project has dependencies to other frameworks you must link them to application target (even if application target does not have dependency to that frameworks and never uses them).

使你的framework支持Carthage

Carthage 正式的只支持动态框架。动态框架在OS X的任何版本都可以使用,但IOS 只能是8或者8+。
因为Carthage没有集中的包列表,没有项目规范格式,大多数框架应该自动构建。
任何框架项目的具体要求如下。

share your xcode schemes

Carthage将只构建从.xcodeproj共享的Xcode方案。你可以通过运行carthage build –no-skip-current,然后检查 Carthage/Build 文件夹来查看是否成功创建了所有计划的方案。
Carthage will only build Xcode schemes that are shared from your .xcodeproj. You can see if all of your intended schemes build successfully by running carthage build –no-skip-current, then checking the Carthage/Build folder.

当你运行那个命令的时候如果有一个重要的方案没有被编译,打开Xcdoe并确保该方案标记为“共享”,以便Carthage可以发现它。
If an important scheme is not built when you run that command, open Xcode and make sure that the scheme is marked as Shared, so Carthage can discover it.

Carthage build –no-skip-current

将预编译的框架归档到一个zip文件中

如果它们附加到项目存储库上的GitHub发行版或通过二进制项目定义文件,Carthage可以自动使用预构建框架,而不是从头开始构建。

要为特定标记提供预构建框架,所有被支持的平台的二进制文件应该一起压缩到一个归档中,并且该归档应附加到与该标记相对应的已发布版本。 附件应在其名称中包括.framework(例如ReactiveCocoa.framework.zip),以向Carthage表明其包含二进制文件。
你可以像下面这样通过carthage archive命令执行归档操作:

carthage bulid --no-skip-current
carthage archive YourFrameworkName

Build static frameworks to speed up your app’s launch times
If you embed many dynamic frameworks into your app, its pre-main launch times may be quite slow. Carthage is able to help mitigate this by building your dynamic frameworks as static frameworks instead. Static frameworks can be linked directly into your application or merged together into a larger dynamic framework with a few simple modifications to your workflow, which can result in dramatic reductions in pre-main lauch times. See the StaticFrameworks doc for details.
Please note that a few caveats apply to this approach:
• Swift static frameworks are not officially supported by Apple
• This is an advanced workflow that is not built into Carthage, YMMV

猜你喜欢

转载自blog.csdn.net/zhuge1127/article/details/82078097