概述 Expo 提供的用于开发原生模块的 API 和实用程序。
¥What is the Expo Modules API
Expo Modules API 允许你编写 Swift 和 Kotlin,以使用原生模块和视图为你的应用添加新功能。该 API 旨在利用现代语言功能,在两个平台上尽可能保持一致,需要最少的样板,并提供与 React Native 的 Turbo Modules API 相当的性能特性。Expo 模块全部支持新架构,并自动向后兼容使用旧架构的现有 React Native 应用。
¥The Expo Modules API allows you to write Swift and Kotlin to add new capabilities to your app with native modules and views. The API is designed to take advantage of modern language features, to be as consistent as possible on both platforms, to require minimal boilerplate, and provide comparable performance characteristics to React Native's Turbo Modules API. Expo Modules all support the New Architecture and are automatically backwards compatible with existing React Native apps using the old architecture.
我们相信使用 Expo Modules API 可以尽可能轻松地构建和维护几乎所有类型的 React Native 模块,并且我们认为 Expo Modules API 是绝大多数为其应用构建原生模块的开发者的最佳选择。
¥We believe that using the Expo Modules API makes building and maintaining nearly all kinds of React Native modules about as easy as it can be, and we think that the Expo Modules API is the best choice for the vast majority of developers building native modules for their apps.
¥Common questions
大多数时候,Expo 和 React Native 开发者不需要编写任何原生代码 - 库已经可用于广泛的用例,从相机到视频到地图到触觉等等。
¥Most of the time, Expo and React Native developers don't need to write any native code — libraries are already available for a wide range of use cases, from camera to video to maps to haptics and much more.
但有时,没有什么能完全满足你的需要。也许你想集成公司要求的分析服务,但该服务尚未有 React Native 库,因此你需要围绕其 SDK 构建一个模块。或者,你可能想要访问你的应用所需的系统功能,但不常用,因此没有人为其维护库。
¥But sometimes, nothing does exactly what you need. Maybe you want to integrate an analytics service that your company mandates but that doesn't yet have a React Native library yet, so you need to build a module around their SDK. Or maybe you want to access a system feature that your app requires, but isn't commonly used, so nobody maintains a library for it.
总结并解释 React Native 团队的建议:
¥To summarize and paraphrase the recommendation from the React Native team:
如果你打算在原生模块中使用 C++,请使用 Turbo Modules,因为它可以更轻松地访问更底层的机制。
¥If you intend to use C++ in your native module, use Turbo Modules since it provides easier access to lower-level mechanisms.
如果你正在寻找更好的开发者体验,并且愿意依赖模块中的 expo
包,请使用 Expo Modules API。
¥If you are looking for a better developer experience and you are willing to depend on the expo
package in your module, then use the Expo Modules API.
如果你想了解我们如何实现我们的库,Expo SDK 是一个很好的地方。另一个很好的资源是开源应用,例如 Bluesky。
¥The Expo SDK is a great place to look if you want to learn how we have implemented our libraries. Another great resource is open source apps, such as Bluesky.
以下库是我们在社区中最喜欢的一些库:
¥The following libraries are some of our favorites from the community:
将 Expo Modules API 添加到你的应用对你的应用大小的影响微乎其微,它可能会使大小增加几百千字节。在此博客文章中了解更多信息。
¥Adding the Expo Modules API to your app has a negligible impact on your app size, it may increase the size by a few hundred kilobytes. Learn more in this blog post.
Expo Modules API 具有与 React Native 的 Turbo Modules API 类似的性能特性。这两个 API 都利用了 React Native 的 JavaScript 接口 (JSI),而不是使用 JSON 消息队列 ("bridge") (了解有关 JSI 的更多信息) 的传统方法。
¥The Expo Modules API has similar performance characteristics to React Native's Turbo Modules API. Both APIs leverage React Native's JavaScript Interface (JSI), rather than the legacy approach of using a JSON message queue ("bridge") (learn more about JSI).
Expo 模块和 Turbo 模块都没有设计为尽可能快,但它们在重要的地方很快。例如,Expo Modules API 可以利用代码生成和新的原生 Swift / C++ 互操作来减少单个方法调用的开销。但是,这会带来一些开发者体验挑战和开销,而且我们还没有遇到任何用例,这种优化会提供任何有意义的实际性能改进。实际上,执行原生方法主体所花费的时间通常比方法调用的开销大几个数量级。Expo Modules 和 Turbo Modules 都可以轻松每秒执行数十万个原生方法调用,这远远超过你可能在任何应用中找到的调用,并且方法调用的开销不太可能成为瓶颈。
¥Neither Expo Modules nor Turbo Modules are designed to be as fast as technically possible, but rather they are fast where it matters. For example, the Expo Modules API could leverage code generation and the new native Swift / C++ interop to reduce the overhead of individual method calls. However, this imposes some developer experience challenges and overhead, and we have not yet encountered any use cases where such an optimization would provide any meaningful real-world performance improvements. In reality, the time spent executing the body of a native method is often orders of magnitude greater than the overhead of the method invocation. Both Expo Modules and Turbo Modules can easily execute hundreds of thousands of native method calls per second, which is well over what you are likely to find in any app, and the overhead of the method calls is unlikely to be the bottleneck.
如果你在使用 Expo Modules API 时遇到任何性能瓶颈,提交问题 和我们很乐意与你讨论。
¥If you encounter any performance bottlenecks with the Expo Modules API, file an issue and we'd be happy to discuss it with you.
Expo Modules API 对 macOS 和 tvOS 进行了实验性支持。查看 额外的平台支持 教程以了解更多信息。
¥The Expo Modules API has experimental support for macOS and tvOS. See Additional platform support tutorial for more information.
在 集成现有库 教程中了解有关此内容的更多信息。
¥Learn more about this in the Integrate an existing library tutorial.
¥Next steps
关于使用 Expo Modules API 创建保留设置的原生模块的教程。
关于使用 Expo Modules API 创建渲染 WebView 的原生视图的教程。
关于使用 Kotlin 和 Swfit 创建原生模块的参考。
关于 Expo Modules API 背后的设计考虑因素的概述。
可用配置选项的参考。