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
feat: autolinking for the new architecture on Android (#1603)
* feat: generate new arch files with hardcoded values
* feat: add libraryName to android dependency config
* feat: add componentNames and componentDescriptors to android dependency config
* feat: add androidMkPath to android dependency config
* fix: remove duplicate component names
* chore: run generateNewArchitectureFiles when new architecture is turned on
* feat: use only componentDescriptors, take interfacesOnly into account
* docs: document new architecture autolinking
* chore: add comments
* fix: TurboModule support and indentation
* chore: prettify mk output; remove jni sourceset; extract variables
* feat: make new config nullable
* chore: update docs on how to disable new arch autolinking
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ And then:
39
39
40
40
```sh
41
41
cd /my/new/react-native/project/
42
-
yarn link "@react-native-community/cli-platform-ios""@react-native-community/cli-platform-android""@react-native-community/cli""@react-native-community/cli-server-api""@react-native-community/cli-types""@react-native-community/cli-tools""@react-native-community/cli-debugger-ui""@react-native-community/cli-hermes""@react-native-community/cli-plugin-metro"
42
+
yarn link "@react-native-community/cli-platform-ios""@react-native-community/cli-platform-android""@react-native-community/cli""@react-native-community/cli-server-api""@react-native-community/cli-types""@react-native-community/cli-tools""@react-native-community/cli-debugger-ui""@react-native-community/cli-hermes""@react-native-community/cli-plugin-metro""@react-native-community/cli-clean"
43
43
```
44
44
45
45
Once you're done with testing and you'd like to get back to regular setup, run `yarn unlink` instead of `yarn link` from above command. Then `yarn install --force`.
Copy file name to clipboardExpand all lines: docs/autolinking.md
+28Lines changed: 28 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,7 @@ yarn react-native run-android
16
16
That's it. No more editing build config files to use native code.
17
17
18
18
Also, removing a library is similar to adding a library:
19
+
19
20
```sh
20
21
# uninstall
21
22
yarn remove react-native-webview
@@ -48,6 +49,10 @@ The [native_modules.gradle](https://github.com/react-native-community/cli/blob/m
48
49
1. At build time, before the build script is run:
49
50
1. A first Gradle plugin (in `settings.gradle`) runs `applyNativeModulesSettingsGradle` method. It uses the package metadata from `react-native config` to add Android projects.
50
51
1. A second Gradle plugin (in `app/build.gradle`) runs `applyNativeModulesAppBuildGradle` method. It creates a list of React Native packages to include in the generated `/android/build/generated/rn/src/main/java/com/facebook/react/PackageList.java` file.
52
+
1. When the new architecture is turned on, the `generateNewArchitectureFiles` task is fired, generating `/android/build/generated/rn/src/main/jni` directory with the following files:
53
+
-`Android-rncli.mk` – creates a list of codegen'd libs. Used by the project's `Android.mk`.
54
+
-`rncli.cpp` – registers codegen'd Turbo Modules and Fabric component providers. Used by `MainApplicationModuleProvider.cpp` and `MainComponentsRegistry.cpp`.
55
+
-`rncli.h` - a header file for `rncli.cpp`.
51
56
1. At runtime, the list of React Native packages generated in step 1.2 is registered by `getPackages` method of `ReactNativeHost` in `MainApplication.java`.
52
57
1. You can optionally pass in an instance of `MainPackageConfig` when initializing `PackageList` if you want to override the default configuration of `MainReactPackage`.
53
58
@@ -97,6 +102,27 @@ module.exports = {
97
102
};
98
103
```
99
104
105
+
## How can I disable autolinking for new architecture (Fabric, TurboModules)?
106
+
107
+
It happens that packages come with their own linking setup for the new architecture. To disable autolinking in such cases (currently `react-native-screens`, `react-native-safe-area-context`, `react-native-reanimated`, `react-native-gesture-handler`), update your `react-native.config.js`'s `dependencies` entry to look like this:
108
+
109
+
```js
110
+
// react-native.config.js
111
+
module.exports= {
112
+
dependencies: {
113
+
'fabric-or-tm-library': {
114
+
platforms: {
115
+
android: {
116
+
libraryName:null,
117
+
componentDescriptors:null,
118
+
androidMkPath:null,
119
+
},
120
+
},
121
+
},
122
+
},
123
+
};
124
+
```
125
+
100
126
## How can I autolink a local library?
101
127
102
128
We can leverage CLI configuration to make it "see" React Native libraries that are not part of our 3rd party dependencies. To do so, update your `react-native.config.js`'s `dependencies` entry to look like this:
@@ -124,6 +150,7 @@ correct location and update them accordingly:
124
150
- path to `native_modules.gradle` in your `android/app/build.gradle`
125
151
126
152
Dependencies are only linked if they are listed in the package.json of the mobile workspace, where "react-native" dependency is defined. For example, with this file structure:
153
+
127
154
```
128
155
/root
129
156
/packages
@@ -135,4 +162,5 @@ Dependencies are only linked if they are listed in the package.json of the mobil
135
162
package.json <-- Dependencies here are ignored when auto-linking
136
163
package.json
137
164
```
165
+
138
166
In this example, if you add a package with native code as a dependency of `components`, you need to also add it as a dependency of `mobile` for auto-linking to work.
Copy file name to clipboardExpand all lines: docs/dependencies.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,9 @@ type AndroidDependencyParams = {
55
55
packageImportPath?:string;
56
56
packageInstance?:string;
57
57
buildTypes?:string[];
58
+
libraryName?:string|null;
59
+
componentDescriptors?:string[] |null;
60
+
androidMkPath?:string|null;
58
61
};
59
62
```
60
63
@@ -119,3 +122,21 @@ An array of build variants or flavors which will include the dependency. If the
119
122
120
123
A string that defines which method other than `implementation` do you want to use
121
124
for autolinking inside `build.gradle` i.e: `'embed project(path: ":$dependencyName", configuration: "default")',` - `"dependencyName` will be replaced by the actual package's name. You can achieve the same result by directly defining this key per `dependency`_(without placeholder)_ and it will have higher priority than this option.
125
+
126
+
#### platforms.android.libraryName
127
+
128
+
> Note: Only applicable when new architecture is turned on.
129
+
130
+
A string indicating your custom library name. By default it's taken from the `libraryName` variable in your library's `build.gradle`.
131
+
132
+
#### platforms.android.componentDescriptors
133
+
134
+
> Note: Only applicable when new architecture is turned on.
135
+
136
+
An array of custom component descriptor strings. By default they're generated based on `codegenNativeComponent` calls.
137
+
138
+
#### platforms.android.androidMkPath
139
+
140
+
> Note: Only applicable when new architecture is turned on.
141
+
142
+
A relative path to a custom _Android.mk_ file not registered by codegen. Relative to `sourceDir`.
0 commit comments