Skip to content

Commit 9573c5d

Browse files
author
Riccardo Cipolleschi
committed
feat: Add fabric component to app - Android
1 parent 1cf3697 commit 9573c5d

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

docs/the-new-architecture/pillars-fabric-components.md

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ cd android
534534

535535
This script first adds the package to the app, in the same way iOS does. Then, after moving to the `android` folder, it invokes a Gradle task to generate the codegen.
536536

537+
:::note
538+
To run the **CodeGen**, you need to enable the **New Architecture** in the Android app. This can be done by opening the `gradle.properties` files and by switching the `newArchEnabled` property from `false` to `true`.
539+
:::
540+
537541
The generated code is stored in the `MyApp/node_modules/rnt-centered-text/android/build/generated/source/codegen` folder and it has this structure:
538542

539543
```title="Android generated code"
@@ -740,9 +744,7 @@ This is the last piece of Native Code for Android. It defines the Package object
740744

741745
This is the last step to finally see our Fabric Component running on our app.
742746

743-
#### iOS
744-
745-
To achieve this in iOS, we need to issue a couple of commands and then we can read the Component from JS.
747+
#### Shared
746748

747749
First of all, we need to add the NPM package which contains the Component to the app. This can be done with the following command:
748750

@@ -751,7 +753,11 @@ cd MyApp
751753
yarn add ../RTNCenteredText
752754
```
753755

754-
This command will add the `RTNCenteredText` Component to the `node_modules` of your app. Then, we need to install the new dependencies in our iOS project. To do so, we need to run these commands:
756+
This command will add the `RTNCenteredText` Component to the `node_modules` of your app.
757+
758+
#### iOS
759+
760+
Then, we need to install the new dependencies in our iOS project. To do so, we need to run these commands:
755761

756762
```sh
757763
cd ios
@@ -766,7 +772,63 @@ You may have to run `bundle install` once before you can use `RCT_NEW_ARCH_ENABL
766772

767773
#### Android
768774

769-
#### JS
775+
Android configuration requires slightly more steps in order to be able to use our new Component.
776+
777+
First, we need to enable the **New Architecture**, because **Fabric** requires it to run properly. This can be done by:
778+
779+
1. Open the `android/gradle.properties` file
780+
2. Scroll down to the end of the file and switch the `newArchEnabled` property from `false` to `true`.
781+
782+
Then, we need to instruct the `Android.mk` file that it needs to build also the new library.
783+
This can be with these steps:
784+
785+
1. Open the `android/app/src/main/jni/Android.mk` file
786+
1. Add this line to include the library at the beginning of the file:
787+
788+
```diff
789+
include $(REACT_ANDROID_DIR)/Android-prebuilt.mk
790+
791+
# If you wish to add a custom TurboModule or Fabric component in your app you
792+
# will have to include the following autogenerated makefile.
793+
# include $(GENERATED_SRC_DIR)/codegen/jni/Android.mk
794+
795+
+include $(NODE_MODULES_DIR)/rnt-centered-text/android/build/generated/source/codegen/jni/Android.mk
796+
include $(CLEAR_VARS)
797+
```
798+
799+
1. In the same file, scroll down until you find a list of `libreact` libraries. There, we have to add the the library that has been generated. To do so, we need to add this line:
800+
```diff
801+
libreact_codegen_rncore \
802+
+libreact_codegen_RTNCenteredText \
803+
libreact_debug \
804+
```
805+
806+
:::note
807+
The name of the library will be `libreact_codegen_<libraryname>` where `<libraryname>` is the value that has been set in the config.
808+
Also, this step won't be necessary anymore as soon as we release a version of React Native which supports autolinking for Android.
809+
:::
810+
811+
Finally, we need to configure the Fabric component registry to load the Fabric Component at runtime. This can be done by:
812+
813+
1. Open the `MyApp/android/app/src/main/jni/MainComponentsRegistry.cpp`
814+
1. Add the following include:
815+
816+
```c++
817+
#include <react/renderer/components/RTNCenteredText/ComponentDescriptors.h>
818+
```
819+
820+
1. Update the `sharedProviderRegistry` with this line:
821+
822+
```diff
823+
auto providerRegistry = CoreComponentsRegistry::sharedProviderRegistry();
824+
825+
+providerRegistry->add(concreteComponentDescriptorProvider<RTNCenteredTextComponentDescriptor>());
826+
827+
// Custom Fabric Components go here. You can register custom
828+
```
829+
830+
#### JS
831+
770832
Finally, we can read the Component in our JS application.
771833
To do so, we have to:
772834

0 commit comments

Comments
 (0)