mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager,"HelloWorld",null);
setContentView(mReactRootView);
我就从这个HelloWord入手,首先我们实例化了一个ReactInstanceManager,startReactApplication中会走到ReactInstanceManager中的createReactContext方法,
里面有这么一段:
CoreModulesPackage coreModulesPackage =
new CoreModulesPackage(this,mBackBtnHandler,mUIImplementationProvider);
processPackage(
coreModulesPackage,reactContext,moduleSpecs,reactModuleInfoMap,jsModulesBuilder);
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
CoreModulesPackage里面包含一些接口原型,里面有个方法
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
List<Class<? extends JavaScriptModule>> jsModules = new ArrayList<>(Arrays.asList(
DeviceEventManagerModule.RCTDeviceEventEmitter.class,JSTimersExecution.class,RCTEventEmitter.class,RCTNativeAppEventEmitter.class,AppRegistry.class,com.facebook.react.bridge.Systrace.class,HMRClient.class));
if (ReactBuildConfig.DEBUG) {
jsModules.add(DebugComponentOwnershipModule.RCTDebugComponentOwnership.class);
jsModules.add(JSCHeapCapture.HeapCapture.class);
jsModules.add(JSCSamplingProfiler.SamplingProfiler.class);
}
return jsModules;
}
例如在这里面我们看到了AppRegistry,
public interface AppRegistry extends JavaScriptModule {
void runApplication(String appKey,WritableMap appParameters);
void unmountApplicationComponentAtRootTag(int rootNodeTag);
void startHeadlessTask(int taskId,String taskKey,WritableMap data);
}
这里面的方法是和AppRegistry.js方法是对应的,MainReactPackage是我们自己添加的,他里面的createJSModules是个空集合,没什么用,RN首先会把CoreModulesPackage里面的JS接口原型收集到JavaScriptModuleRegistry.Builder中,然后把我们自己添加的package中的JS原型收集到Builder中,从而构建出JavaScriptModuleRegistry,并且包含收集的JS接口原型,CatalyInstance在getJsModule方法中会根据参数拿到相应的接口原型,催化成动态代理
实例供给JAVA调用
原文链接:https://www.f2er.com/react/305447.html