Skip to content

Commit

Permalink
Update the New App template to reduce the amount of C++ code in user …
Browse files Browse the repository at this point in the history
…space (#34666)

Summary:
Pull Request resolved: #34666

This diff updates the new app template and reduces the amount of C++ code in user space.
From now on users will have to only:
1. Create a CMakeLists.txt file
2. Create an OnLoad.cpp file with the Modules/Components they want to provide.

Changelog:
[Android] [Changed] - Update the template to Reduce the amount of C++ code in user space for New Architecture

Reviewed By: cipolleschi

Differential Revision: D39381762

fbshipit-source-id: 7309b6c61ba9ddd8856cb4aaa6d923ddd816741c
  • Loading branch information
cortinico authored and facebook-github-bot committed Sep 12, 2022
1 parent e89bd4a commit b0aba1b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.defaults.DefaultNativeEntryPoint;
import com.facebook.react.defaults.DefaultReactNativeHost;
import com.facebook.soloader.SoLoader;
import java.util.List;
Expand Down Expand Up @@ -57,9 +58,9 @@ public void onCreate() {
SoLoader.init(this, /* native exopackage */ false);
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// If you opted-in for the New Architecture, we enable the TurboModule system
// and load the native dynamic library for this app.
// and load the native entry point for this app.
ReactFeatureFlags.useTurboModules = true;
SoLoader.loadLibrary(BuildConfig.DYNAMIC_LIBRARY_NAME);
DefaultNativeEntryPoint.load(BuildConfig.DYNAMIC_LIBRARY_NAME);
}
ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
}
Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions template/android/app/src/main/jni/MainApplicationModuleProvider.h

This file was deleted.

This file was deleted.

This file was deleted.

65 changes: 0 additions & 65 deletions template/android/app/src/main/jni/MainComponentsRegistry.cpp

This file was deleted.

32 changes: 0 additions & 32 deletions template/android/app/src/main/jni/MainComponentsRegistry.h

This file was deleted.

71 changes: 66 additions & 5 deletions template/android/app/src/main/jni/OnLoad.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
#include <DefaultComponentsRegistry.h>
#include <DefaultTurboModuleManagerDelegate.h>
#include <fbjni/fbjni.h>
#include "MainApplicationTurboModuleManagerDelegate.h"
#include "MainComponentsRegistry.h"
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
#include <rncli.h>

namespace facebook {
namespace react {

void registerComponents(
std::shared_ptr<ComponentDescriptorProviderRegistry const> registry) {
// Custom Fabric Components go here. You can register custom
// components coming from your App or from 3rd party libraries here.
//
// providerRegistry->add(concreteComponentDescriptorProvider<
// AocViewerComponentDescriptor>());

// By default we just use the components autolinked by RN CLI
rncli_registerProviders(registry);
}

std::shared_ptr<TurboModule> provideModules(
const std::string &name,
const JavaTurboModule::InitParams &params) {
// Here you can provide your own module provider for TurboModules coming from
// either your application or from external libraries. The approach to follow
// is similar to the following (for a library called `samplelibrary`):
//
// auto module = samplelibrary_ModuleProvider(moduleName, params);
// if (module != nullptr) {
// return module;
// }
// return rncore_ModuleProvider(moduleName, params);

// By default we just use the module providers autolinked by RN CLI
return rncli_ModuleProvider(name, params);
}

class MainApplicationNativeEntryPoint
: public facebook::jni::HybridClass<MainApplicationNativeEntryPoint> {
public:
constexpr static auto kJavaDescriptor =
"Lcom/facebook/react/defaults/DefaultNativeEntryPoint;";

static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject>) {
DefaultTurboModuleManagerDelegate::moduleProvidersFromEntryPoint =
&provideModules;
DefaultComponentsRegistry::registerComponentDescriptorsFromEntryPoint =
&registerComponents;
return makeCxxInstance();
}

static void registerNatives() {
registerHybrid({
makeNativeMethod(
"initHybrid", MainApplicationNativeEntryPoint::initHybrid),
});
}

private:
friend HybridBase;
using HybridBase::HybridBase;
};

} // namespace react
} // namespace facebook

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
return facebook::jni::initialize(vm, [] {
facebook::react::MainApplicationTurboModuleManagerDelegate::
registerNatives();
facebook::react::MainComponentsRegistry::registerNatives();
facebook::react::MainApplicationNativeEntryPoint::registerNatives();
});
}

0 comments on commit b0aba1b

Please sign in to comment.