3

I'm trying to create a binary xcframework and I wanted it to have some SPM dependencies. Is it possible?

So far I'm facing the issue where I can't export my framework with a dependency because the BUILD_LIBRARY_FOR_DISTRIBUTION flag must be FALSE for the dependency to work. But in this case, I won't be able to generate the binary.

This is what I'm trying to do:

  1. Add a SPM as a dependency:

  2. Archive using xcodebuild:

    xcodebuild archive -project frameworkPOC.xcodeproj -scheme frameworkPOC -destination "generic/platform=iOS" -archivePath "archives/frameworkPOC"

  3. Generate binaries through:

    xcodebuild -create-xcframework -archive archives/frameworkPOC.xcarchive -framework frameworkPOC.framework -output xcframeworks/frameworkPOC.xcframework

So far so good... The exported framework works fine. But if I add a SPM such as Alamofire I get this warning:enter image description here

And when I export with BUILD_LIBRARY_FOR_DISTRIBUTION set to YES, the framework is broken as it seems Alamofire was not found.

So returning to my question: Is it possible to generate a binary framework that contains SPM dependencies?

1 Answer 1

1

It is absolutely possible to create an xcframework that requires dependencies. You would publish a Package.swift that described the dependencies, and the app would link them all, along with your xcframework.

It is not advisable (or directly supported) to create an xcframework that includes dependencies.

Consider the case that another framework (xcframework or otherwise) also included Alamofire. At best, this would include a redundant copy. At worst, if the required versions are different, the program would have undefined behavior and which version is used would be determined at load time. This is a fundamental problem of including dependencies. It exists in just about every system, including xcframeworks. There is no safe way to hide the fact that you require Alamofire from the consumer of your framework. You need to include that in your Package.swift.

There are tricky ways to include dependencies directly if you can absolutely ensure that no consuming package, or dependency of the consuming package, also includes the dependency. That can in some cases be worth the trouble for managing purely internal packages. But this would never be the case for a broadly used framework like Alamofire.

Sign up to request clarification or add additional context in comments.

3 Comments

Excellent explanation Rob! The Alamofire usage here is more to prove a concept. I can be more specific about why I need the dependencies inside my framework binary. I have a SPM which is in a private repository. Not everyone has access to it. What I want to do is create another xcframework binary that contains this SPM, so I can distribute it to another group of developers. I think I actually need to change my approach.
Ah; I've built solutions for clients in the past for this exact situation, and they work. When possible, however, I generally recommend (and have usually convinced my clients to adopt) the methods described above, or to restructure a bit to make the problem go away. It's just simpler to manage and you avoid future surprises (and avoid having to hire someone like me to manage it all for you... :D)
The place it can make sense is when you're distributing an xcframework to customers, and so it needs to it to be absolutely seamless for them, but you also have a complex internal package structure that is difficult to remove. That's when someone like me usually shows up to help, and the extra complexity can be worth the investment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.