简体   繁体   English

如何在没有嵌入式二进制文件的情况下为 iOS 构建动态框架?

[英]How to build dynamic Framework for iOS with no Embedded Binaries?

I try to generate framework named AfTestFramework for ios.我尝试为 ios 生成名为AfTestFramework框架。 I created through Xcode Cocoa Touch Framework with some basic code, ran it and got:我使用一些基本代码通过 Xcode Cocoa Touch Framework创建,运行它并得到:

在此处输入图片说明

So far so good.到目前为止一切顺利。

I created new Test Application and drag @ dropped AfTestFramework.framework into the project, like crashlytics does (see below)我创建了新的测试应用程序并将AfTestFramework.framework拖到项目中,就像 crashlytics 一样(见下文)

After building project and running I got Library not loaded ... Reason: image not found :构建项目并运行后,我Library not loaded ...... Reason: image not found

dyld: Library not loaded: @rpath/AfTestFramework.framework/AfTestFramework Referenced from: /var/containers/Bundle/Application/9B12D0AD-94AA-4119-A5DE-6BCAA806FA9F/TestAvoidEmbedded1.app/TestAvoidEmbedded1 Reason: image not found dyld:库未加载:@rpath/AfTestFramework.framework/AfTestFramework 引用自:/var/containers/Bundle/Application/9B12D0AD-94AA-4119-A5DE-6BCAA806FA9F/TestAvoidEmbedded1.app/TestAvoidEmbedded1 原因:找不到图像

I know the solution is to attach this framework to Embedded Binaries but I look for the way to avoid this approach.我知道解决方案是将此框架附加到Embedded Binaries但我正在寻找避免这种方法的方法。

Further, It demands to create 2 Frameworks: for Simulator and for Release otherwise I cannot release application with my framework, that looks a bit messy and weird.此外,它需要创建 2 个框架:用于模拟器和用于发布,否则我无法使用我的框架发布应用程序,这看起来有点混乱和奇怪。

I found that following frameworks work without Embedded Binaries :我发现以下框架在没有Embedded Binaries情况下工作:

ans so on.等等。

So far I didn't find any description, I know also that its something new.到目前为止,我没有找到任何描述,我也知道它是新的东西。

I started to play with xcodebuild like:我开始玩xcodebuild像:

 xcodebuild -sdk "iphoneos10.1" "ARCHS=arm64 armv7 armv7s" "VALID_ARCHS=arm64 armv7 armv7s" "ONLY_ACTIVE_ARCH=NO" -target AfTestFramework  -configuration "Release"  clean build

but no success.但没有成功。

Can somebody spread the light on this problem?有人可以解决这个问题吗?

If you add a dynamic Framework to your iOS project, you must add it to the Embedded Binaries .如果将动态框架添加到 iOS 项目,则必须将其添加到Embedded Binaries In opposite to static libraries are dynamic libraries loaded at runtime.与静态库相反的是在运行时加载的动态库。 Thus the dynamic linker must access them on the device.因此动态链接器必须在设备上访问它们。

The only possible way to add the code inside of the framework without embedding the framework is to bind it statically to your app.在不嵌入框架的情况下在框架内添加代码的唯一可能方法是将其静态绑定到您的应用程序。 For that you should create a static library from the framework.为此,您应该从框架创建一个静态库。 (Probably, libtool (1) may help you with that.) But anyway you may run into other problems, if the framework needs its bundle structure for loading resources etc. (也许, libtool (1) 可以帮助你。)但无论如何你可能会遇到其他问题,如果框架需要它的包结构来加载资源等。

I'm not quite sure what you are trying to achieve but it sounds like you are trying to create a framework which has a dependency on another framework.我不太确定您想要实现什么,但听起来您正在尝试创建一个依赖于另一个框架的框架。

So when used, it would be something like app -> your-framework -> some-other-framework .因此,在使用时,它类似于app -> your-framework -> some-other-framework

I would strongly recommend looking at carthage as a solution to our problem.我强烈建议将迦太基视为我们问题的解决方案。 Carthage is an OS X native dependency manager that is designed to manage dependencies for you. Carthage 是一个 OS X 原生依赖管理器,旨在为你管理依赖。

Firstly you need to check that the framework your framework uses is Carthage friendly.首先,您需要检查您的框架使用的框架是否对 Carthage 友好。 For now I'll assume so.现在我假设是这样。

In your frameworks project you would add a Cartfile where you declare your dependency on the other framework, then use Carthage to download and build the other framework.在您的框架项目中,您将添加一个Cartfile ,在其中声明您对另一个框架的依赖,然后使用 Carthage 下载并构建另一个框架。 You then add the built framework to your project as you would with any Apple supplied framework.然后,您将构建的框架添加到您的项目中,就像使用 Apple 提供的任何框架一样。 You DO NOT need to embed the framework and generally speaking it's not recommended to do so.并不需要嵌入框架,一般来说它不建议这样做。 Carthage will handle this issue.迦太基会处理这个问题。

In the app project you would then also add a Cartfile , but you only need to specify your project in it.在 app 项目中,您还将添加一个Cartfile ,但您只需要在其中指定您的项目。 When Carthage builds the framework, it will automatically locate the other framework, download and build it as well.当 Carthage 构建框架时,它会自动定位其他框架,下载并构建它。 Then in the apps build phases you simple specify linking agains both frameworks and add a carthage-copy script phase to include them in the Frameworks directory of the finished app.然后在应用程序构建阶段,您只需指定重新链接两个框架并添加一个carthage-copy脚本阶段以将它们包含在已完成应用程序的Frameworks目录中。

With regard to building for simulator and release.关于构建模拟器和发布。 You don't need to build multiple targets.您不需要构建多个目标。 For a start, using Carthage means that the frameworks are only built when you need them, so this removes any requirement for you to supply binaries.首先,使用 Carthage 意味着框架仅在您需要时才构建,因此这消除了您提供二进制文件的任何要求。

When telling Carthage to build the dependencies, by default it builds Release binaries.当告诉 Carthage 构建依赖项时,默认情况下它会构建Release二进制文件。 Building Debug frameworks for testing is just a matter of add --configuration Debug to Carthage's command line arguments.构建用于测试的调试框架只是将--configuration Debug添加到 Carthage 的命令行参数的问题。

Finally - to be fair, there are also two other package managers out there: Ruby based CocoaPods which is an older manager and for Swift, the Swift Package Manager.最后 - 公平地说,还有另外两个包管理器:基于 Ruby 的 CocoaPods,它是一个较老的管理器,以及 Swift 的 Swift 包管理器。 Personally I prefer Carthage for reasons I explain in a blog post here .我个人更喜欢迦太基,原因我在这里的博客文章中解释

这是旧的,但如果它对将来的参考有用:你可以做

$lipo -create <path to simulator framework> <path to device framework> -output <path to the output framework>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM