How to create XCFramework using Fastlane

How to create XCFramework using Fastlane
How to create XCFramework using Fastlane

Are you still creating XCFramework by using xcodebuild command? Just Don’t, there is a better way to do it, and it is faster too. Use Fastlane and it will generate xcframework like a charm.

Before we start creating XCFramework a little background about Fastlane and XCFramework:

What is Fastlane?

Fastlane

It’s an iOS app automation tool, which can build, test, distribute to AppStore and can do lot more.

It actually runs the command provided by Apple, mostly xcodebuild command to perform all these operations.

We too can explicitly run Xcodebuild command however it’s not user friendly and we can’t automate it. It’s a manual process.

Fastlane runs Xcodebuild command by through a script written in Ruby.

Are you already familier with Fastlane and want to speed it up checkout this Top 5 Secrets to speed up Fastlane iOS

What is XCFramework?

XCFramework

XCFramework is newly introduced by Apple which will eventually replace Framework. It contains binary of different architecture variants. In simple terms it is a universal/fat library which includes all different variants supporting devices and simulators.

Even though the name XCFramework is quite similar to Framework it actually very different, you may be interested in know the difference between XCFramework vs Framework.

How To Create XCFramework using Fastlane?

Step-1: Install Fastlane

brew install fastlane

That’s all! The simplest way to install Fastlane. (If you don’t have brew install brew first )

You can follow this Fastlane setup guide too.

Step-2: Setup Fastfile in iOS/macOS project

Open your terminal and navigate to your project’s directory and run the following command:

fastlane init

It will create Fastfile which is the holy grail to Fastlane

Step-3: Write a lane to create xcframework

lane :buildXCFramework do |options|
create_xcframework(
    workspace: 'path/to/sample.xcworkspace',
    scheme: 'framework_scheme',
    product_name: 'Sample', 
    destinations: ['iOS', 'maccatalyst'],
    xcframework_output_directory: 'path/to/your/output_dir'
)
end

This is actually written by bielikb at github

This create_xcframework function actually builds Framework for both device and simulator. Then it creates XCFramework by using xcodebuild -create-xcframework

Step-4: Run the lane to get XCFramework

fastlane buildXCFramework

That’s All! It will finally say fastlane.tools finished successfully 🎉

You got your XCFraework.

Now that you have successfully created XCFramework, you must know few build settings which are necessary to set.

  • Build Libraries for Distribution = Yes
  • Skip Install = No
    • Build for active directory = Yes (recommended but not necessary)