From 9bc0770845cb1ed01344048ea5468dc89a2430e2 Mon Sep 17 00:00:00 2001 From: Sergey Akhalkov Date: Fri, 7 Jul 2017 13:05:40 +0300 Subject: [PATCH] add support for RN 0.46.x (#905) --- .../codepush/react/CodePushNativeModule.java | 46 ++++++------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java index 1cda9b7..636063a 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java @@ -11,6 +11,7 @@ import com.facebook.react.ReactApplication; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactRootView; import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.JSBundleLoader; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; @@ -28,7 +29,6 @@ import org.json.JSONObject; import java.io.IOException; import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -100,37 +100,14 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { // to approach this. private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBundleFile) throws IllegalAccessException { try { - Field bundleLoaderField = instanceManager.getClass().getDeclaredField("mBundleLoader"); - Class jsBundleLoaderClass = Class.forName("com.facebook.react.cxxbridge.JSBundleLoader"); - Method createFileLoaderMethod = null; - String createFileLoaderMethodName = latestJSBundleFile.toLowerCase().startsWith("assets://") - ? "createAssetLoader" : "createFileLoader"; - - Method[] methods = jsBundleLoaderClass.getDeclaredMethods(); - for (Method method : methods) { - if (method.getName().equals(createFileLoaderMethodName)) { - createFileLoaderMethod = method; - break; - } - } - - if (createFileLoaderMethod == null) { - throw new NoSuchMethodException("Could not find a recognized 'createFileLoader' method"); - } - - int numParameters = createFileLoaderMethod.getGenericParameterTypes().length; - Object latestJSBundleLoader; - - if (numParameters == 1) { - // RN >= v0.34 - latestJSBundleLoader = createFileLoaderMethod.invoke(jsBundleLoaderClass, latestJSBundleFile); - } else if (numParameters == 2) { - // AssetLoader instance - latestJSBundleLoader = createFileLoaderMethod.invoke(jsBundleLoaderClass, getReactApplicationContext(), latestJSBundleFile); + JSBundleLoader latestJSBundleLoader; + if (latestJSBundleFile.toLowerCase().startsWith("assets://")) { + latestJSBundleLoader = JSBundleLoader.createAssetLoader(getReactApplicationContext(), latestJSBundleFile, false); } else { - throw new NoSuchMethodException("Could not find a recognized 'createFileLoader' method"); + latestJSBundleLoader = JSBundleLoader.createFileLoader(latestJSBundleFile); } + Field bundleLoaderField = instanceManager.getClass().getDeclaredField("mBundleLoader"); bundleLoaderField.setAccessible(true); bundleLoaderField.set(instanceManager, latestJSBundleLoader); } catch (Exception e) { @@ -160,10 +137,10 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { @Override public void run() { try { - // This workaround has been implemented in order to fix https://github.com/facebook/react-native/issues/14533 - // resetReactRootViews allows to call recreateReactContextInBackground without any exceptions - // This fix also relates to https://github.com/Microsoft/react-native-code-push/issues/878 - resetReactRootViews(instanceManager); + // We don't need to resetReactRootViews anymore + // due the issue https://github.com/facebook/react-native/issues/14533 + // has been fixed in RN 0.46.0 + //resetReactRootViews(instanceManager); instanceManager.recreateReactContextInBackground(); mCodePush.initializeUpdateAfterRestart(); @@ -182,6 +159,9 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { } } + // This workaround has been implemented in order to fix https://github.com/facebook/react-native/issues/14533 + // resetReactRootViews allows to call recreateReactContextInBackground without any exceptions + // This fix also relates to https://github.com/Microsoft/react-native-code-push/issues/878 private void resetReactRootViews(ReactInstanceManager instanceManager) throws NoSuchFieldException, IllegalAccessException { Field mAttachedRootViewsField = instanceManager.getClass().getDeclaredField("mAttachedRootViews"); mAttachedRootViewsField.setAccessible(true);