Embedding XCFramework in application with project framework dependencies
Asked Answered
I

6

10

I have an Xcode workspace which features a project with an iOS Application target, and another project with a Framework target. The framework target is dependent on another framework, which is integrated in the form of an xcframework:

  • MyApp
  • MyFramework
  • OtherFramework

Using regular OtherFramework.framework would require it to be linked to MyFramework and then embedded in MyApp even though MyApp doesn't require the framework itself. However when integrating with xcframework, this project then fails to build with a No such module 'OtherFramework' error.

Project settings:

MyFramework Project

MyApp Project

Removing OtherFramework.xcframework from the MyApp target fixes the build issue, but then causes library not loaded errors as the framework is not present in the application.

Demo project here: https://github.com/msaps/XCFramework-Link-Issue

How are you meant to link an xcframework in an application and link in a dependent framework?

Why?

pyckamil just posted this article which explains the issue in detail: Everything wrong with XCFrameworks.

It turns out Xcode has an optimisation for the ProcessXCFrameworkLibrary step which extracts the correct .framework from an .xcframework for the active build architecture. This is only run once which causes issues for additional targets that try to link the same framework.

Update

This issue is resolved in Xcode 12.0

Ingratiating answered 5/2, 2020 at 14:49 Comment(1)
What about if i have building a swift framework containing a other cocoapods ? . I did checkout your github repo and i can see that your OtherFramework.xcframework which is external framework contains both arm64 and x86_64 files in it. In my case when i do a pod install the three dependencies does not contain device support ?. Is this something xcode does it my itself ?. or i need to manually build my dependencies each of them for both device and sim and copy those into my swift project for it to build ? . Can you share a sample project with cocopods in it which builds ?Zoochemistry
I
3

UPDATED - Resolved in Xcode 12.0

shinsuk came up with a reliable workaround that works by adding architecture-explicit framework search paths to ensure the correct .framework within an XCFramework is found.

Details can be found in the README.

Search Paths

Ingratiating answered 1/6, 2020 at 8:15 Comment(3)
can you please share a github link to the solution you described above ?Zoochemistry
this issue is still persisting in xcode12.4 right? i am having the same issue and have to go this path to solve itElusive
Yup @MartinMlostek I'm having it also and this was indeed the fix. It seems XCode simply doesn't work well with xcframeworks, ironically.Phosphide
D
1

Check build settings and defining the Framework Search Paths to a folder which contains the frameworks in question. If the frameworks are placed in your project directory, simply set the framework search path to $(SRCROOT) and set it to recursive.

check the response Getting error "No such module" using Xcode, but the framework is there

Derbyshire answered 5/2, 2020 at 16:50 Comment(1)
The search paths are set correctly - both projects can individually find the xcframework without issue. The build errors occur when trying to link them both in a workspace, which is required to ensure the framework is embedded and linked correctly.Ingratiating
M
1

I had this issue as well after using xcframework instead of framework. So I changed my project structure:

The MyFramework Peoject embed OtherFramework.xcframework,Then make it exported using @_exported import OtherFramework in MyFramework Project. And the MyApp just link MyFramework but can both import/use MyFramework and OtherFramework.

BTW, It seems to custom the @rpath and manual codesign the OtherFramework.

Moor answered 13/4, 2020 at 4:17 Comment(0)
L
1

IMO, It seems not xcframework issue.

Check out this answer: https://mcmap.net/q/438352/-signing-sub-framework-embedded-in-root-framework-not-working-xcode-11-2-1

Umbrella frameworks are not supported on iOS, watchOS, or tvOS.

OtherFramework.xcframework should be signed and embedded in your host app.

and add "@executable_path/Frameworks" setting into your MyFramework.framework > Build settings > Runpath Search Paths.

Leaky answered 28/4, 2020 at 6:20 Comment(2)
None of the frameworks here are umbrella frameworks, the issue arises when trying to embed OtherFramework.xcframework in the host application while it is also linked in an another framework that the host app uses.Ingratiating
You are correct. It's not a umbrella framework issue. I also have same problem you experienced. Then I suggest a workaround. github.com/msaps/XCFramework-Link-Issue/pull/2Leaky
K
0

I had an issue like that as well. First, make sure if you have MyFramework.framework file within the same directory as MyApp.

Second, when building MyFramework.framework, make sure that OtherFramework.xcframework is as well in MyFramework's project directory.

And one more thing, check target SDK versions. They should be somewhere on the same level.

Kaye answered 5/2, 2020 at 16:7 Comment(1)
The MyFramework framework is a project within the workspace - they both have the same $(SRCROOT) and the framework search paths are set correctly. OtherFramework.xcframework can be found without an issue when it is linked only in one of the projects, but if I link it in both (and embed in MyApp), as required to properly include the framework, the build errors occur.Ingratiating
O
-1

I had the same issue as you, and after seeing your pbxproj I think it can be solved the same way.

Change your framework search path to recursive (either through UI or manually editing the pbxproj "$(SRCROOT)/../Frameworks" => "$(SRCROOT)/../Frameworks/**"), like so: https://github.com/msaps/XCFramework-Link-Issue/pull/1/files

Oid answered 21/4, 2020 at 15:3 Comment(3)
The link you included in your answer may point useful information, if it does, you should include this information in your answer and refrain from posting link-only answers. If the link breaks in the future your answer will have no value.Scrofula
@Scrofula The link points to a PR of a single line changes that is already in the answer in entirety.Oid
@Oid having checked this out - it potentially can workaround the issue in some projects. But it also can cause issues where the incorrect architecture is attempted to be linked from the XCFramework. I've updated the README to reflect this information.Ingratiating

© 2022 - 2024 — McMap. All rights reserved.