美文网首页
BUCK - 使用BUCK编译iOS项目(2)

BUCK - 使用BUCK编译iOS项目(2)

作者: 西西的一天 | 来源:发表于2019-12-21 17:09 被阅读0次

在上一节中使用了BUCK编译了一个最简单的iOS工程。本节来进一步添加一些难度,把Cocoapods引入项目中,添加一些第三方依赖。之前我的所有编译需要的信息都写在一个BUCK文件中,当项目复杂后,写在一起回导致可读性和可维护性降低,官方给出了这样一份图:


packages.png

子目录下可以有自己的BUCK文件,对自己的内容进行描述、管理,当根目录下的BUCK需要依赖子目录下的类库时,可以通过一种path的方式进行引用。那么当我们使用Cocoapods时,所有三方库都被下载到Pods目录下,可以通过在Pods目录下写一个BUCK文件,来声明可以被使用的三方库。

使用BUCK之前

在引入BUCK之前,若需要使用Cocoapods做依赖管理非常简单,只需在项目根目录中执行pod init,在自动生成的Podfile中添加需要的依赖;再执行pod install即可。

pod install会生成一个workspace的工程文件,打开运行,即完成了依赖的导入。它的编译过程依赖于原先的工程文件以及新生成的workspace文件。依赖关系为:原Target -> Pods -> 其他三方库。

回想一下上一节中提到的BUCK编译过程,BUCK的编译是没有工程文件参与的,如需要依赖其他的三方库,其实也不需要经由Pods这个中间Target做为衔接,直接由原Target直接依赖其他三方库即可(但需要为三方库编写对应的BUCK文件)。

使用BUCK之后

此时我们需要做的便是如下三步:

使用Cocoapods来下载三方库

首先我们来先写一个简单的Podfile

platform :ios, '10.0'

# Only download the files, don't create Xcode projects
install! 'cocoapods', integrate_targets: false

target 'BuckSample' do
  pod 'Alamofire', '5.0.0-rc.3'
end

和原本的Podfile没有什么差别,唯一不同的是添加了一句:
install! 'cocoapods', integrate_targets: false,它的作用是只下载,并不生成相应的工程文件,这也正是BUCK所需要的。

编写BUCK文件

在Podfile中,只添加了Alamofire的依赖,所以需要添加的BUCK文件只需要一个apple_binary。

load("//Config:buck_rule_macros.bzl", "apple_third_party_lib")

apple_third_party_lib(
    name = "Alamofire",
    srcs = glob([
        "Alamofire/**/*.swift",
    ]),
    frameworks = [
        "$SDKROOT/System/Library/Frameworks/CFNetwork.framework",
    ],
)

此处的apple_third_party_lib只是一个helper,定义在buck_rule_macros.bzl中,只是忽略三方库的warning而已:

def apple_third_party_lib(**kwargs):
    apple_lib(
        warning_as_error = False,
        suppress_warnings = True,
        **kwargs
    )

在这个BUCK中还有一个framework,如何知道需要哪些系统的Framework呢?这就需要看看所引用的三方库的Podspec文件了:

Pod::Spec.new do |s|
  s.name = 'Alamofire'
  s.version = '5.0.0-rc.3'
  s.license = 'MIT'
  s.summary = 'Elegant HTTP Networking in Swift'
  s.homepage = 'https://github.com/Alamofire/Alamofire'
  s.authors = { 'Alamofire Software Foundation' => 'info@alamofire.org' }
  s.source = { :git => 'https://github.com/Alamofire/Alamofire.git', :tag => s.version }
  s.documentation_url = 'https://alamofire.github.io/Alamofire/'

  s.ios.deployment_target = '10.0'
  s.osx.deployment_target = '10.12'
  s.tvos.deployment_target = '10.0'
  s.watchos.deployment_target = '3.0'

  s.swift_versions = ['5.0', '5.1']

  s.source_files = 'Source/*.swift'

  s.frameworks = 'CFNetwork'
end

添加完这个BUCK后,当再次运行buck targets //...时,便可以看到这个target:

Starting new Buck daemon...
Parsing buck files... 0.7 sec
//App:Assets
//App:MyBuckSampleApp
//App:MyBuckSampleAppBinary
//App:MyBuckSampleAppPackage
//App:Resource
//App:workspace
//Pods:Alamofire

运行buck build //Pods:Alamofire可以看到它是否编译成功。

在原Target的BUCK文件中添加依赖

此时,便可以开始在主工程中添加Alamofire的依赖了:

apple_binary(
    name = "MyBuckSampleAppBinary",
    visibility = [
        "//App:",
        "//App/...",
    ],
    swift_version = "5",
    srcs = [
        "AppDelegate.swift",
        "ViewController.swift",
    ],
    configs = app_binary_configs("MyBuckSampleApp"),
    deps = [
        ":Resource",
        ":Assets",
        "//Pods:Alamofire",
    ]
)

可以看到在deps中,添加了"//Pods:Alamofire",这个Pods就是对应的Pods目录,Alamofire就是这个依赖的名字。至此已完成了依赖的添加。

验证

更新一下ViewController:

import UIKit
import Alamofire

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        let helloLabel = UILabel.init(frame: CGRect(x: 62, y: 180, width: 300, height: 32))
        helloLabel.text = "Hello World"        
        view.addSubview(helloLabel)
        let urlStr = "http://onapp.yahibo.top/public/?s=api/test/list"
        AF.request(URL(string: urlStr)!).responseJSON { (response) in
            switch response.result {
            case .success(let json):
                guard let dict = json as? Dictionary<String, Any> else {
                    return
                }
                guard let innderDict = dict["data"] as? Dictionary<String, Any> else {
                    return
                }
                guard let arr = innderDict["list"] as? Array<Any> else {
                    return
                }
                print(arr)
                helloLabel.text = "Hello World: \(arr.count)"
                break
            case .failure(let error):
                helloLabel.text = "Hello World: error"
                print("error:\(error)")
                break
            }
        }
    }
}

大功告成!!

Tips:

当使用IDE时,会出现一个编译错误:

Undefined symbols for architecture x86_64:
  "_swift_getFunctionReplacement", referenced from:
      _swift_getFunctionReplacement50 in libswiftCompatibilityDynamicReplacements.a(DynamicReplaceable.cpp.o)

需要将build setting 中 DEAD_CODE_STRIPPING 设置为 YES

相关文章

网友评论

      本文标题:BUCK - 使用BUCK编译iOS项目(2)

      本文链接:https://www.haomeiwen.com/subject/fcsgnctx.html