XCFramework is an official apple recommended and supported format that makes it easier to represent a distribution binary for multiple platforms and architectures. It requires XCode11 and above.

Making a static library

We will first create a static library project LYKitDemo clone to the local, and then enter the LYKitDemo directory.

Simulator architecture

Start by compiling the project into the iPhone Ulator architecture

bel@beldeMacBook-Pro LYKitDemo % xcodebuild archive -project 'LYKitDemo.xcodeproj' \ -scheme 'LYKitDemo' \ -configuration Release \ -destination 'generic/platform=iOS Simulator' \ -archivePath '.. /archives/LYKitDemo.framework-iphonesimulator.xcarchive' \ SKIP_INSTALL=NOCopy the code
  • xcodebuild: The command actually used in Xcode.
  • archive: packaging.
  • project: Project name.
  • scheme: Select Scheme.
  • configuration: In which environment.
  • destinationThe iOS Simulator is currently specified for the platform to distribute.
  • archivePath: Indicates the compressed path.
  • SKIP_INSTALL=NO: If set to YES, the generated framwork file will not be stored in the Products directory.

Real machine architecture

Next, let’s compile the real machine architecture

bel@beldeMacBook-Pro LYKitDemo % xcodebuild archive -project 'LYKitDemo.xcodeproj' \ -scheme 'LYKitDemo' \ -configuration Release \ -destination 'generic/platform=iOS' \ -archivePath '.. /archives/LYKitDemo.framework-iphoneos.xcarchive' \ SKIP_INSTALL=NOCopy the code

This generates the package files for the simulator and the real machine

In the Xcarchive file, under the Product folder, the corresponding library files are stored.

Lipo command merge

Next, we merge the frameworks under the two architectures, using lipo command

lipo -output LYKitDemo -create .. /archives/LYKitDemo.framework-iphoneos.xcarchive/Products/Library/Frameworks/LYKitDemo.framework/LYKitDemo .. /archives/LYKitDemo.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/LYKitDemo.framework/LYKitDemoCopy the code
  • -output: Output name.
  • The path to the file to merge.

A architectures have the same arm64 and can’t be in the same fat output file

fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: .. /archives/LYKitDemo.framework-iphoneos.xcarchive/Products/Library/Frameworks/LYKitDemo.framework/LYKitDemo and .. /archives/LYKitDemo.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/LYKitDemo.framework/LYKitDemo have the same architectures (arm64) and can't be in the same fat output fileCopy the code

This is because the static libraries in the simulator architecture have ARM64, and the static libraries in the real architecture also have ARM64, so they cannot be merged because they have the same architecture. We extracted the X86_64 schema from the static library file so that there was only one schema and no duplication.

bel@beldeMacBook-Pro LYKitDemo % lipo -output LYKitDemo-x86_64 -extract x86_64 .. /archives/LYKitDemo.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/LYKitDemo.framework/LYKitDemoCopy the code

Then we merge. Under the same directory of archives, we create lipo folder and store the output in this directory.

bel@beldeMacBook-Pro lipo % lipo -output LYKitDemo -create .. /archives/LYKitDemo.framework-iphoneos.xcarchive/Products/Library/Frameworks/LYKitDemo.framework/LYKitDemo .. /LYKitDemo/LYKitDemo-x86_64Copy the code

Next, we need to configure header files and resource files for the static library file, which is a bit tedious. Use the lipo command to create static inventory in two problems:

1. Two static libraries with the same architecture cannot be merged.

2. Configuring headers and resource files is cumbersome.

XCFramework

Compared to traditional frameworks:

  • 1. Distribution binaries for multiple platforms can be provided with a single.XCFramework file;
  • 2, andFat HeaderIn contrast, it can be divided by platform and can contain different platform files for the same architecture.
  • 3. In use, there is no need to peel off unnecessary architectural systems through scripts.

Create xcframework

Next we create an XCFramework

bel@beldeMacBook-Pro xcframework % xcodebuild -create-xcframework \ -framework '.. /archives/LYKitDemo.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/LYKitDemo.framework' \ -debug-symbols '/Users/bel/Desktop/gitBox/Examples/archives/LYKitDemo.framework-iphonesimulator.xcarchive/dSYMs/LYKitDemo.framework.dSY M' \ -framework '.. /archives/LYKitDemo.framework-iphoneos.xcarchive/Products/Library/Frameworks/LYKitDemo.framework' \ -debug-symbols '/Users/bel/Desktop/gitBox/Examples/archives/LYKitDemo.framework-iphoneos.xcarchive/BCSymbolMaps/0C1C6181-F2E0-3D17-8573 -65CC6AEDBD97.bcsymbolmap' \ -debug-symbols '/Users/bel/Desktop/gitBox/Examples/archives/LYKitDemo.framework-iphoneos.xcarchive/BCSymbolMaps/7D66A732-3121-386D-8397 -5F44DEA908F1.bcsymbolmap' \ -debug-symbols '/Users/bel/Desktop/gitBox/Examples/archives/LYKitDemo.framework-iphoneos.xcarchive/dSYMs/LYKitDemo.framework.dSYM' \ -output 'LYKitDemo.xcframework' xcframework successfully written out to: /Users/bel/Desktop/gitBox/Examples/xcframework/LYKitDemo.xcframeworkCopy the code
  • -framework: Path to the framework
  • -debug-symbols: debug symbol, which must be an absolute path
  • -output: Output position

So we have thetaXcframework file.Static libraries created using THE XCFramework do not appear to have duplicate schemas and also have header information

In this case I encountered oneNo 'swiftinterface' files found withinError, this needs to be placed in the project fileBUILD_FOR_LIBRARIES_FOR_DISTRIBUTIONSet toYES, and then, recompile, and you’re done.

Using xcframework

Create a new project and add lykitdemo. xcFramework to the Frameworks

Then, import the header file and you can use it

import UIKit import LYKitDemo class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() LYPerson.run() } } LYXCFrameworkTe[7770:14236252] Running......Copy the code

When we drag a file into Xcode, Xcode selects the file for the schema that we’re running on. If we are running an emulator, only x86 files will be copied. If we are running a real machine, only ARM64 files will be copied to reduce the size of the App package.

So we use the XCFramework to do the static library. Compared to LIPo, the XCFramework has several advantages:

  • 1. No need to deal with header files.
  • 2. No duplication of architecture.
  • 3. When linking, the corresponding architecture will be automatically selected, so there is no need to copy all of it to the App, which reduces the size of the App.

If you use Xcode to create static libraries, you can refer to this article XCode12 to create Swift and OC mixed static libraries