Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

feat(android): add instantiateReactNative and destroyReactNative APIs (#17680)

Calinteodor564a44b1ce85
diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeet.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeet.java
index 68c2911fd0..7beb397d5d 100644
--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeet.java
+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeet.java
@@ -16,6 +16,7 @@
package org.jitsi.meet.sdk;
import android.app.Activity;
+import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
@@ -78,6 +79,26 @@ public class JitsiMeet {
}
}
+ /**
+ * Starts the React Native runtime if it's not already running. Optional
+ * warm-up after {@link #destroyReactNative()}; joining a conference also
+ * restarts it on demand.
+ *
+ * @param context - Any {@link Context}; its application context is used.
+ */
+ public static void instantiateReactNative(Context context) {
+ ReactHostHolder.initReactHost((Application) context.getApplicationContext());
+ }
+
+ /**
+ * Destroys the React Native runtime, freeing its resources. Teardown is
+ * asynchronous. Every {@link JitsiMeetView} must be disposed first; the
+ * next {@link JitsiMeetView} join() re-creates the runtime.
+ */
+ public static void destroyReactNative() {
+ ReactHostHolder.destroyReactHost();
+ }
+
public static boolean isCrashReportingDisabled(Context context) {
SharedPreferences preferences = context.getSharedPreferences("jitsi-default-preferences", Context.MODE_PRIVATE);
String value = preferences.getString("isCrashReportingDisabled", "");
diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java
index b490483df0..7a60473176 100644
--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java
+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java
@@ -190,6 +190,9 @@ public class JitsiMeetView extends FrameLayout {
props = new Bundle();
}
+ // No-op while running; rebuilds the host after destroyReactNative().
+ JitsiMeet.instantiateReactNative(getContext());
+
ReactHost reactHost = ReactHostHolder.getReactHost();
if (reactHost == null) {
JitsiMeetLogger.w("Cannot create surface, ReactHost is not initialized");
diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/ReactHostHolder.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/ReactHostHolder.java
index a573d87d08..876077d38a 100644
--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/ReactHostHolder.java
+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/ReactHostHolder.java
@@ -22,12 +22,18 @@ import androidx.annotation.Nullable;
import com.facebook.react.ReactHost;
import com.facebook.react.ReactPackage;
+import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.RetryableMountingLayerException;
-import com.facebook.react.defaults.DefaultReactHost;
+import com.facebook.react.defaults.DefaultComponentsRegistry;
+import com.facebook.react.defaults.DefaultReactHostDelegate;
+import com.facebook.react.defaults.DefaultTurboModuleManagerDelegate;
+import com.facebook.react.fabric.ComponentFactory;
import com.facebook.react.modules.core.DeviceEventManagerModule;
+import com.facebook.react.runtime.ReactHostImpl;
+import com.facebook.react.runtime.hermes.HermesInstance;
import com.facebook.react.uimanager.ViewManager;
import com.oney.WebRTCModule.EglUtils;
import com.oney.WebRTCModule.WebRTCModuleOptions;
@@ -219,17 +225,19 @@ class ReactHostHolder {
}
}
- JitsiMeetLogger.d(TAG, "initializing RN");
+ JitsiMeetLogger.d(TAG + " initializing RN");
- reactHost = DefaultReactHost.getDefaultReactHost(
- app,
+ // Same construction getDefaultReactHost() does, minus its private static cache
+ // that only an internal invalidate() clears — a destroyed host is never replaced.
+ JSBundleLoader bundleLoader
+ = JSBundleLoader.createAssetLoader(app, "assets://index.android.bundle", true);
+
+ DefaultReactHostDelegate delegate = new DefaultReactHostDelegate(
+ "index.android", /* jsMainModulePath */
+ bundleLoader,
getReactNativePackages(),
- "index.android", /* jsMainModulePath */
- "index.android.bundle", /* jsBundleAssetPath */
- null, /* jsBundleFilePath */
- null, /* jsRuntimeFactory (defaults to Hermes) */
- BuildConfig.DEBUG, /* useDevSupport */
- Collections.emptyList(), /* cxxReactPackageProviders */
+ new HermesInstance(),
+ null, /* bindingsInstaller */
e -> {
// Backport of react-native #57181: ignore the benign missing-viewState
// mount race. Remove once RN ships the fix.
@@ -240,13 +248,37 @@ class ReactHostHolder {
throw new RuntimeException(e);
}
return kotlin.Unit.INSTANCE;
- }, /* exceptionHandler */
- null /* bindingsInstaller */
- );
+ }, /* exceptionHandler */
+ new DefaultTurboModuleManagerDelegate.Builder());
+
+ ComponentFactory componentFactory = new ComponentFactory();
+ DefaultComponentsRegistry.register(componentFactory);
+
+ reactHost = new ReactHostImpl(
+ app,
+ delegate,
+ componentFactory,
+ true, /* allowPackagerServerAccess */
+ BuildConfig.DEBUG /* useDevSupport */);
reactHost.start();
}
+ /**
+ * Destroys the React Native runtime and drops the host reference so a
+ * new one can be built. Teardown is asynchronous.
+ */
+ static void destroyReactHost() {
+ if (reactHost == null) {
+ return;
+ }
+
+ JitsiMeetLogger.d(TAG + " destroying RN");
+
+ reactHost.invalidate();
+ reactHost = null;
+ }
+
// Matches the missing-viewState mount race fixed upstream in react-native #57181.
private static boolean isMissingViewStateException(Throwable e) {
for (Throwable t = e; t != null; t = t.getCause()) {