xcode 如何在不暴露源代码的情况下分发 Swift 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25020783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:18:22  来源:igfitidea点击:

How to distribute Swift Library without exposing the source code?

iosxcodeswift

提问by Jimmy

The first thing I tried is to create a static librarybut later I found out that it's not supported yet. Apple Xcode Beta 4 Release Notes:

我尝试的第一件事是创建一个静态库,但后来我发现它尚不受支持。Apple Xcode Beta 4 发行说明:

Xcode does not support building static libraries that include Swift code. (17181019)

Xcode 不支持构建包含 Swift 代码的静态库。(17181019)

I was hoping that Apple will be able to add this in the next Beta release or the GA version but I read the following on their blog:

我希望 Apple 能够在下一个 Beta 版本或 GA 版本中添加此功能,但我在他们的博客上阅读了以下内容:

While your app's runtime compatibility is ensured, the Swift language itself will continue to evolve, and the binary interface will also change. To be safe, all components of your app should be built with the same version of Xcode and the Swift compiler to ensure that they work together.

This means that frameworks need to be managed carefully. For instance, if your project uses frameworks to share code with an embedded extension, you will want to build the frameworks, app, and extensions together. It would be dangerous to rely upon binary frameworks that use Swift — especially from third parties. As Swift changes, those frameworks will be incompatible with the rest of your app. When the binary interface stabilizes in a year or two, the Swift runtime will become part of the host OS and this limitation will no longer exist.

在确保您的应用程序的运行时兼容性的同时,Swift 语言本身会不断发展,二进制接口也会发生变化。为安全起见,您的应用程序的所有组件都应使用相同版本的 Xcode 和 Swift 编译器构建,以确保它们协同工作。

这意味着需要谨慎管理框架。例如,如果您的项目使用框架与嵌入式扩展共享代码,您将希望一起构建框架、应用程序和扩展。依赖使用 Swift 的二进制框架是很危险的——尤其是来自第三方的。随着 Swift 的变化,这些框架将与您应用程序的其余部分不兼容。当二进制接口在一两年内稳定下来时,Swift 运行时将成为主机操作系统的一部分,这种限制将不再存在。

The news is really alarming for me a person who writes components for other developers to use and include in their apps. Is this means that I have to distribute the source code or wait for two years?. Is there any other way to distribute the library without exposing the code (company policy)?

对于一个为其他开发人员编写组件供其他开发人员使用和包含在他们的应用程序中的人来说,这个消息真的很令人震惊。这是否意味着我必须分发源代码或等待两年?。有没有其他方法可以在不暴露代码的情况下分发库(公司政策)?

Update:

更新:

Is Swift code obfuscation an option at this point ?

Swift 代码混淆在这一点上是一种选择吗?

采纳答案by rickster

Swift is beta now, and even for 1.0 Apple has been pretty clear they're after a restricted feature set -- better to do a small number of things well than to try to do everything.

Swift 现在是 Beta 版,即使是 1.0,Apple 也很清楚他们追求的是受限制的功能集 - 最好将少数事情做好而不是尝试做所有事情。

So for now, there's no way to distribute binary static libraries. Presumably that'll change sometime after Swift 1.0. For now, you can:

所以现在,没有办法分发二进制静态库。据推测,这会在 Swift 1.0 之后的某个时候改变。目前,您可以:

  • Distribute source
  • Ship a binary framework(instead of a library) if you're okay with the ABI being fragile
  • Use ObjC for library code
  • 分发源
  • 如果您对 ABI 的脆弱性感到满意,请提供二进制框架(而不是库)
  • 使用 ObjC 编写库代码

You can always combine approaches, too: e.g., implement the critical (secret) details of your library in ObjC, and ship Swift source that wraps it in a nice Swift API.

您也可以随时组合方法:例如,在 ObjC 中实现库的关键(秘密)细节,并提供将其包装在一个很好的 Swift API 中的 Swift 源代码。

Obfuscating code written in a language that's very much subject to change sounds like a recipe for a maintenance nightmare.

用一种很容易改变的语言编写的混淆代码听起来像是维护噩梦的秘诀。

回答by George

I believe the whole approach is wrong. You cannot do something that is not (yet) doable by the technology you are trying to use.

我相信整个方法都是错误的。你不能做一些你正在尝试使用的技术(还)不可行的事情。

Rationale: Swift is a new language, currently in Beta, and therefore changing. As I see it, this fact means not only that you are not able to ship static libraries, but that (real) developers will not be actually use third-party static libraries. What's the actual use of a library that may not work in the next release of the compiler? The issue gets bigger if you whant to use more than one library, because they might not be compatible! Therefore, even if you would be able to ship static libraries, they wouldn't be actually useful for production environment. So, what's the point?

基本原理:Swift 是一种新语言,目前处于 Beta 版,因此会发生变化。在我看来,这个事实不仅意味着您不能发布静态库,而且(真正的)开发人员实际上不会使用第三方静态库。在编译器的下一个版本中可能无法使用的库的实际用途是什么?如果您想使用多个库,问题就会变得更大,因为它们可能不兼容!因此,即使您能够发布静态库,它们对于生产环境也没有实际用处。那么,有什么意义呢?

Suggestion: write your static libraries in Objective-C (or C or whatever "non-beta"). Developers who need third-party libraries (e.g. yours) shouldn't expect them to be written in Swift until Swift is stable. You don't use experimental materials to build real bridges, right? You use well-tested, predictable ones.

建议:用 Objective-C(或 C 或任何“非测试版”)编写静态库。在 Swift 稳定之前,需要第三方库(例如您的)的开发人员不应期望它们是用 Swift 编写的。你不会使用实验材料来建造真正的桥梁,对吧?您使用经过充分测试的、可预测的。

回答by yoAlex5

From Xcode 9beta 4, Xcode supports static librarywith Swiftsources.

Xcode的9测试4时,Xcode支持static librarySwift源。