How do you archive Swift Package Dependencies to include in your XCFramework for binary distribution as a Swift Package?

Viewed 2222

Goal is to host a git repo for a Swift Package to distribute proprietary code as a binary in a XCFramework. The source code has dependencies on other Swift Packages. Listing the dependencies in the .package config is not enough. Simply asking consumers to import dependencies is not an option, as they may be making use of the same packages at different levels. The main problem I am trying to solve is how to resolve these dependencies when my Swift Package is imported into a project.

  1. Each dependency package should be archived and wrapped into the XCFramework as well, right?
  2. How would I do that?

The packages I am using are also available in a CocoaPod as well.

  1. Would it be easier to import them as pods for the purpose of archiving?

I have gone thru the following resources and SO posts, but can not find answer to this specific purpose.

2 Answers

I faced into same issue couple of months ago and haven't found a proper solution so I just started to use Cocoapods for distribution and this script for archiving my framework target which have some Cocoapods as a dependencies:

xcodebuild archive -workspace "../workspace_name.xcworkspace" \
  -scheme %scheme_name% \
  -sdk iphonesimulator \
  -archivePath "./SdkArchives/ios_simulators.xcarchive" \
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
  SKIP_INSTALL=NO
  
xcodebuild archive -workspace "../workspace_name.xcworkspace" \
  -scheme %scheme_name% \
  -sdk iphoneos \
  -archivePath "./SdkArchives/ios_devices.xcarchive" \
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
  SKIP_INSTALL=NO
  
rm -rf ../DevPods/SDK/framework_name.xcframework
  
xcodebuild -create-xcframework \
-framework ./SdkArchives/ios_devices.xcarchive/Products/Library/Frameworks/framework_name.framework \
-framework ./SdkArchives/ios_simulators.xcarchive/Products/Library/Frameworks/framework_name.framework \
-output ../DevPods/SDK/framework_name.xcframework

rm -rf SdkArchives

Than you can simply package this xcframework as a pod using this lines in your podspeck:

s.vendored_frameworks = 'framework_name.xcframework'
s.dependency 'framework_name_dependency'

Also you could check out this article, I haven't tried it but looks promising. https://kean.blog/post/xcframeworks-caveats

Related