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
facebook#34445)

Summary:
Pull Request resolved: facebook#34445

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

Reviewed By: cipolleschi

Differential Revision: D38820111

fbshipit-source-id: 9853140e9d8c15606a3856aa60523c8ac6160a3d
  • Loading branch information
cortinico authored and facebook-github-bot committed Aug 18, 2022
1 parent 5f8808a commit e8c6b4b
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@
import androidx.annotation.Nullable;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.facebook.react.defaults.DefaultReactActivityDelegate;

public class RNTesterActivity extends ReactActivity {
public static class RNTesterActivityDelegate extends ReactActivityDelegate {
public static class RNTesterActivityDelegate extends DefaultReactActivityDelegate {
private static final String PARAM_ROUTE = "route";
private Bundle mInitialProps = null;
private final @Nullable ReactActivity mActivity;

public RNTesterActivityDelegate(ReactActivity activity, String mainComponentName) {
super(activity, mainComponentName);
super(
activity,
mainComponentName,
true, // fabricEnabled
true // concurrentRootEnabled
);
this.mActivity = activity;
}

@Override
protected ReactRootView createRootView() {
ReactRootView reactRootView = new ReactRootView(getContext());
reactRootView.setIsFabric(true);
return reactRootView;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
// Get remote param before calling super which uses it
Expand All @@ -51,11 +49,6 @@ protected void onCreate(Bundle savedInstanceState) {
protected Bundle getLaunchOptions() {
return mInitialProps;
}

@Override
protected boolean isConcurrentRootEnabled() {
return true;
}
}

@Override
Expand Down
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 @@ -14,9 +14,6 @@ void MainApplicationTurboModuleManagerDelegate::registerNatives() {
registerHybrid({
makeNativeMethod(
"initHybrid", MainApplicationTurboModuleManagerDelegate::initHybrid),
makeNativeMethod(
"canCreateTurboModule",
MainApplicationTurboModuleManagerDelegate::canCreateTurboModule),
});
}

Expand All @@ -35,11 +32,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
Loading

0 comments on commit e8c6b4b

Please sign in to comment.