guy*_*.gc 7 android react-native
我正在开发一个现有的大型Android和iOs项目,我的团队希望采用本机反应.首先,我们希望在RN中实现一个功能,因为我们没有能力立即迁移整个项目.
我成功地添加了一个RN视图,既可以从本地节点服务器也可以从位于assets文件夹中的JS包文件中添加,但感觉有些笨拙.我知道iO支持这一点,但我在文档中找不到与现有Android项目集成的任何内容.以下是我提出的要点:
public class ReactNativeView extends FrameLayout {
private static final String COMPONENT_NAME = "AwesomeProject";
// Path of our RN module relative to project root
private static final String MODULE_NAME = "react-sources/index.android"
public ReactNativeView(Context Context) {
super(context);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ReactInstanceManager.Builder builder = ReactInstanceManager.builder()
.setApplication(AppDelegate.sharedApplication())
.addPackage(new MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.BEFORE_RESUME);
if (BuildConfig.USE_RN_LOCAL_SERVER) {
// Set module name to be loaded from local node server
builder.setJSMainModuleName(MODULE_NAME);
} else {
Uri uri = AssetsManager.getInstance().getJsBundleUri();
// Load bundled jsBundle
builder.setJSBundleFile(uri.getPath());
}
ReactInstanceManager reactInstanceManager = builder.build();
MainActivity mainActivity = MainActivity.getMainActivity();
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ReactRootView reactRootView =
new ReactRootView(AppDelegate.sharedApplication().getTopActivity());
// Is it really necessary to call 'startReactApplication' each time we want to render a react component?
reactRootView.startReactApplication(reactInstanceManager, COMPONENT_NAME, null);
addView(reactRootView);
// After the view is added to the hierarchy we call the manager's 'onResume' function
// to show the react view
reactInstanceManager.onResume(mainActivity, mainActivity); // <-- What kind of sorcery is this?!
}
}
Run Code Online (Sandbox Code Playgroud)
我还在BuildConfig中添加了一个变量来确定要使用的JS包:
buildTypes {
debug {
buildConfigField "boolean", "USE_RN_LOCAL_SERVER", "true" // load from local server
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "boolean", "USE_RN_LOCAL_SERVER", "false"
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个完整的黑客.startReactApplication当我正在做的事情只是在屏幕上添加一个视图感觉很尴尬时调用.我错过了API或者这不是支持的方案吗?
| 归档时间: |
|
| 查看次数: |
1268 次 |
| 最近记录: |