Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove additional config steps required when running Fabric version on Android #1322

Merged
merged 5 commits into from Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,7 +22,6 @@
import com.fabricexample.BuildConfig;
import com.fabricexample.newarchitecture.components.MainComponentsRegistry;
import com.fabricexample.newarchitecture.modules.MainApplicationTurboModuleManagerDelegate;
import com.swmansion.rnscreens.RNScreensComponentsRegistry;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -97,7 +96,6 @@ public JSIModuleProvider<UIManager> getJSIModuleProvider() {
// The one that is generated with the template contains no components
// and just provides you the one from React Native core.
MainComponentsRegistry.register(componentFactory);
RNScreensComponentsRegistry.register(componentFactory);

final ReactInstanceManager reactInstanceManager = getReactInstanceManager();

Expand Down
6 changes: 2 additions & 4 deletions FabricExample/android/app/src/main/jni/Android.mk
Expand Up @@ -5,7 +5,6 @@ include $(REACT_ANDROID_DIR)/Android-prebuilt.mk
# If you wish to add a custom TurboModule or Fabric component in your app you
# will have to include the following autogenerated makefile.
# include $(GENERATED_SRC_DIR)/codegen/jni/Android.mk
include $(REACT_ANDROID_DIR)/../../react-native-screens/android/src/main/jni/Android.mk

include $(CLEAR_VARS)

Expand All @@ -19,7 +18,7 @@ LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)

# If you wish to add a custom TurboModule or Fabric component in your app you
# will have to uncomment those lines to include the generated source
# will have to uncomment those lines to include the generated source
# files from the codegen (placed in $(GENERATED_SRC_DIR)/codegen/jni)
#
# LOCAL_C_INCLUDES += $(GENERATED_SRC_DIR)/codegen/jni
Expand All @@ -44,8 +43,7 @@ LOCAL_SHARED_LIBRARIES := \
librrc_view \
libruntimeexecutor \
libturbomodulejsijni \
libyoga \
librnscreens_modules
libyoga

LOCAL_CFLAGS := -DLOG_TAG=\"ReactNative\" -fexceptions -frtti -std=c++17 -Wall

Expand Down
27 changes: 4 additions & 23 deletions README-Fabric.md
@@ -1,28 +1,9 @@
# React Native Screens - Fabric version

To use this library with your fabric application, you have to:
To use this library with your Fabric application, you have to:

1. Add latest react-native-screens
2. on iOS
- Install pods using `RCT_NEW_ARCH_ENABLED=1 pod install` (iOS only)
- Install pods using `RCT_NEW_ARCH_ENABLED=1 pod install` – this is a ste
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be this is the same as in Fabric app?

