在Xcode项目中添加Carthage的最快方法

8

使用Carthage在Xcode项目中添加依赖项的最快方法是什么。

如何后期添加或编辑依赖项。

3个回答

11

安装 Carthage

下载 Carthage

打开终端

终端: cd ~/路径/到/包含/项目的文件夹

创建 Carthage 文件:

终端: touch Cartfile

从项目文件夹中打开 Cartfile 文件并添加所需的依赖项

示例 Cartfile 文件

github "Alamofire/Alamofire" == 4.5

github "Alamofire/AlamofireImage"

编辑 Cartfile 文件后保存。

从终端运行以下命令

终端: carthage update --platform iOS

xCode > Build phases

  • 左上角加号 > 新建 Run Script Phases
  • Run Script > Shell script 窗口 > 添加以下内容:

/usr/local/bin/carthage copy-frameworks

  • Run Script > Input file 窗口 > 添加以下内容:

$(SRCROOT)/Carthage/Build/iOS/DependencyName.framework

  • Link Binary With Libraries > 加号 > Add Other > 导航至项目文件夹 > Carthage > Build > iOS > 添加框架

enter image description here

完成


问题:在“目标依赖项”中,不应该有一些框架的依赖吗?如果您修改了Cartfile,请确保使用carthage重新编译。这样可以确保新的框架在“运行时”被链接进来吗? - David H
1
我能找到的最简单的指南。比官方文档还好。+1 - Stefano Mtangoo
在我的测试中,这种方法无法用于模拟器构建。也许此时 Carthage 页面上的说明更好用:https://github.com/Carthage/Carthage#quick-start - ThomasW

2

使用Carthage流程

[iOS依赖管理器]

简述:

Download dependency -> build fat binary -> it should  be imported -> slice for release

长版:

  1. Installing Carthage

    //Homebrew
    brew install carthage
    
  2. Create Cartfile file at the project(.xcodeproj)/workspace(.xcworkspace) directory

  3. Modify Cartfile. Add necessary dependency == repo

    github "<owner>/<repo>" == <version>
    
  4. 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)
            }
        }
    }   
    
  5. Drag and drop builded frameworks to General -> Frameworks and Libraries

    //framework location
    <cartfile_path>/Carthage/Build/
    
  6. 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 --no-use-binaries]

这个选项告诉Carthage不要使用预编译二进制文件,而是构建所有依赖项。这将需要更长的时间来完成,但可以解决一些潜在的问题。

[快速 Carthage 构建]

使用该选项可以启用构建缓存和增量构建,从而加快构建速度。这对于具有大量依赖项的项目特别有用。

[本地 Carthage]

使用此选项可以将Carthage依赖项存储在本地文件夹中,以避免在每次构建时下载它们。这对于在没有网络连接的情况下工作的开发人员特别有用。

0
  • 在您的 Mac 上安装 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。请查看它们的自述文件。
  • 将其添加到 Xcode 中:在 Finder 中导航到 $project_dir/Carthage/Build。您将找到一个 FrameworkName.frameworkFrameworkName.xcframework。将 .framework/.xcframework 拖入您的 Xcode 项目(在项目导航器中)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接