background

As your business expands, there are more private and third-party CocoaPod libraries and more files in your project. The recompilation process takes a long time each time pod install/update is performed, which indirectly introduces the need to speed up the compilation. Binarization refers to the process of converting the source code of a module into a static or dynamic library by compilation to speed up the compilation of that component in a project.

The specific implementation

  • To create an Xcode project, select the Framework or Static Library template
  • Add Cocoapods support for switching source code during debugging
  • Added Carthage support for binaries
  • Add PodSpecs for managing associated binary libraries

Cocoapods

We used Cocoapods to split each component into a separate POD library.

Creating a component project

pod lib create MyLib
Copy the code

Edit podspec file

Pod::Spec.new do |s|
  s.name             = 'MyLib'
  s.version          = '0.1.0 from'
  s.summary          = 'A short description of MyLib.'
  s.description      = <<-DESC TODO: Add long description of the pod here. DESC
  s.homepage         = 'https://github.com/wkcdeie/MyLib'
  s.license          = { :type= >'MIT'.:file= >'LICENSE' }
  s.author           = { 'wkcdeie'= >'[email protected]' }
  s.source           = { :git= >'https://github.com/wkcdeie/MyLib.git'.:tag => s.version.to_s }
  s.ios.deployment_target = '8.0'
  s.source_files = 'MyLib/Classes/**/*'
end
Copy the code

validation

pod lib lint MyLib.podspec
Copy the code

Push code to remote repository

Git tag '0.0.1' git pushCopy the code

Carthage

Carthage is a simple, decentralized dependency management tool.

The installation

brew install carthage
Copy the code

use

mkdir my-vendors
cd ~/my-vendors
touch Cartfile
Copy the code

Edit Cartfile

# # git "https://github.com/SVProgressHUD/SVProgressHUD.git" > ~ 1.0 or making "SVProgressHUD/SVProgressHUD" ~ > 1.0Copy the code

Rely on the source

  • githubCarthage: Tell Carthage to go to Github and download the file as Username/ProjectName.
  • git: database address, which can be a remote URL. Use git://, http://, SSH :// or the local database address.

The version number

  • = = 1.0Indicates that version 1.0 is used
  • > = 1.0Indicates that version 1.0 or later is used
  • ~ > 1.0Indicates that version 1.0 is used but version 2.0 is used
  • branch,tag,commitRepresents the use of specific branches, labels, and commit information

run

carthage update --platform iOS --no-use-binaries
Copy the code
  • --platform iOSOnly the iOS platform is built
  • --no-use-binariesDo not use libraries already built online and rebuild directly from local source

When this command is executed, Carthage/Checkouts and Carthage/Build are generated in the current directory. Checkouts stores the downloaded source code, and builds store the built binaries. As shown in figure:

Use attention

Since Swift does not currently have Module Stability, it is best not to add Carthage/Build to the code base. After each dependency pull, perform the Carthage Build operation again using the current computer’s Swift environment.

Create a binary library Pod project

pod lib create MyVendors
Copy the code

Edit podspec file

Pod::Spec.new do |s|
  s.name             = 'MyVendors'
  s.version          = '0.1.0 from'
  s.summary          = 'A short description of MyVendors.'
  s.description      = <<-DESC TODO: Add long description of the pod here. DESC
  s.homepage         = 'https://github.com/wkcdeie/MyVendors'
  s.license          = { :type= >'MIT'.:file= >'LICENSE' }
  s.author           = { 'wkcdeie'= >'[email protected]' }
  s.source           = { :git= >'https://github.com/wkcdeie/MyVendors.git'.:tag => s.version.to_s }
  s.ios.deployment_target = '8.0'

  s.subspec "SVProgressHUD" do |sp|
    sp.framework = 'QuartzCore'
    if ENV['IS_SOURCE']
      puts 'Notice: SVProgressHUD is source now'
      sp.dependency 'SVProgressHUD'.'~ > 2.2.5'
    else
      puts 'Notice: SVProgressHUD is binary now'
      sp.vendored_frameworks = 'Carthage/Build/iOS/SVProgressHUD.framework'
    end
  end
end
Copy the code

Add this to your main project Podfile:

pod 'MyVendors'.:subspecs= > ['SVProgressHUD'].:path= >'.. /my-vendors'
Copy the code

Execute pod Install and the entire operation is complete. If you need source code debugging, use IS_SOURCE=1 pod install.

Afterword.

  • If the project uses Swift and Xcode creates a Framework project, add this to your main project Podfileuse_frameworksThe tag.
  • If the number of dynamic libraries in a project is high, this can affect project startup time. According to Apple’s recommendation, the project should not exceed6Dynamic library, except system library. The solution to this problem is to use static libraries.

Reprint my blog