Expo 模块 API:概述
概述 Expo 提供的用于开发原生模块的 API 和实用程序。
什么是 Expo Modules 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 模块 API 才能构建 Expo / React Native 应用吗?
大多数情况下,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.
我应该什么时候使用 Turbo 模块,什么时候使用 Expo 模块 API?
总结并改述 React Native 团队的建议:
🌐 To summarize and paraphrase the recommendation from the React Native team:
- 如果你打算在本地模块中使用 C++,请使用 Turbo Modules,因为它提供了更方便地访问底层机制的方法。
- 如果你希望获得更好的开发者体验,并且愿意在你的模块中依赖
expo包,那么请使用 Expo 模块 API。
我在哪里可以找到开源的 Expo 模块来学习?
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 模块 API 会对我的应用大小产生什么影响?
将 Expo 模块 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 模块 API 会对我的应用性能产生什么影响?
Expo 模块 API 与 React Native 的 Turbo 模块 API 具有类似的性能特性。两者都利用了 React Native 的 JavaScript 接口(JSI),而不是使用传统的 JSON 消息队列(“桥接”)的方法(了解更多关于 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 模块 API 可以利用代码生成以及新的原生 Swift / C++ 互操作来减少单个方法调用的开销。然而,这会带来一些开发体验上的挑战和额外负担,而且我们尚未遇到任何使用场景能够通过这种优化获得显著的实际性能提升。实际上,执行原生方法主体所花费的时间通常远大于方法调用本身的开销。无论是 Expo 模块还是 Turbo 模块,都可以轻松地每秒执行数十万次原生方法调用,这远远超过任何应用中可能出现的情况,因此方法调用的开销不太可能成为性能瓶颈。
🌐 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 支持除 Android、iOS 和网页之外的平台吗?
Expo 模块 API 对 macOS 和 tvOS 提供实验性支持。有关更多信息,请参见其他平台支持教程。
🌐 The Expo Modules API has experimental support for macOS and tvOS. See Additional platform support tutorial for more information.
我如何使用 Expo Modules API 将第三方 SDK 提供给我的 Expo 应用?
在集成现有库教程中了解更多信息。
🌐 Learn more about this in the Integrate an existing library tutorial.
下一步
🌐 Next steps
关于使用 Expo Modules API 创建保留设置的原生模块的教程。
关于使用 Expo Modules API 创建渲染 WebView 的原生视图的教程。
有关使用 Kotlin 和 Swift 创建原生模块的参考资料。
关于 Expo Modules API 背后的设计考虑因素的概述。
可用配置选项的参考。