3. on Android
- In `ReactNativeHost` in your Application (for react native 0.68 template it is in `android/app/src/main/java/com/YOUR_APP_NAME/newarchitecture/MainApplicationReactNativeHost.java`) add
```diff
import com.fabricexample.newarchitecture.modules.MainApplicationTurboModuleManagerDelegate;
+ import com.swmansion.rnscreens.RNScreensComponentRegistry;
...
public JSIModuleProvider<UIManager> getJSIModuleProvider() {
...
MainComponentsRegistry.register(componentFactory);
+ RNScreensComponentsRegistry.register(componentFactory);
```
- In your app `Android.mk` (for react native 0.68 template it is in `android/app/src/main/jni/Android.mk`) add
```diff
+ include $(REACT_ANDROID_DIR)/../../react-native-screens/android/src/main/jni/Android.mk
include $(CLEAR_VARS)
...
LOCAL_SHARED_LIBRARIES := libjsi \
...
- libyoga
+ libyoga \
+ librnscreens_modules
```
- There are no additional steps required so long you app is configured to build with Fabric – this is typically configured by setting `newArchEnabled=true` in `gradle.properties` file in your project.
33 changes: 32 additions & 1 deletion android/build.gradle
Expand Up @@ -41,6 +41,23 @@ android {
versionCode 1
versionName "1.0"
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
if (isNewArchitectureEnabled()) {
var appProject = rootProject.allprojects.find {it.plugins.hasPlugin('com.android.application')}
externalNativeBuild {
ndkBuild {
arguments "APP_PLATFORM=android-21",
"APP_STL=c++_shared",
"NDK_TOOLCHAIN_VERSION=clang",
"GENERATED_SRC_DIR=${appProject.buildDir}/generated/source",
"PROJECT_BUILD_DIR=${appProject.buildDir}",
"REACT_ANDROID_DIR=${appProject.rootDir}/../node_modules/react-native/ReactAndroid",
"REACT_ANDROID_BUILD_DIR=${appProject.rootDir}/../node_modules/react-native/ReactAndroid/build"
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
cppFlags "-std=c++17"
targets "rnscreens_modules"
}
}
}
}
lintOptions {
abortOnError false
Expand All @@ -49,6 +66,20 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
if (isNewArchitectureEnabled()) {
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
}
packagingOptions {
// For some reason gradle only complains about the duplicated version of libreact_render libraries
// while there are more libraries copied in intermediates folder of the lib build directory, we exlude
// only the ones that make the build fail (ideally we should only include librnscreens_modules but we
// are only allowed to specify exlude patterns)
exclude "**/libreact_render*.so"
}
sourceSets.main {
java {
if(isNewArchitectureEnabled()){
kacperkapusciak marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -57,7 +88,7 @@ android {
]
} else {
srcDirs += [
"src/paper/java",
"src/paper/java",
"build/generated/source/codegen/java"
]
}
Expand Down
13 changes: 11 additions & 2 deletions android/src/main/java/com/swmansion/rnscreens/RNScreensPackage.kt
Expand Up @@ -4,10 +4,19 @@ import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager
import com.facebook.soloader.SoLoader

class RNScreensPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext) =
emptyList<NativeModule>()
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// For Fabric, we load c++ naitve library here, this triggers screen's Fabric
kacperkapusciak marked this conversation as resolved.
Show resolved Hide resolved
// component registration which is necessary in order to avoid asking users
// to manually add init calls in their application code.
// This should no longer be needed if RN's autolink mechanism has Fabric support
SoLoader.loadLibrary("rnscreens_modules")
}
return emptyList<NativeModule>()
}

override fun createViewManagers(reactContext: ReactApplicationContext) =
listOf<ViewManager<*, *>>(
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/jni/Android.mk
@@ -1,5 +1,7 @@
SCREENS_MAIN_THIS_DIR := $(call my-dir)

include $(REACT_ANDROID_DIR)/Android-prebuilt.mk

include $(SCREENS_MAIN_THIS_DIR)/../../../build/generated/source/codegen/jni/Android.mk
include $(SCREENS_MAIN_THIS_DIR)/../../../../common/cpp/Android.mk

Expand Down
7 changes: 7 additions & 0 deletions android/src/main/jni/RNScreensComponentsRegistry.cpp
Expand Up @@ -53,6 +53,13 @@ void RNScreensComponentsRegistry::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", RNScreensComponentsRegistry::initHybrid),
});
// This is a temporary solution that allows components exported by the screens
// library to be added to the main component registry. This code is triggered
// when c++ screens library is initialized and is needed because RN's autolinking
// does not currently support Fabric components. As a consequence, users would need
// to manually put library initialization calls in their ReactNativeHost implementation
// which is undesirable.
sharedProviderRegistry();
}

} // namespace react
Expand Down
3 changes: 1 addition & 2 deletions android/src/main/jni/RNScreensComponentsRegistry.h
Expand Up @@ -21,8 +21,7 @@ class RNScreensComponentsRegistry
private:
friend HybridBase;

static std::shared_ptr<ComponentDescriptorProviderRegistry const>
sharedProviderRegistry();
static std::shared_ptr<ComponentDescriptorProviderRegistry const> sharedProviderRegistry();

const ComponentFactory *delegate_;

Expand Down