Skip to content

Commit

Permalink
Simplify the template for New Architecture using the .defaults package
Browse files Browse the repository at this point in the history
Summary:
This commit simplifies the new app template by encapsulating a lot of configuration
by using the `DefaultReactNativeHost` from the .defaults package.

This should work for most of the users while still allowing advanced use cases
by using the good old ReactNativeHost.

Changelog:
[Android] [Changed] - Simplify the template for New Architecture using the .defaults package

Differential Revision: D38820111

fbshipit-source-id: d85969d12ecf8012bf526f73c4f680b031392c4a
  • Loading branch information
cortinico authored and facebook-github-bot committed Aug 18, 2022
1 parent 5f8808a commit 9b098eb
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 227 deletions.
9 changes: 9 additions & 0 deletions template/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def reactNativeArchitectures() {
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

/**
* The name of the dynamic library for this application. This will contain all the
* compiled C++ code and will be loaded at runtime.
* The default is "appmodules" so that we'll have a `libappmodules.so` to load.
*/
def dynamicLibraryName = "appmodules"

android {
ndkVersion rootProject.ext.ndkVersion

Expand All @@ -141,6 +148,7 @@ android {
versionCode 1
versionName "1.0"
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
buildConfigField "String", "DYNAMIC_LIBRARY_NAME", "\"$dynamicLibraryName\""

if (isNewArchitectureEnabled()) {
// We configure the CMake build only if you decide to opt-in for the New Architecture.
Expand All @@ -150,6 +158,7 @@ android {
"-DREACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
"-DREACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build",
"-DNODE_MODULES_DIR=$rootDir/../node_modules",
"-DTARGET_NAME=$dynamicLibraryName",
"-DANDROID_STL=c++_shared"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.defaults.DefaultReactNativeHost;
import com.facebook.soloader.SoLoader;
import com.helloworld.newarchitecture.MainApplicationReactNativeHost;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {

private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
new DefaultReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
Expand All @@ -32,26 +32,35 @@ protected List<ReactPackage> getPackages() {
protected String getJSMainModuleName() {
return "index";
}
};

private final ReactNativeHost mNewArchitectureNativeHost =
new MainApplicationReactNativeHost(this);
@Override
public String getDynamicLibraryName() {
// If you enabled the New Architecture, you need to return the name of the
// dynamic library to load (usually 'appmodule'). This is configured
// in your build.gradle file.
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
return BuildConfig.DYNAMIC_LIBRARY_NAME;
} else {
return null;
}
}
};

@Override
public ReactNativeHost getReactNativeHost() {
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
return mNewArchitectureNativeHost;
} else {
return mReactNativeHost;
}
return mReactNativeHost;
}

@Override
public void onCreate() {
super.onCreate();
// If you opted-in for the New Architecture, we enable the TurboModule system
ReactFeatureFlags.useTurboModules = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
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.
ReactFeatureFlags.useTurboModules = true;
SoLoader.loadLibrary(BuildConfig.DYNAMIC_LIBRARY_NAME);
}
ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion template/android/app/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.13)

# Define the library name here.
project(helloworld_appmodules)
project(${TARGET_NAME})

# This file includes all the necessary to let you build your application with the New Architecture.
include(${REACT_ANDROID_DIR}/cmake-utils/ReactNative-application.cmake)
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,5 @@ MainApplicationTurboModuleManagerDelegate::getTurboModule(
return MainApplicationModuleProvider(name, params);
}

bool MainApplicationTurboModuleManagerDelegate::canCreateTurboModule(
const std::string &name) {
return getTurboModule(name, nullptr) != nullptr ||
getTurboModule(name, {.moduleName = name}) != nullptr;
}

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MainApplicationTurboModuleManagerDelegate
public:
// Adapt it to the package you used for your Java class.
static constexpr auto kJavaDescriptor =
"Lcom/helloworld/newarchitecture/modules/MainApplicationTurboModuleManagerDelegate;";
"Lcom/facebook/react/defaults/DefaultTurboModuleManagerDelegate;";

static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject>);

Expand All @@ -26,12 +26,6 @@ class MainApplicationTurboModuleManagerDelegate
std::shared_ptr<TurboModule> getTurboModule(
const std::string &name,
const JavaTurboModule::InitParams &params) override;

/**
* Test-only method. Allows user to verify whether a TurboModule can be
* created by instances of this class.
*/
bool canCreateTurboModule(const std::string &name);
};

} // namespace react
Expand Down
2 changes: 1 addition & 1 deletion template/android/app/src/main/jni/MainComponentsRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MainComponentsRegistry
public:
// Adapt it to the package you used for your Java class.
constexpr static auto kJavaDescriptor =
"Lcom/helloworld/newarchitecture/components/MainComponentsRegistry;";
"Lcom/facebook/react/defaults/DefaultComponentsRegistry;";

static void registerNatives();

Expand Down

0 comments on commit 9b098eb

Please sign in to comment.