react-native-multibundler icon indicating copy to clipboard operation
react-native-multibundler copied to clipboard

没有执行预加载的代码会报错

Open moujunqiang opened this issue 5 years ago • 3 comments

moujunqiang avatar Aug 03 '20 06:08 moujunqiang

ReactInstanceManager reactInstanceManager = ((ReactApplication)getApplication()).getReactNativeHost().getReactInstanceManager(); if (!reactInstanceManager.hasStartedCreatingInitialContext()) { reactInstanceManager.createReactContextInBackground();//这里会先加载基础包platform.android.bundle,也可以不加载 } 如果注释掉MainActivity这段代码会报错

moujunqiang avatar Aug 03 '20 06:08 moujunqiang

如果注释掉MainActivity中的这几行

ReactInstanceManager reactInstanceManager = ((ReactApplication)getApplication()).getReactNativeHost().getReactInstanceManager();
        if (!reactInstanceManager.hasStartedCreatingInitialContext()) {
            reactInstanceManager.createReactContextInBackground();
        }

会导致崩溃

修复方法是: 修改AsyncReactActivity中

//默认給null的ComponentName
  protected ReactActivityDelegate createReactActivityDelegate() {
        return new ReactActivityDelegate(this, null);
    }


//runApp中加载完成了Bundle再调用mDelegate.loadApp(getMainComponentName());
protected void runApp(String scriptPath){
        if(scriptPath!=null){
            scriptPath = "file://"+scriptPath.substring(0,scriptPath.lastIndexOf(File.separator)+1);
        }
        final String path = scriptPath;
        final RnBundle bundle = getBundle();
        final ReactInstanceManager reactInstanceManager = ((ReactApplication)getApplication()).getReactNativeHost().getReactInstanceManager();
        if(bundle.scriptType == ScriptType.NETWORK){//如果是网络加载的话,此时正在子线程
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ScriptLoadUtil.setJsBundleAssetPath(
                            reactInstanceManager.getCurrentReactContext(),
                            path);
                    mDelegate.loadApp(getMainComponentNameInner());
                }
            });
        } else {//主线程运行
            ScriptLoadUtil.setJsBundleAssetPath(
                    reactInstanceManager.getCurrentReactContext(),
                    path);
            mDelegate.loadApp(getMainComponentName());
        }
    }

hexiangyuan avatar Aug 20 '20 09:08 hexiangyuan

可以参考 https://github.com/smallnew/react-native-multibundler/pull/88

hexiangyuan avatar Aug 20 '20 09:08 hexiangyuan