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

Do I need to know the Expo Modules API to build an Expo / React Native app?

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.

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.

When should I use Turbo Modules and when should I use the Expo Modules API?

To summarize and paraphrase the recommendation from the React Native team:

  • If you intend to use C++ in your native module, use Turbo Modules since it provides easier access to lower-level mechanisms.
  • 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.
Where can I find open source Expo Modules to learn from?

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:

What impact does using the Expo Modules API have on my app size?

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.

What impact does using the Expo Modules API have on my app's performance?

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).

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.

If you encounter any performance bottlenecks with the Expo Modules API, file an issue and we'd be happy to discuss it with you.

Does the Expo Modules API support platforms other than Android, iOS, and web?

The Expo Modules API has experimental support for macOS and tvOS. See Additional platform support tutorial for more information.

How can I use the Expo Modules API to make a third-party SDK available to my Expo app?

Learn more about this in the Integrate an existing library tutorial.

下一步

¥Next steps

教程:创建原生视图

关于使用 Expo Modules API 创建保留设置的原生模块的教程。

了解应用大小

关于使用 Expo Modules API 创建渲染 WebView 的原生视图的教程。

Expo 模块 API:参考

关于使用 Kotlin 和 Swfit 创建原生模块的参考。

Expo 模块 API:设计注意事项

关于 Expo Modules API 背后的设计考虑因素的概述。

expo-module.config.json

可用配置选项的参考。