Skip to content

Commit

Permalink
fix(android): fix loading of core modules on 0.75 + New Arch (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Jul 17, 2024
1 parent 1b24387 commit 6c8451f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions android/app/src/main/jni/ComponentsRegistry.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "ComponentsRegistry.h"

#if !__has_include(<DefaultTurboModuleManagerDelegate.h>)

#include "AutolinkingCompat.h"

#if __has_include(<react/fabric/CoreComponentsRegistry.h>) // >= 0.71
Expand Down Expand Up @@ -61,3 +63,5 @@ ComponentsRegistry::initHybrid(facebook::jni::alias_ref<jclass>, ComponentFactor

return makeCxxInstance(delegate);
}

#endif // !__has_include(<DefaultTurboModuleManagerDelegate.h>)
19 changes: 18 additions & 1 deletion android/app/src/main/jni/OnLoad.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/cmake-utils/default-app-setup/OnLoad.cpp
#if __has_include(<DefaultTurboModuleManagerDelegate.h>)

#include <DefaultComponentsRegistry.h>
#include <DefaultTurboModuleManagerDelegate.h>
#include <rncore.h>

#include <fbjni/fbjni.h>

Expand All @@ -12,6 +14,7 @@
using facebook::react::CallInvoker;
using facebook::react::DefaultComponentsRegistry;
using facebook::react::DefaultTurboModuleManagerDelegate;
using facebook::react::JavaTurboModule;
using facebook::react::TurboModule;

namespace
Expand All @@ -25,13 +28,27 @@ namespace
return nullptr;
#endif // __has_include(<ReactCommon/CxxReactPackage.h>)
}

std::shared_ptr<TurboModule> javaModuleProvider(const std::string &name,
const JavaTurboModule::InitParams &params)
{
#if __has_include(<autolinking.h>) // >= 0.75
// We first try to look up core modules
if (auto module = rncore_ModuleProvider(name, params)) {
return module;
}
#endif // __has_include(<autolinking.h>)

// And we fallback to the module providers autolinked by RN CLI
return autolinking_ModuleProvider(name, params);
}
} // namespace

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *)
{
return facebook::jni::initialize(vm, [] {
DefaultTurboModuleManagerDelegate::cxxModuleProvider = &cxxModuleProvider;
DefaultTurboModuleManagerDelegate::javaModuleProvider = &autolinking_ModuleProvider;
DefaultTurboModuleManagerDelegate::javaModuleProvider = &javaModuleProvider;
DefaultComponentsRegistry::registerComponentDescriptorsFromEntryPoint =
&autolinking_registerProviders;
});
Expand Down
4 changes: 4 additions & 0 deletions android/app/src/main/jni/TurboModuleManagerDelegate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "TurboModuleManagerDelegate.h"

#if !__has_include(<DefaultTurboModuleManagerDelegate.h>)

#include <rncore.h>

#include "AutolinkingCompat.h"
Expand Down Expand Up @@ -47,3 +49,5 @@ bool TurboModuleManagerDelegate::canCreateTurboModule(StringRef name)
return getTurboModule(name, nullptr) != nullptr ||
getTurboModule(name, {.moduleName = name}) != nullptr;
}

#endif // !__has_include(<DefaultTurboModuleManagerDelegate.h>)

0 comments on commit 6c8451f

Please sign in to comment.