add support for RN 0.46.x (#905)

This commit is contained in:
Sergey Akhalkov
2017-07-07 13:05:40 +03:00
committed by GitHub
parent 9a6b44b588
commit 9bc0770845

View File

@@ -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);