Skip to main content

连接库

并非每个应用都使用所有原生功能,并且包含支持所有这些功能的代码会影响二进制大小......但我们仍然希望在你需要时支持添加这些功能。

¥Not every app uses all the native capabilities, and including the code to support all those features would impact the binary size... But we still want to support adding these features whenever you need them.

考虑到这一点,我们将其中许多功能公开为独立的静态库。

¥With that in mind we exposed many of these features as independent static libraries.

对于大多数库来说,它就像拖动两个文件一样快,有时需要第三步,但仅此而已。

¥For most of the libs it will be as quick as dragging two files, sometimes a third step will be necessary, but no more than that.

注意

我们随 React Native 提供的所有库都位于存储库根目录的 Libraries 文件夹中。其中一些是纯 JavaScript,你只需对其进行 require 即可。其他库也依赖于一些原生代码,在这种情况下,你必须将这些文件添加到你的应用中,否则一旦你尝试使用该库,应用就会抛出错误。

¥All the libraries we ship with React Native live in the Libraries folder in the root of the repository. Some of them are pure JavaScript, and you only need to require it. Other libraries also rely on some native code, in that case you'll have to add these files to your app, otherwise the app will throw an error as soon as you try to use the library.

¥Here are the few steps to link your libraries that contain native code

自动链接

¥Automatic linking

安装具有原生依赖的库:

¥Install a library with native dependencies:

npm install <library-with-native-dependencies> --save
信息

--save--save-dev 标志对于此步骤非常重要。React Native 将根据 package.json 文件中的 dependenciesdevDependencies 链接你的库。

¥--save or --save-dev flag is very important for this step. React Native will link your libs based on dependencies and devDependencies in your package.json file.

就是这样!下次你构建应用时,由于 autolinking 机制,原生代码将被链接。

¥That's it! Next time you build your app the native code will be linked thanks to the autolinking mechanism.

手动链接

¥Manual linking

步骤 1

¥Step 1

如果库有原生代码,则其文件夹内必须有一个 .xcodeproj 文件。将此文件拖到你在 Xcode 上的项目中(通常在 Xcode 上的 Libraries 组下);

¥If the library has native code, there must be an .xcodeproj file inside its folder. Drag this file to your project on Xcode (usually under the Libraries group on Xcode);

步骤 2

¥Step 2

单击主项目文件(代表 .xcodeproj 的文件),选择 Build Phases 并将静态库从要导入的库内的 Products 文件夹拖动到 Link Binary With Libraries

¥Click on your main project file (the one that represents the .xcodeproj) select Build Phases and drag the static library from the Products folder inside the Library you are importing to Link Binary With Libraries

步骤 3

¥Step 3

并不是每个库都需要这一步,你需要考虑的是:

¥Not every library will need this step, what you need to consider is:

我需要在编译时知道库的内容吗?

¥Do I need to know the contents of the library at compile time?

这意味着,你是在原生端使用这个库还是仅在 JavaScript 中使用该库?如果你只在 JavaScript 中使用它,那么就可以开始了!

¥What that means is, are you using this library on the native side or only in JavaScript? If you are only using it in JavaScript, you are good to go!

如果你确实需要从原生调用它,那么我们需要知道库的标头。为此,你必须转到项目文件,选择 Build Settings 并搜索 Header Search Paths。你应该在其中包含库的路径。(本文档曾经建议使用 recursive,但现在不再建议这样做,因为它可能会导致微妙的构建失败,尤其是对于 CocoaPods。)

¥If you do need to call it from native, then we need to know the library's headers. To achieve that you have to go to your project's file, select Build Settings and search for Header Search Paths. There you should include the path to your library. (This documentation used to recommend using recursive, but this is no longer recommended, as it can cause subtle build failures, especially with CocoaPods.)