You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This section describes the required steps to ensure that a TurboModule can be used as a Native Module.
6
+
import Tabs from '@theme/Tabs';
7
+
import TabItem from '@theme/TabItem';
8
+
import constants from '@site/core/TabsConstants';
9
+
import BetaTS from './\_markdown_beta_ts_support.mdx';
7
10
8
-
The section explains:
11
+
:::info
12
+
The creation of a backward compatible TurboModule requires the knowledge of how to create a TurboModule. To recall these concepts, have a look at this [guide](pillars-turbomodules).
9
13
10
-
- How to avoid installing dependencies when they are not needed
11
-
- The usage of compilation pragmas to avoid compiling code that requires types from the codegen
12
-
- API uniformity in JS, so that they don’t have to import different files
14
+
TurboModules only works when the New Architecture is properly setup. If you already have a library that you want to migrate to the New Architecture, have a look at the [migration guide](../new-architecture-intro) as well.
15
+
:::
16
+
17
+
Creating a backward compatible TurboModule lets your users continue leverage your library, independently from the architecture they use. The creation of such a module requires a few steps:
18
+
19
+
1. Configure the library so that dependencies are not installed in the old architecture.
20
+
1. Update the codebase so that the New Architecture types are not compiled when not available.
21
+
1. Uniform the JavaScript API so that your user code won't need changes.
22
+
23
+
While the last step is the same for all the platforms, the first two steps are different for iOS and Android.
24
+
25
+
## Configure the TurboModule Dependencies
26
+
27
+
### <aname="dependencies-ios" />iOS
28
+
29
+
The Apple platform installs TurboModules using [Cocoapods](https://cocoapods.org) as dependency manager.
30
+
31
+
Every TurboModule defines a `podspec` that looks like this:
The **goal** is to avoid installing the dependencies when the app is prepared for the Old Architecture.
74
+
75
+
When we want to install the dependencies we use the following commands, depending on the architecture:
76
+
77
+
```sh
78
+
# For the Old Architecture, we use:
79
+
pod install
80
+
81
+
# For the New Architecture, we use:
82
+
RCT_NEW_ARCH_ENABLED=1 pod install
83
+
```
84
+
85
+
Therefore, we can leverage this environment variable in the `podspec` to exclude the settings and the dependencies that are related to the New Architecture:
86
+
87
+
```diff
88
+
+ if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
89
+
# The following lines are required by the New Architecture.
This `if` guard prevents the dependencies from being installed when the environment variable is not set.
98
+
99
+
### Android
100
+
101
+
To create a module that can work with both architecture, you need to configure gradle to choose which files need to be compiled depending on the chosen architecture. This can be achieved by using different source sets in the gradle configuration.
102
+
103
+
:::note
104
+
This approach can lead to duplicated code, but it is the safest and cleanest way to achieve the desired result. You will see how to reduce the duplication in the next section.
105
+
:::
106
+
107
+
To configure the TurboModule so that it picks the proper sourceset, you have to update the `build.gradle` file in the following way:
108
+
109
+
```diff title="build.gradle"
110
+
+// Add this function in case you don't have it already
1. The first lines define a function that returns whether the New Architecture is enabled or not.
139
+
2. The `buildConfigField` line defines a build configuration boolean field called `IS_NEW_ARCHITECTURE_ENABLED`, and initialize it using the function declared in the first step.
140
+
3. The last lines leverage the function declared in step one to decide which source sets we need to build, depending on the choosen architecture.
141
+
142
+
## Update the codebase
143
+
144
+
### iOS
145
+
146
+
The second step is to instruct Xcode to avoid compiling all the lines using the New Architecture types and files when we are building an app with the Old Architecture.
147
+
148
+
The file to change is the module implementation file, which is usually a `<your-module>.mm` file. That file is structured as follow:
149
+
150
+
- Some `#import` statements, among which there is a `<GeneratedSpec>.h` file.
151
+
- The module implementation, using the various `RCT_EXPORT_xxx` and `RCT_REMAP_xxx` macros.
152
+
- The `getTurboModule:` function, which uses the `<MyModuleSpecJSI>` type, generated by The New Architecture.
153
+
154
+
The **goal** is to make sure that the `TurboModule` still builds with the Old Architecture. To achieve that, we can wrap the `#import "<GeneratedSpec>.h"` and the `getTurboModule:` function into an `#ifdef RCT_NEW_ARCH_ENABLED` compilation directive, as shown in the following example:
This snippet uses the same `RCT_NEW_ARCH_ENABLED` flag used in the previous [section](#dependencies-ios). When this flag is not set, Xcode skips the lines within the `#ifdef` during compilation and it does not include them into the compiled binary.
176
+
177
+
### Android
178
+
179
+
To create a backward compatible TurboModule you have two different source sets that are loaded and compiled depending on the proper architecture.
180
+
181
+
Therefore, you have to:
182
+
183
+
1. Create a Native Module in the `src/oldarch` path. See [this guide](../native-modules-intro) to learn how to create a Native Module.
184
+
2. Create a TurboModule in the `src/newarch` path. See [this guide](./pillars-turbomodules) to learn how to create a TurboModule
185
+
186
+
and then gradle could decide which implementation to pick.
187
+
188
+
Some files can be shared between a Native Module and a TurboModule: these should be created or moved into a folder that is loaded by both the architectures. These files are:
189
+
190
+
- the `<MyModule>Package.java` file used to load the module.
191
+
- a `<MyTurboModule>Impl.java` file where we can put the code that both the Native Module and the TurboModule has to execute.
192
+
193
+
The final folder structure looks like this:
194
+
195
+
```sh
196
+
my-module
197
+
├── android
198
+
│ ├── build.gradle
199
+
│ └── src
200
+
│ ├── main
201
+
│ │ ├── AndroidManifest.xml
202
+
│ │ └── java
203
+
│ │ └── com
204
+
│ │ └── MyModule
205
+
│ │ ├── MyModuleImpl.java
206
+
│ │ └── MyModulePackage.java
207
+
│ ├── newarch
208
+
│ │ └── java
209
+
│ │ └── com
210
+
│ │ └── MyModule.java
211
+
│ └── oldarch
212
+
│ └── java
213
+
│ └── com
214
+
│ └── MyModule.java
215
+
├── ios
216
+
├── js
217
+
└── package.json
218
+
```
219
+
220
+
The code that should go in the `MyModuleImpl.java` and that can be shared by the Native Module and the TurboModule is, for example:
221
+
222
+
```java title="example of MyModuleImple.java"
223
+
packagecom.MyModule;
224
+
225
+
importandroidx.annotation.NonNull;
226
+
importcom.facebook.react.bridge.Promise;
227
+
importjava.util.Map;
228
+
importjava.util.HashMap;
229
+
230
+
publicclassMyModuleImpl {
231
+
232
+
publicstaticfinalStringNAME="MyModule";
233
+
234
+
publicvoidfoo(doublea, doubleb, Promisepromise) {
235
+
// implement the logic for foo and then invoke promise.resolve or
236
+
// promise.reject.
237
+
}
238
+
}
239
+
```
240
+
241
+
Then, the Native Module and the TurboModule can be updated with the following steps:
242
+
243
+
1. Create a private instance of the `MyModuleImpl` class.
244
+
2. Initialize the instance in the module constructor.
245
+
3. Use the private instance in the modules methods.
246
+
247
+
For example, for a Native Module:
248
+
249
+
```java title="Native Module using the Impl module"
// NAME is a static variable, so we can access it using the class name.
292
+
returnMyModuleImpl.NAME;
293
+
}
294
+
295
+
@Override
296
+
publicvoidfoo(doublea, doubleb, Promisepromise) {
297
+
// Use the implementation instance to execute the function.
298
+
implementation.foo(a, b, promise);
299
+
}
300
+
}
301
+
```
302
+
303
+
For a step-by-step example on how to achieve this, have a look at [this repo](https://github.com/react-native-community/RNNewArchitectureLibraries/tree/feat/back-turbomodule).
304
+
305
+
## Unify the JavaScript specs
306
+
307
+
The last step makes sure that the JavaScript behaves transparently to chosen architecture.
308
+
309
+
For a TurboModule, the source of truth is the `Native<MyModule>.js` (or `.ts`) spec file. The app accesses the spec file like this:
310
+
311
+
```ts
312
+
importMyModulefrom'your-module/src/index';
313
+
```
314
+
315
+
The **goal** is to conditionally `export` from the `index` file the proper object, given the architecture chosen by the user. We can achieve this with a code that looks like this:
If you are using TypeScript and you want to follow the example, make sure to `export` the `NativeModule` in a separate `ts` file called `<MyModule>.ts`.
354
+
:::
355
+
356
+
Whether you are using Flow or TypeScript for your specs, we understand which architecture is running by checking whether the `global.__turboModuleProxy` object has been set or not.
357
+
358
+
:::caution
359
+
The `global.__turboModuleProxy` API may change in the future for a function that encapsulate this check.
360
+
:::
361
+
362
+
- If that object is `null`, the app has not enabled the TurboModule feature. It's running on the Old Architecture, and the fallback is to use the default [`NativeModule` implementation](../native-modules-intro).
363
+
- If that object is set, the app is running with the TurboModules enabled and it should use the `Native<MyModule>` spec to access the TurboModule.
0 commit comments